SysDesignAtlas

Consistency Models

The spectrum from linearizability to eventual consistency, the session guarantees that solve most real problems cheaply, and how to pick one without saying "strong" or "eventual.

CoreHard11 min readconsistencylinearizabilityreplication
Confidence

#Why there's a spectrum

Once your data lives on more than one machine, a question appears that never came up with a single database: if I write something here and read it there a millisecond later, am I guaranteed to see it? A consistency model is the storage system's answer — a contract saying which results a read is allowed to return when writes are happening at the same time.

The stronger the model, the more the system must coordinate, and coordination costs round trips and availability. So this is not a quality ladder where stronger is better — it's a price list. Your job is to buy the weakest model that keeps the product correct.

Most discussions use only two words, "strong" and "eventual" — and the entire interesting middle is where good designs live.

  STRONGER / more coordination / slower / less available
     ▲
     │  Linearizability        looks like one copy; newest write always wins
     │  Sequential             everyone agrees on an order — just not the clock's
     │  Causal                 cause always lands before effect
     │  Session guarantees     promises to one user: you see your own writes
     │  Eventual               all copies agree in the end, given quiet
     ▼
  WEAKER / cheap / fast / highly available

#Linearizability (strong consistency)

The contract: the system behaves as if there is exactly one copy of the data, and every operation takes effect atomically at some instant between its invocation and its response. In plainer terms: each operation appears to happen at one single instant, somewhere between when you sent it and when you got the reply. Once a write completes, every subsequent read — from any client — sees it or something newer.

That last rule is the "real-time" clause, and it is what makes linearizability strong and expensive. If Alice's write finished at 12:00:01 and Bob starts a read at 12:00:02, Bob must see Alice's write — even if Bob is on another continent and has never heard of Alice. The order has to match the wall clock, not just be some story the replicas agree on.

In the diagram below, each bar is one operation, stretching from when it was sent to when it returned.

  LINEARIZABLE                        NOT LINEARIZABLE

  W(x=1) ├──┤                         W(x=1) ├──┤
  R(x)        ├──┤ → 1                R(x)        ├──┤ → 1
  R(x)            ├──┤ → 1            R(x)            ├──┤ → 0  ✗
                                                   (went backwards
                                                    in time)

Cost: every read has to prove it isn't out of date — by checking with a majority of the copies (a quorum), or by talking to a designated leader that holds a time-limited exclusive right to answer (a lease). Minimum one round trip to a majority. Cross-region, this means 100 ms+ per operation, and during a partition the minority stops serving.

Where it's required: distributed locks and leader election, uniqueness constraints, inventory decrements on the last unit, account balance before a withdrawal, anything where the read causes an irreversible action.

Systems: etcd, ZooKeeper, Spanner, single-node Postgres, DynamoDB with ConsistentRead=true.

#Sequential consistency

Everyone sees all operations in the same single order, and each client's own operations stay in the order that client issued them — but that shared order need not match real time. If Alice writes and then Bob (having never talked to Alice) reads, Bob may legally see the old value.

Weaker than linearizability, rarely offered as an explicit database guarantee, but useful vocabulary: it's the difference between "there's one agreed history" and "that history matches the clock on the wall."

#Causal consistency

The contract: operations that are causally related are seen in the same order by everyone. Concurrent (unrelated) operations may be seen in different orders by different observers.

Causality is defined by the happens-before relation: if you read X and then write Y, Y may depend on X, so everyone must see X before Y.

The canonical motivating failure:

  Alice:  "I lost my keys!"          (post P1)
  Bob:    "Glad you found them!"     (reply P2, depends on P1)

  Carol's replica receives P2 before P1:

      "Glad you found them!"    ← nonsense
      "I lost my keys!"

Causal consistency makes this impossible while still allowing two unrelated posts to arrive in any order — which is fine, since nobody can tell.

This is the sweet spot for social systems. It's the strongest model that remains fully available during partitions, which is a genuinely remarkable result. Implemented with vector clocks, dependency tracking, or hybrid logical clocks.

#Session guarantees — the practical workhorses

These are per-client promises: each one is about what one user sees, not what everyone sees. They're cheap, you can combine several of them, and they solve the overwhelming majority of user-visible consistency bugs.

Read-your-writes. A client always sees its own writes. Without it: you edit your profile, the read hits a lagging replica, your change appears to vanish, you edit again — and now you have a duplicate or an angry user.

Implementations: route a user's reads to the primary for N seconds after a write; pin the session to one replica; or use a version token — the write returns a version number, the client sends it along with its next read, and a replica only answers once it has caught up to at least that version.

Monotonic reads. Time never goes backwards for one client. Without it: refresh shows 51 comments, refresh again shows 49 (you hit a more-lagged replica), refresh again shows 51.

Implementation: pin the client to a replica by consistent hashing on user ID, or track the highest version seen and never serve older.

Monotonic writes. A client's own writes apply in the order issued. Without it, "create doc" and "rename doc" can land out of order and the rename fails.

Writes-follow-reads. If you read a value and then write, your write is ordered after the value you read — the mechanism behind sensible reply threading. You read Alice's post and reply to it; every replica places your reply after her post, never before it.

