TL;DR
A consistency model is a contract between a replicated store and the programs that use it. It says which value a read may return, given the writes that came before it and the copies that have already received them. Strong models hide the copies from the programmer and cost coordination on every operation. Weak models answer fast and leave the application to cope with stale or conflicting values. The choice matters because copies on different machines are never changed at the same instant.
Builds on: Linearizability, Happened-Before Relation, Quorum · Leads to: Network Partition, Dynamo, Facebook's memcache system
Where it's used
Shopping carts that must stay writable. The Dynamo paper describes a key-value store built so that writes keep being accepted when some machines or links fail. A write goes to whichever replicas are reachable, so two copies of one customer's cart can differ for a while. The store then merges them. The consistency model is the promise that the copies converge and that an item the customer added is not silently dropped.
Caches in front of a database. The Facebook memcache paper describes a large cache that sits in front of a database and serves most reads. After a write, a cache entry can briefly hold an old value. The system accepts that short window in exchange for speed, and uses invalidation messages to shrink it. A reader who needs the newest value cannot get it from the cache alone, so the model being offered is weaker than the database's own.
Leader and lock services. A coordination service that stores which machine is the leader needs reads that never return an older leader after a newer one was acknowledged. Otherwise two machines can both act as leader. Services of this kind are built on Distributed Consensus, for example with Paxos, and they choose a strong model for exactly this reason.
How it works
Take one key x stored on two replicas, A and B. Time is counted in plain ticks. A client writes x = 1 to A at tick 0. A forwards the write to B, and the message takes 5 ticks to arrive.
-
At tick 0, the client writes
x = 1to A. A stores 1 and replies "ok" right away. B still holds 0. Check: after tick 0, A holds 1 and B holds 0. The copies disagree, and that is normal for a replicated store. -
At tick 2, a second client reads
xfrom B and gets 0. Is that allowed? It depends on the model. The write finished at tick 0, before this read started, so under Linearizability the read must return 1, and the answer 0 breaks the rule. Under eventual consistency the answer 0 is allowed, because the model only promises that the copies agree later. Check: the same history is correct under one model and wrong under another. That is the point of naming a model. -
At tick 5, the forwarded message arrives and B stores 1. From now on the copies agree. If no new writes come in, every later read on either replica returns 1. Check: the disagreement lasted 5 ticks. A model that bounds this time is stronger than one that does not.
-
Add a conflict, in a second run that starts the same way. At tick 3, before the message of step 3 arrives, a second client writes
x = 2to B instead of reading. Now A holds 1 and B holds 2, and each replica forwards its write to the other. Now each replica has two candidate values. The store needs a rule to choose. With last-writer-wins, each write carries a timestamp and the larger timestamp is kept. Suppose the write of 2 carries timestamp 3 and the write of 1 carries timestamp 0. Both replicas then keep 2. Check: both replicas end at 2, whichever message arrived first. The merge rule, not the arrival order, decides the result.
sequenceDiagram participant C1 as Client 1 participant A as Replica A participant B as Replica B participant C2 as Client 2 C1->>A: write x = 1 (tick 0) A-->>C1: ok A-->>B: replicate x = 1 (arrives tick 5) C2->>B: read x (tick 2) B-->>C2: 0 C2->>B: write x = 2 (tick 3) B-->>A: replicate x = 2 Note over A,B: both keep the write with the larger timestamp
The math
A consistency model is a rule that a history must satisfy. A history is the list of reads and writes on a replicated store, each with its invocation time, its response time and the value it returned or wrote. The model decides which histories are allowed. A store provides a model when every history it can produce is allowed.
Step 1 in symbols: what the eventual rule says
Let be the key and let be the value that the last write leaves. For each replica , let be the value it holds at time . Eventual consistency says that for every replica there is a time such that for all .
Nothing in this rule constrains for . That is why the read at tick 2 in step 2 of the walkthrough is allowed. The rule describes the end state, not the path.
Step 2 in symbols: causal order
Write when event happened before event in the sense of the Happened-Before Relation: same machine in program order, or a send before its receive. Two writes with neither nor are concurrent.
Causal consistency says: if , then every replica applies before it applies . Concurrent writes carry no such rule, so two replicas may apply them in different orders. Step 4 of the walkthrough had two concurrent writes, which is why the merge rule was needed.
Step 3 in symbols: the last-writer-wins rule
Each write carries a timestamp . A replica that has seen a set of writes keeps the value of the write with the largest timestamp:
In the walkthrough, and , so and the value is 2. Ties are broken by a fixed rule such as the replica identifier, so that every replica makes the same choice.
Step 4 in symbols: quorum overlap
Let there be replicas. A write is acknowledged by of them, and a read asks of them. Call the two sets and , so and . If , then is not empty. The reason is the pigeonhole principle: if the two sets were disjoint, together they would need distinct replicas, which is more than .
This overlap is the ingredient that lets a read see the latest acknowledged write, as the Quorum lesson explains. It is not the whole story. A read also has to pick the newest version among the replies, and the system must avoid other paths that bypass the quorum.
Worked examples
A key starts at 0 on replicas A and B. A client writes 7 to A. The write is acknowledged at tick 1. B learns the value at tick 4.
- A read from B at tick 2 returns 0. Under linearizability this is forbidden, because the write finished at tick 1, before the read began at tick 2. Under eventual consistency it is allowed.
- A read from B at tick 5 returns 7. The write has reached B, so every model accepts it.
- A read from A at tick 2 returns 7. A holds the value from the moment it was acknowledged, so this read is allowed under any model.
- Interpretation: the model decides whether the answer in step 1 is a bug or the normal behavior of the system. The same history can be correct under one contract and wrong under another.
Three replicas are numbered 1, 2 and 3. Each write must be acknowledged by 2 of them, so . Each read asks 2 of them, so .
- Check the condition: , and . So the condition holds.
- A write is acknowledged by replicas . A later read asks replicas . The intersection is , which is not empty.
- Replica 2 holds the new value. Among the two replies, the read takes the one with the newer version, so it returns the new value.
- Now try the weaker setting and . The write is acknowledged by and the read asks . The intersection is empty, because .
- Replica 3 has not received the write, so the read returns the old value. The condition fails: , which is not greater than 3.
- Interpretation: the overlap condition is what makes a read find the latest acknowledged write. Choosing smaller quorums makes operations faster, but it removes that guarantee.
A customer's cart is stored on two replicas. Each write stores the whole cart, as a list of items, with a timestamp. Assume the customer starts with a cart that holds bread.
-
The phone writes the cart as bread and milk, with timestamp 10:00:01.
-
The laptop, which has not seen the phone's write, writes the cart as bread and eggs, with timestamp 10:00:02.
-
The two writes are concurrent, because neither device saw the other. Each replica receives both.
-
Last-writer-wins keeps the write with the larger timestamp, the laptop's bread and eggs. The milk is lost, and nothing reports it.
-
A union merge instead combines the two carts into bread, milk and eggs. Nothing is lost, but the rule must know that an item removed on one device should stay removed, which a plain union cannot tell.
-
Interpretation: the consistency model decides what the store promises about this conflict. Last-writer-wins gives a simple, always-convergent answer that can silently drop data. A merge keeps data but needs a richer rule.
Try it: the phone writes bread and milk at 10:00:05. The laptop writes bread only at 10:00:03, without having seen the phone's write. Under last-writer-wins, which cart remains? Answer: the phone's cart, bread and milk, because its timestamp 10:00:05 is later than 10:00:03. The laptop's write is discarded.
Common mistakes and tradeoffs
It is tempting to think that eventual consistency means the copies agree quickly. In fact, the model gives no time bound. The copies agree once every message has arrived, and a long partition or a slow link can keep them apart for as long as it lasts. A bound on the delay is a stronger promise and has to be stated separately.
It is tempting to think that a weak model means reads are random. In fact, weak models often keep session guarantees for a single client. Read-your-writes says that a client sees its own earlier write. Monotonic reads says that a client never sees an older value after a newer one. These are cheaper than full linearizability and cover most user-facing bugs.
It is tempting to think that a replicated store is consistent by default. In fact, a replica that accepts writes and forwards them later can answer with an old value at any time. Any guarantee must be built in, with a mechanism such as a leader, a quorum, or a replicated log as in State Machine Replication.
It is tempting to think that the strongest model is always the right one. In fact, each step up costs coordination. Strong models need replicas to talk before answering, which adds latency, and during a Network Partition some requests must wait or be refused. The CAP theorem paper states this tradeoff.
Use a strong model when a stale answer causes a wrong decision that is hard to undo, such as a lock, a leader, a unique claim, or a stock count that gates a sale. Use a weak model with session guarantees when slightly old data is harmless and availability matters more, such as a cached product page, a feed, or a view counter. Use a merge rule when losing a write is unacceptable and the data can be combined.
| Model | What it promises | Typical cost |
|---|---|---|
| Linearizable | Each operation appears to happen at one instant, in real-time order | Coordination on every operation |
| Sequential | One order of operations seen by all, respecting each client's own order | Less than linearizable, still global agreement |
| Causal | Causally related writes are seen in order; concurrent ones may differ | Tracks dependencies, e.g. with vector clocks |
| Session | One client's reads and writes stay consistent with each other | Cheap, per-client state |
| Eventual | Copies agree if writes stop | Lowest latency, needs a merge rule |
History
In 1979 Leslie Lamport introduced sequential consistency, a model for shared memory on multiprocessors, because programs on such machines could observe memory operations in orders that the programmers had not expected. It was an early clear statement that the order of memory operations needed a contract.
Systems for weakly connected replicas, such as the Bayou project in the 1990s, formalized session guarantees for clients that move between replicas. In the late 2000s the term eventual consistency became common, partly through writing on the design of large key-value stores. The Dynamo paper in 2007 described a production shopping system that accepted writes during failures and relied on merge rules afterwards, which made weak models a common engineering choice.
One reason it caught on is that coordination cost grows with the number of machines involved in each operation. A model that lets each copy answer alone scales to large loads, and many services needed that.
Recap
- A replicated store hides its copies behind a contract, the consistency model, which says what a read may return.
- Linearizability is the strongest common contract. Causal and session models keep only the orders users notice, and eventual consistency promises only that copies agree once writes stop.
- Quorums with make a read meet every acknowledged write, and last-writer-wins or a merge rule makes concurrent writes converge.
- The tradeoff is latency and availability: each step toward strong consistency makes some requests wait or fail during a partition.
Read next: Network Partition for what happens when replicas cannot reach each other, and the Dynamo paper for a system that chose a weak model on purpose.
Check yourself
QuestionUnder linearizability, a write completes at tick 4 and a read starts at tick 5. Must the read return the written value? Why?Show answer
Yes. The write finished before the read started, so real-time order places the write first. A linearizable read cannot return a value older than the last write that completed before it.
QuestionThree replicas, . A write is acknowledged by replicas and a read asks . Do the two sets always share a replica? Give the reason.Show answer
Yes. The write set and the read set each have 2 replicas, so together they name 4 slots among only 3 replicas. By the pigeonhole principle at least one replica appears in both sets, because .
QuestionTwo writes are concurrent. Write a has timestamp 8 and write b has timestamp 5. Under last-writer-wins, which value remains, and does the outcome depend on the order in which replicas received the writes?Show answer
Write a remains, because 8 is larger than 5. The outcome does not depend on arrival order, since every replica compares the same two timestamps and keeps the larger one.
QuestionA product page cache is 30 seconds behind the database. A shopper sees a price that changed a minute ago. Is this a consistency bug? What would make it one?Show answer
Not by itself. A cache that is allowed to lag under an eventual or session model is working as designed, and a short stale price is a known tradeoff. It becomes a bug if the contract promised a bound the system breaks, or if the shopper's own earlier update is not visible to that shopper, which would violate read-your-writes.