Consistency models
Overview
Once data is copied to several replicas, a reader might hit a copy that hasn’t seen the latest write. A consistency model is the contract that says what a reader may observe. Stronger models are easier to program against but cost latency and availability; weaker ones are fast but allow surprises. Good designs pick the model per feature.
The spectrum
Linearizability (strong consistency)
The system behaves as if there were a single copy of the data and every operation happened at one instant. Once a write completes, every later read, from anyone, sees it (or something newer).
- Needed for: distributed locks, leader election, unique usernames, account balances, seat reservations, anything where two clients racing must agree on one winner.
- Cost: every write (and often every read) must coordinate with a majority or a leader, which adds latency and means the system can’t answer during some network partitions (next lesson).
Sequential and causal consistency
Causal consistency guarantees that if one operation could have influenced another, everyone sees them in that order. A reply is never visible before the message it replies to; a comment never appears on a post that a reader can’t see yet. Unrelated operations may appear in different orders to different readers. It’s much cheaper than linearizability and matches what users actually notice.
(Sequential consistency is between the two: everyone sees all operations in the same order, though not necessarily in real-time order.)
Session guarantees
Cheap promises scoped to one user’s session that remove the most confusing anomalies:
- Read-your-writes: you always see your own updates.
- Monotonic reads: once you’ve seen a value, you never see an older one.
- Monotonic writes: your writes are applied in the order you made them.
- Writes follow reads: a write you make after reading something is ordered after that thing.
Implement them by routing a user to the leader after they write, pinning a session to one replica, or tracking the version each session has seen (week 3, replication).
Eventual consistency
If writes stop, all replicas eventually hold the same value. There’s no promise about what you see in the meantime: stale values, values going backwards, different values on each refresh.
- Fine for: like and view counts, feeds, recommendations, search indexes, analytics, presence indicators.
- Benefits: lowest latency, highest availability, easy multi-region writes.
“Eventually” is usually milliseconds to seconds, but under failures it can be much longer, so measure replication lag.
Strong consistency inside, eventual outside
A common, effective pattern:
- the source of truth (orders, payments, inventory, user accounts) lives in a strongly consistent store,
- derived data (caches, search indexes, feeds, analytics, read models) is updated asynchronously and is eventually consistent,
- user-facing session guarantees hide most of the lag.
Tunable consistency
Some databases let you choose per request. Cassandra offers consistency levels such as ONE, QUORUM and ALL; DynamoDB offers eventually consistent or strongly consistent reads; MongoDB has read and write concerns. Quorums (a later lesson) explain how these work.
In an interview
When you introduce replicas, caches or asynchronous updates, state the consistency each feature gets: “Usernames are claimed with a unique constraint on the primary, so they’re strongly consistent. The feed is eventually consistent, typically a few seconds behind. Users always see their own posts because we read their recent writes from the primary.”
Recap
Consistency models describe what readers of replicated data can see. Linearizability acts like a single copy and is required for uniqueness, locks and money; causal consistency preserves cause and effect; session guarantees fix per-user anomalies cheaply; eventual consistency only promises convergence and suits counters, feeds and derived data. Choose the weakest model each feature can tolerate.
Assign a consistency level
For each feature of a social app with replicated storage, choose the weakest consistency model that is still acceptable and explain why: (a) the number of likes on a post; (b) a user changing their password; (c) replies in a comment thread; (d) a user editing their bio and immediately viewing their profile; (e) claiming a unique username.
(a)
(b)
(c)
(d)
(e)(a) Eventual: a like count that's a few seconds behind or slightly off is fine.
(b) Strong (linearizable) for the credential check: once the password changes, the old
one must stop working everywhere immediately, and sessions should be revoked.
(c) Causal: a reply must never appear before the comment it answers. Exact global
ordering of unrelated comments doesn't matter.
(d) Read-your-writes (a session guarantee): the user must see their own new bio;
other users can see it a little later.
(e) Linearizable / strongly consistent: two people must never both get the same
username, so use a single authoritative store with a unique constraint or a
consensus-backed check.Summary
- A consistency model is a promise about what readers can see when data is replicated.
- Linearizability: the system behaves like a single up-to-date copy; required for locks, uniqueness and money movements.
- Causal consistency keeps cause before effect (a reply never appears before its question) without a global order.
- Session guarantees such as read-your-writes and monotonic reads fix the most visible anomalies for each user cheaply.
- Eventual consistency only promises that replicas converge; it's fastest and most available, and fine for counters and feeds.