#Eventual consistency

The contract: if writes stop, all replicas eventually converge. That's the whole promise — no bound on "eventually," no guarantee about what you see in the meantime.

It's the weakest useful model, and it's genuinely fine for a lot of data: view counts, recommendation feeds, search indexes, denormalized aggregates (precomputed totals like "comments on this post"), DNS.

Its two failure modes to be ready for:

  1. Conflict resolution. When two replicas take conflicting writes, someone must decide. Last-write-wins is easy and silently destroys data. See CRDTs for the principled alternative.
  2. Unbounded staleness. "Eventually" can mean hours if a replica is partitioned. Production systems monitor and alarm on replication lag precisely because the model gives no guarantee.

#Isolation is a different axis

A frequent confusion worth pre-empting: consistency models describe replication; isolation levels describe concurrent transactions. They're independent of each other — a system can be strong on one and weak on the other.

  • Consistency: "I have five copies of this data — what can a reader see?"
  • Isolation: "Two transactions are interleaving — what anomalies are possible?"

Serializable isolation (transactions behave as if they ran one at a time) on a single node says nothing about replica freshness. Linearizability on a key-value store says nothing about multi-key transactions. Strict serializability is the combination of both, and it's the strongest (and most expensive) guarantee on offer — Spanner's headline feature. See Transactions & Isolation.

#Choosing, concretely

Ask three questions:

  1. What's the cost of a stale read? Wrong like-count → nothing. Wrong balance → money moves incorrectly. This alone eliminates most of the spectrum.
  2. Does a read cause an irreversible action? Reading inventory to display it can be stale. Reading inventory to decide whether to sell cannot.
  3. What does the user perceive? Users notice their own changes disappearing far more than they notice other people's changes arriving late. Hence: session guarantees buy most of the perceived correctness of strong consistency for a fraction of the cost.
DataModelReasoning
Account balance (for transfer)LinearizableRead drives an irreversible action
Seat / ticket assignmentLinearizableDouble-booking is unrecoverable
Username registrationLinearizableUniqueness is a hard invariant
Your own profile editsRead-your-writesOnly you notice the lag
Comment threadsCausalReplies must follow parents
Feed timelineEventual + monotonicStaleness invisible; going backwards is not
Like / view countsEventualApproximation is acceptable by design
Search indexEventualSeconds of lag is expected behavior

#The sentence worth remembering

"Different data in this system needs different guarantees, so I'll pick per operation rather than globally. The transactional core — inventory and payment — needs linearizability, and I'll accept the coordination cost there. Feed and counters go eventual for availability and latency. The one place I'd add a session guarantee is the user's own posts: read-your-writes, so their content never appears to vanish after they publish it."

That sentence contains a model choice, a justification, an acknowledged cost, and a user-facing consideration — which is precisely the four-part structure the rubric rewards.

#Check yourself

What does linearizability promise, and what does it cost?Show answer
The system behaves as if there's one copy: once a write completes, every later read from anyone sees it or something newer. Every read must check with a quorum or a lease-holding leader — at least one round trip, 100 ms+ across regions.
When is linearizability genuinely required?Show answer
When a read leads to an irreversible action: locks and leader election, uniqueness (usernames), the last unit of inventory, a balance check before a withdrawal.
What does causal consistency guarantee, and why is it the "sweet spot" for social apps?Show answer
Causally related operations — like a reply and the post it answers — are seen in the same order by everyone, while unrelated ones may arrive in any order. It's the strongest model that stays available during a partition.
A user edits their profile and the change seems to vanish on reload. Which guarantee fixes it, and how?Show answer
Read-your-writes. Route that user's reads to the primary for a short while after a write, pin their session to one replica, or have the client carry a version token the replica must have caught up to.
What breaks without monotonic reads?Show answer
Time goes backwards for one user: a refresh shows 51 comments, the next shows 49 from a more lagged replica, then 51 again. Pinning the user to one replica fixes it.
What does eventual consistency promise — and what two problems must you handle?Show answer
Only that replicas converge once writes stop. You must resolve conflicting writes (last-write-wins silently loses data) and monitor replication lag, because "eventually" has no bound.
Consistency models versus isolation levels — what's the difference?Show answer
Consistency is about replicas: what a reader may see across copies. Isolation is about concurrent transactions: which anomalies interleavings can cause. Strict serializability is both at once.
What three questions pick a consistency model?Show answer
What does a stale read cost? Does the read cause an irreversible action? What will the user notice — usually their own changes vanishing, which session guarantees fix cheaply.

#What to carry forward

  • A consistency model is a price list, not a quality ladder: buy the weakest model that keeps the product correct.
  • Linearizability is for reads that trigger irreversible actions; it costs a quorum round trip on every read.
  • Causal consistency keeps replies after their posts and stays available during partitions.
  • Session guarantees — read-your-writes, monotonic reads — fix most user-visible bugs for the price of a routing rule.
  • Consistency (replicas) and isolation (transactions) are different axes; strict serializability is both.
00:00
/ 11m