No history yet

Data Consistency Models

Beyond Rigid Models

Most engineers are familiar with the classic database divide: relational databases offer ACID guarantees, while NoSQL databases follow the BASE model. ACID (Atomicity, Consistency, Isolation, Durability) provides strong transactional integrity, perfect for systems like financial ledgers where every transaction must be precise and reliable. BASE (Basically Available, Soft state, Eventual consistency) prioritizes uptime and scale, a great fit for social media feeds where showing slightly stale data is better than showing nothing at all.

But for a system architect, this binary view is too simple. The reality is a spectrum. Modern distributed SQL databases can be configured to sacrifice some consistency for higher availability, while many NoSQL systems can be tuned to provide strong, even linearizable, consistency at the cost of higher latency. Understanding this flexibility is key to designing effective, large-scale systems.

The CAP theorem states that it is not possible to guarantee all three of the desirable properties – consistency, availability, and partition tolerance at the same time in a distributed system with data replication.

The Fundamental Trade-offs

Two theorems dictate the choices you must make in any distributed system. The first is the well-known CAP theorem.

CAP states that a distributed system can only provide two of three guarantees: Consistency, Availability, and Partition Tolerance. Since network partitions are an unavoidable reality of distributed computing, the theorem forces a choice. When a partition happens, does your system prioritize returning the most recent, correct data (Consistency), or does it prioritize responding to every request, even if the data might be stale (Availability)?

However, the CAP theorem only describes behavior during a network failure. The PACELC theorem provides a more complete picture by considering the system's behavior during normal operation.

If there is a Partition (P), a system trades between Availability (A) and Consistency (C). Else (E), a system trades between Latency (L) and Consistency (C).

PACELC acknowledges that even without partitions, a fundamental trade-off exists between latency and consistency. To achieve strong consistency, a database often needs to coordinate with multiple nodes before confirming a write, which adds latency. Systems that relax consistency can often respond much faster. This 'Else' case is arguably more important, as systems spend most of their time in normal operation, not in a partitioned state.

A Spectrum of Consistency

The 'C' in both ACID and CAP is not a single state but a range of possibilities. Understanding these levels allows you to make precise architectural decisions.

Linearizability

noun

The strongest consistency model. If operation B starts after operation A completes, operation B must see the system in the same state it was in after A completed, or a later state. To an outside observer, the system appears to be a single, atomic unit.

Linearizability, often called strong consistency, is what many developers intuitively expect. It's atomic and instantaneous. But this guarantee comes at a high latency cost in distributed systems.

A more relaxed and often more practical model is causal consistency. This model guarantees that operations with a potential causal relationship are seen in the same order by all replicas. For example, a reply to a comment in a forum post should never appear before the original comment. Unrelated operations, like two users posting different comments, can appear in different orders on different replicas without issue.

Finally, there is eventual consistency. This model simply guarantees that if no new updates are made, all replicas will eventually converge to the same state. During the convergence window, reads may return stale data. This is the 'Soft state' of the BASE model.

Tunable Consistency

Modern distributed databases like Cassandra, DynamoDB, and Riak don't force you into a single consistency model. Instead, they offer tunable consistency, allowing you to make trade-offs on a per-operation basis. This is typically managed through a quorum-based system.

R+W>NR + W > N

Let's say you have a system with a replication factor (N) of 3. You can configure your operations in several ways:

  • Strong Consistency (CP): Set W=3 and R=1. Every write must go to all replicas. This is slow but guarantees that any subsequent read will see the data. Alternatively, you could use W=2 and R=2. This configuration satisfies the R + W > N rule (2 + 2 > 3), guaranteeing that your read quorum (R) will always overlap with at least one node from the successful write quorum (W).

  • High Availability (AP): Set W=1 and R=1. A write succeeds as soon as one replica confirms it, and a read returns as soon as one replica responds. This is fast and highly available, but reads might hit a replica that hasn't yet received the latest write, resulting in eventual consistency.

This tunable approach allows an architect to decide, for example, that a user profile update requires strong consistency (W=3, R=1), while logging a 'like' can tolerate eventual consistency (W=1, R=1) for better performance. You can shift between CP and AP behavior not just for the entire system, but for each individual operation.

Quiz Questions 1/6

According to the CAP theorem, a distributed system must make a trade-off between which two properties when a network partition occurs?

Quiz Questions 2/6

Which of the following systems is the most appropriate candidate for a database designed with the BASE model (Basically Available, Soft state, Eventual consistency)?

By moving beyond simple ACID vs. BASE labels and understanding the underlying theorems and consistency models, you can design systems that are precisely tailored to their specific requirements for correctness, availability, and performance.