Consistency Models

The spectrum of guarantees for replicated data — linearizability, sequential and causal consistency, session guarantees (read-your-writes, monotonic reads), and eventual consistency — with examples of anomalies each one prevents.

Advanced 4 min readLesson 3 of 9 Interview Q&A

When data is replicated, different clients may see different versions at the same moment. A consistency model is a contract: it says which results reads are allowed to return. Stronger models are easier to program against but need more coordination (latency, reduced availability); weaker models are faster and more available but expose anomalies.

Linearizabilityone copy, real-time orderstrongestSequential consistencyone global order, consistent with each client's orderCausal consistencycause before effect, everywhereSession guaranteesread-your-writes, monotonic reads…Eventual consistencyreplicas converge if writes stopweakest

(These describe single-object or non-transactional operations. Transaction isolation levels — serializable, snapshot isolation — are a related but separate axis: Isolation levels. Strict serializability combines serializability with linearizability.)

Linearizability

The system behaves as if there is a single copy of the data, and every operation takes effect atomically at some instant between its start and its end. Once a write completes, every later read (in real time) sees it — or something newer.

A: write x=1B: read → 1C: read → must be 10457810

Once B has seen 1, C (starting later) can never see the old 0. Anomaly prevented: stale reads after a write is visible to someone else.

Needed for: leader election, distributed locks, uniqueness constraints (two users can't claim the same username), compare-and-set. Provided by consensus systems (etcd, ZooKeeper writes, Spanner), single-leader databases reading from the leader. Cost: every operation coordinates with a majority or the leader; unavailable in a minority partition (CAP).

Sequential consistency

All clients see operations in the same total order, and that order respects each client's own program order — but not necessarily real time. A read may return a slightly old value, as long as everyone agrees on the sequence. (CPU memory models discuss this level — see Java memory model.)

Causal consistency

Operations that are causally related are seen by everyone in the same order; concurrent (unrelated) operations may be seen in different orders by different clients.

Classic anomaly it prevents:

Alicereplica 1replica 2Bobpost: 'I lost my keys'comment: 'Found them!'replicates comment first…reads → sees 'Found them!' with no post ✗

Under causal consistency, the comment depends on the post, so no replica shows it before the post. Implemented with dependency tracking (version vectors, see Clocks & ordering). Causal consistency is the strongest model that remains available during partitions, which makes it attractive for geo-replicated systems (MongoDB causal sessions, COPS, many collaborative apps).

Session guarantees (client-centric)

Weaker than causal, but they fix the anomalies users notice most. Each applies within one client's session:

GuaranteePromiseAnomaly prevented
Read-your-writesYou always see your own writesUpdate your profile, refresh, the old name appears
Monotonic readsYou never see data go "back in time"A comment appears, then disappears on refresh (different replica)
Monotonic writesYour writes are applied in the order you made them
Writes-follow-readsA write made after reading X is ordered after XReplying to a message others can't see yet

Implementation tricks: route a user's reads to the leader for a while after they write; sticky sessions to one replica; clients pass the last-seen version (a log position or timestamp) and replicas wait until they've caught up before answering.

Eventual consistency

If no new writes arrive, all replicas eventually converge to the same value. That's all — no bound on how long, and no guarantee about what you see meanwhile (stale values, values going backwards, concurrent writes resolved somehow).

It's the default for highly available, low-latency systems: DNS, CDNs, caches, Dynamo-style stores, social feeds, view counters. Conflict resolution is the hard part: last-writer-wins (simple, but silently drops concurrent updates), application-level merging, or CRDTs that merge automatically (CRDTs).

Strong eventual consistency adds: replicas that have received the same set of updates are in the same state — guaranteed by CRDTs.

Choosing

Use caseModel
Locks, leader election, unique usernames, account balancesLinearizable
Collaborative docs, comments and replies, geo-replicated social appsCausal
User profile edits, shopping cart viewed by its ownerRead-your-writes + monotonic reads
Like counters, view counts, recommendations, DNSEventual
Key takeaways
  • Consistency models define which values reads may return under replication.
  • Linearizability: behaves like one copy in real time — needed for locks and uniqueness, costs coordination.
  • Sequential: one agreed order, not tied to real time.
  • Causal: causes are seen before effects; the strongest model available under partitions.
  • Session guarantees fix user-visible anomalies cheaply.
  • Eventual: replicas converge when writes stop; conflict resolution is the crux.
Interview questions
QWhat is the difference between linearizability and eventual consistency?medium

Linearizability guarantees every operation appears to take effect atomically at one instant between its start and end, so once a write completes all subsequent reads see it — the system behaves like a single copy. Eventual consistency only guarantees replicas converge once writes stop; meanwhile reads may return stale or out-of-order values. Linearizability needs coordination (higher latency, unavailable in minority partitions); eventual consistency is fast and available but requires handling anomalies and conflicts.