Consistency models
What "consistent" means when data lives on several machines, from linearizability down to eventual consistency, and how to choose the weakest model a feature can tolerate.
The problem
A marketplace runs on one PostgreSQL primary. Reads grow, so the team adds two read replicas and sends most queries to them, as the scalability playbook suggests. Within a week, support has a new kind of ticket. “I changed my delivery address and it changed back.” “I posted a review, it disappeared, and then it came back.” Nothing was lost. The replicas apply each change a fraction of a second after the primary, and some reads arrive inside that gap.
Step 1 of 8
One leader takes every write. Two followers copy its changes and serve the reads. The copying is asynchronous: it usually takes a fraction of a second, but nothing guarantees that.
Read the steps as text
- One leader takes every write. Two followers copy its changes and serve the reads. The copying is asynchronous: it usually takes a fraction of a second, but nothing guarantees that.
- A user posts a comment. The write goes to the leader, which stores it and confirms. As far as the user is concerned, the comment is saved.
- The page reloads straight away, and the read is routed to follower 1, which has not received the change yet. The comment is missing. The user did not get read-your-writes, and assumes the comment was lost.
- Follower 2 now receives the change. The user refreshes, and this time the read happens to go to follower 2. The comment appears.
- One more refresh goes back to follower 1, which is still behind. The comment vanishes again. The user has watched time run backwards, which is a violation of monotonic reads.
- A moment later follower 1 catches up and all three copies agree. That is all eventual consistency promises: the copies converge in the end. It says nothing about what a read returns on the way there.
- The first fix gives read-your-writes. For a short while after a user writes, send that user's reads to the leader, or to a follower known to have caught up to that write.
- The second fix gives monotonic reads. Pin each user to one follower, so their view only ever moves forward. Both fixes are cheap session guarantees. Other users may still see slightly old data, and for a comment that is fine.
The first fixes are blunt. Send every read to the primary, which throws away the replicas. Make the primary wait for every replica before confirming a write, which makes each write as slow as the slowest replica and makes writes fail when any replica is down. Add a one-second pause after writes, which fails on the first day that lag exceeds a second.
The useful question is more precise: what exactly is a read allowed to return? A consistency model is an answer to that question. It is a contract between a data store and the code that uses it. Stronger models are easier to program against and cost more in latency and availability. The skill is to choose the weakest one each feature can live with.
Not the C in ACID
The word is overloaded. In ACID, consistency means that a transaction takes the database from one valid state to another, with constraints and invariants intact. Isolation, a separate letter, describes how concurrent transactions interfere, and is covered in ACID and isolation levels.
In distributed systems, consistency is about copies. When the same data lives on several machines, which values may a read observe, and in what order? That is the subject here.
The models, strongest first
Linearizability. The system behaves as if there were one copy of the data and every operation took effect at a single instant between its request and its response. Once any client’s write has completed, every later read by any client returns that value or a newer one. Herlihy and Wing defined it in 1990. Providing it usually means sending operations through a leader and confirming each one with a majority of replicas, using a consensus protocol such as Raft or Paxos. The cost is a round trip to a majority on every operation, and replicas that cannot reach a majority must refuse to answer.
Sequential consistency. All clients see operations in one shared order, and each client’s own operations appear in the order it issued them, but that order need not match real time. A read may miss a write that has already completed elsewhere. Followers replaying a replicated log are in this position: each is at some earlier point in the same history. ZooKeeper works this way. Its writes go through the leader in order, while a read is served by whichever server the client is connected to and may be slightly behind.
Causal consistency. Operations related by cause and effect are seen in that order by everyone: a reply never appears before the message it answers. Unrelated operations may be seen in different orders by different clients. Research results suggest that this is about the strongest model a system can offer while staying available during a network partition, which makes it attractive for products spread across regions.
Session guarantees. These are narrower promises made to one client, first described for the Bayou system in 1994. Two matter most:
- Read-your-writes. After you write, your own reads reflect that write.
- Monotonic reads. Once you have seen a value, you never see an older one.
They are cheap. For read-your-writes, send a user’s reads to the leader for a short time after they write, or have the client carry the log position of its last write and wait for a replica that has reached it. For monotonic reads, pin each user to one replica.
Eventual consistency. If writes stop, the replicas converge. There is no bound on when, and until then a read may return any value that some replica holds. Concurrent writes to different replicas need a rule. Last write wins is simple, but it silently discards one of the writes, and clock skew can make it discard the newer one. The alternatives are to keep both versions for the application to merge, or to use data types built to merge automatically, known as CRDTs.
| Model | What a read can return | Typical use |
|---|---|---|
| Linearizable | The latest completed write, always | Locks, leader election, unique names, stock counts |
| Sequential | A possibly old point in one shared history | Reads from the followers of a replicated log |
| Causal | Everything your view depends on, in order | Comment threads, collaborative features |
| Session guarantees | Your own writes, and nothing older than you have seen | Profiles and settings |
| Eventual | Any recent value, converging later | Like counts, view counts, DNS, caches |
Quorums, briefly
Leaderless stores in the style of Amazon’s Dynamo, such as Cassandra and Riak, let you tune consistency per request. Each key is stored on N replicas. A write is acknowledged once W replicas confirm it. A read asks R replicas and returns the newest version it finds.
If R + W > N, the read set and the write set share at least one replica, so the read finds the latest acknowledged write.
- N = 3, W = 2, R = 2 is the usual choice. Reads and writes both survive one replica being down.
- W = 3, R = 1 gives fast reads, but a write fails when any replica is down.
- W = 1, R = 1 is the fastest and most available, and reads may be stale.
One caution. Overlap is not linearizability. Two concurrent writes can each reach a quorum and one can be silently dropped by last write wins. A write that fails on some replicas is not rolled back on the others. A read that runs during a write may see the new value and a later read the old one. Cassandra, for example, offers linearizable operations only through its separate lightweight transactions, which run Paxos. Replication goes deeper.
Choosing the weakest model that works
For each operation, ask: what is the worst thing that happens if this read is stale, or if these two writes conflict?
- Nobody is harmed. A like count is off by three for a second. Use eventual consistency.
- The author is confused. Their edit seems to be missing. Add read-your-writes and monotonic reads.
- The content stops making sense. A reply appears before its question. Use causal consistency.
- An invariant breaks. Two people get the last seat, two accounts get one username, or a balance goes negative. Use linearizable operations, or a serializable transaction on a single leader.
When to choose a strong model. When correctness depends on an invariant across concurrent actors, and for small amounts of coordination data such as locks and configuration.
When not to. For high-volume, read-heavy data served to users in several regions, where every strongly consistent operation would pay a cross-region round trip and staleness harms nobody.
Most products mix models. A ticketing site keeps seat inventory linearizable and serves event listings from eventually consistent caches. What happens to each choice when the network fails is the subject of CAP and PACELC.
In the interview
Raise consistency whenever you add replicas, a cache or a second region, and say what each read path can return. For example: “Profile reads go to replicas. After a user edits their profile, I route their reads to the leader for a few seconds so that they see their own change. Other users may see the old value for as long as the replication lag, which is fine.”
That answer names the model, the mechanism and the cost. Avoid describing the whole system as “strongly consistent” or “eventually consistent”. Go operation by operation, and keep the strong guarantees for the few operations that protect an invariant. If asked about quorums, give R + W > N, the N = 3 example, and the caution that overlap alone is not linearizability.
Key takeaways
- In distributed systems, consistency describes what reads may observe across replicas. It is not the C in ACID, which is about application invariants.
- Linearizability makes replicated data behave like one copy. It costs latency on every operation, and replicas cut off from the majority must stop answering.
- Read-your-writes and monotonic reads are cheap session guarantees that remove the anomalies users actually notice.
- Eventual consistency promises only that replicas converge once writes stop. It needs a rule for resolving conflicting writes.
- With N replicas, choosing R + W > N makes every read overlap the latest acknowledged write. That is necessary for strong reads but does not by itself give linearizability.
- Decide per operation. Ask what the worst outcome of a stale read or a conflicting write is, and pick the weakest model that rules it out.
Check yourself
0 of 5 answered
Flashcards
These 6 cards are in your review deck. Tap a card to see the answer.
Linearizability in one sentence
Every operation appears to take effect at a single instant between its start and its end, so once a write completes, every later read sees it. The system behaves like one copy.
Linearizability and serializability
Linearizability is about single operations on one object and respects real time. Serializability is about transactions over many objects behaving as if they ran one at a time. Having both is called strict serializability.
Read-your-writes
After you write, your own later reads reflect that write. Other users may still see older data for a while.
Monotonic reads
Once you have seen a value, you never see an older one. The usual mechanism is keeping a user on the same replica.
Eventual consistency
If writes stop, all replicas converge to the same value at some point. It gives no bound on when, and no promise about reads in the meantime.
The quorum rule
With N replicas, W write acknowledgements and R read responses, R + W > N means the read and write sets share at least one replica. N = 3, W = 2, R = 2 is the common choice.