Database Consistency Levels Explained
Consistency in distributed databases describes how up-to-date the data is across all nodes when a read occurs after a write. Different consistency levels offer trade-offs between correctness, availability, and performance.
Strong Consistency
Strong consistency guarantees that after a write completes, all subsequent reads return the most recent write. Every node sees the same data at the same time. This is the standard in single-node databases and distributed databases using consensus protocols.
Strong consistency requires coordination between nodes before confirming a write. Writes must replicate to a quorum of nodes (typically a majority). This adds latency proportional to network round-trips between nodes.
Eventual Consistency
Eventual consistency guarantees that if no new writes occur, all nodes will eventually return the same data. There is no time bound on when convergence happens. This is the weakest consistency model but offers the best performance and availability.
Eventual consistency is common in DNS systems, CDN caches, and some NoSQL databases (Cassandra with consistency level ONE). Most applications use eventual consistency for non-critical data where temporary staleness is acceptable.
Tunable Consistency
Cassandra popularized tunable consistency. Each read and write operation specifies the consistency level. Write ONE acknowledges after one node. Write QUORUM acknowledges after a majority. Write ALL acknowledges after all nodes. Applications choose the appropriate level per operation.
Tunable consistency enables performance optimization. Use higher consistency for critical operations and weaker consistency for high-throughput operations. Monitor consistency trade-offs through read-repair statistics and hinted handoff metrics.
PACELC Trade-offs
The PACELC theorem extends CAP: in a distributed system, if a partition occurs (P), you trade between availability (A) and consistency (C). Else (E), you trade between latency (L) and consistency (C).
Understanding PACELC helps choose the right consistency model. Systems that favor consistency (Spanner) have higher latency within a partition. Systems that favor availability (Cassandra with weak consistency) provide lower latency but may serve stale data.
Choosing Consistency
Use strong consistency for financial transactions, inventory management, and user profile updates where stale data causes errors. Use eventual consistency for social media feeds, analytics, and content delivery where temporary staleness is invisible to users. Use causal consistency for collaborative editing and messaging applications.