Skip to content

Consistency patterns

As soon as data has more than one copy — replicas, caches, Regions, a phone working offline — the copies can disagree for a while. A consistency model is the contract that says which disagreements a reader may observe. Choosing a model is a choice about latency, availability and how much the application code must cope with.

After a write, a read may or may not return it. No effort is made to converge.

  • Example: real-time media and games. If a video call drops packets for two seconds, nobody wants the lost audio replayed later — the stream moves on. Best-effort caches are similar: a cache node that restarts empty simply loses what it held.
  • Implementation: fire-and-forget updates, UDP streams, caches with no invalidation guarantees.

If writes stop, all replicas converge to the same value after some time. In the meantime a read may return an old value. “Eventually” has no upper bound in the definition. In practice the lag is milliseconds to seconds on a healthy system, and much longer during failures.

  • Example: DNS, social media counters, product catalogues, DynamoDB’s default reads, cross-Region replicas.
  • Implementation: asynchronous replication from a leader, or leaderless replication with anti-entropy (read repair, background Merkle-tree sync). Concurrent writes need a conflict rule: last-writer-wins, version vectors with application merge, or CRDTs.

After a write completes, every later read sees it. Usually this means linearizability (below).

  • Example: account balances, inventory reservation, uniqueness constraints (usernames), leader election and locks.
  • Implementation: a single leader that handles all reads and writes for a key, synchronous replication to a quorum, or consensus (Raft, Paxos).
sequenceDiagram
  participant U as User
  participant L as Leader
  participant R as Replica
  U->>L: write name = "Ann"
  L-->>U: OK
  L--)R: replicate name = "Ann" (async, delayed)
  U->>R: read name
  R-->>U: "Anna" (old value)
  Note over U,R: Stale read: the user does not see their own write
  R->>R: apply replicated write
  U->>R: read name
  R-->>U: "Ann"

The write succeeded and nothing is broken. The user still sees their change “disappear”, which feels like a bug. Most of the models below exist to remove exactly this kind of surprise without paying for full strong consistency.

Roughly from strongest to weakest (the order is partial: causal consistency and bounded staleness, for example, are not directly comparable):

ModelGuaranteeWhere it mattersTypical implementation
Strict serializabilityTransactions appear in one order that respects real timeMulti-row money movement, global uniquenessConsensus plus synchronised clocks (Spanner’s TrueTime) or a single sequencer
LinearizabilitySingle-object operations appear atomic and in real-time orderLocks, leader election, compare-and-set, configLeader with quorum commit (Raft, Paxos) and leader or quorum reads
SequentialEveryone sees the same order of operations, and each client’s operations in program order, but possibly lagging real timeReplicated logs, coordination servicesTotal-order broadcast; reads from a lagging but ordered replica
CausalIf A could have influenced B, everyone sees A before B; unrelated writes may appear in any orderComments and replies, chat, collaborative editingDependency tracking with vector clocks or causal tokens
Bounded stalenessReads are at most K versions or T seconds behindDashboards, leaderboards, geo-replicated readsReplica tracks its lag; serves only if within bound, else redirects or blocks
Read-your-writesA client always sees its own writesProfile edits, posting a comment, settingsSticky routing, read from leader after a write, or wait for replica to reach the client’s last write position
Monotonic readsA client never sees data go backwardsPagination, timelines, inboxesStick a client to one replica, or carry a “read at least version V” token
EventualReplicas converge if writes stopCounters, catalogues, DNS, cachesAsync replication, anti-entropy, conflict resolution

The system behaves as if there were one copy of each object, and every operation takes effect at one instant between its start and its end. Once any client has seen a new value, no client can later see the old one.

  • Why it matters: “check then act” logic is only safe on a linearizable store. Examples: acquiring a lock, claiming a username, decrementing stock.
  • How: route all operations for a key through a leader that commits with a majority (etcd, Spanner, CockroachDB). Reads must also be linearizable: they go through the leader with a valid lease, or confirm with a quorum. Otherwise a deposed leader can serve stale data.
  • Cost: at least one quorum round trip per write. The minority side of a partition cannot serve requests.

Do not confuse it with serializability, an isolation level for transactions. Serializability says transactions appear in some serial order, not necessarily the real-time one. Strict serializability combines both. Spanner’s “external consistency” is this combination.

All clients observe the same order of operations, and each client’s own operations appear in the order it issued them. Unlike linearizability, that order may lag behind real time. A client can read a value that was already overwritten elsewhere, as long as everyone sees the overwrite in the same position later. ZooKeeper is close to this model. Writes go through a leader in one global order, and each client’s updates are applied in the order sent. A follower may serve a read that is a little behind; sync() forces it to catch up first.

Writes that are causally related are seen in the same order by everyone. A reply is never visible before the post it answers. Concurrent, unrelated writes may appear in different orders on different replicas.

  • Why it matters: it rules out the anomalies people notice: answers before questions, a “permission removed” followed by a photo that was meant to be hidden.
  • How: each write carries its dependencies, as a vector clock or a token naming the versions the writer had seen. A replica delays making a write visible until its dependencies are visible. MongoDB’s causally consistent sessions do this with cluster-time tokens. They need majority read and write concerns to get all guarantees.
  • Cost: metadata per write, and some waiting for dependencies. Causal consistency is about the strongest model that can stay available during a partition, provided each client keeps talking to the same replicas.

Session guarantees: read-your-writes and monotonic reads

Section titled “Session guarantees: read-your-writes and monotonic reads”

These are guarantees for one client’s session, and they are cheap.

  • Read-your-writes: after you post a comment, you see it, even if other users see it a second later. Implementations:
    • route the user’s reads to the leader for a short window after they write;
    • pin the session to one replica (sticky routing);
    • return the write’s log position (an LSN or version) to the client and have replicas wait until they have applied it before answering.
  • Monotonic reads: if you saw version 5, you never then see version 4. Refreshing a page must not make a new message vanish. The usual fix is to keep the client on the same replica, or send an “at least version V” token with each read.
  • Related guarantees: monotonic writes (a client’s writes apply in order) and writes-follow-reads (a write is ordered after the data it was based on). Azure Cosmos DB’s default Session level gives a client read-your-writes and writes-follow-reads through a session token it sends with each request.

Reads may be stale, but by no more than a stated amount: K versions or T seconds. Examples: Cosmos DB’s bounded staleness level, Spanner’s bounded-staleness reads, CockroachDB’s with_max_staleness() follower reads.

  • Why it matters: it turns “eventually” into a number you can reason about and put in an SLO. A leaderboard that is at most 5 s old is fine. One that is “eventually” correct may be hours behind during an incident.
  • How: replicas track how far behind they are (replication timestamps, closed timestamps). A replica outside the bound redirects the read to a fresher replica or blocks it. Cosmos DB instead throttles writes when a Region falls too far behind.

When several replicas accept writes independently (multi-leader, offline-first apps, multi-Region active-active), concurrent updates must be merged. Last-writer-wins is simple but silently drops one of the writes. Version vectors detect concurrent versions and hand them to the application to merge; Amazon’s original Dynamo shopping cart worked this way. CRDTs (conflict-free replicated data types) are data structures whose merge is mathematically guaranteed to converge regardless of delivery order. Examples are grow-only counters, observed-remove sets and sequence types for collaborative text. They give strong eventual consistency without coordination. The price is restricted operations and metadata overhead.

ModelLatency costAvailable in a partition?Application burden
Linearizable / strict serializableQuorum round trip per write (often per read); cross-Region if replicas span RegionsNo, on the minority sideLowest: behaves like one copy
SequentialQuorum on writes; local readsNo, for writesLow; reads may lag
CausalSmall metadata; occasional wait for dependenciesYes, if the client sticks to its replicasModerate: concurrent writes still conflict
Session guaranteesNear zero (routing or tokens)Yes, while the session’s replica is reachableLow for the user’s own view
Bounded stalenessLocal reads while within boundDegrades to blocking or redirecting when the bound is exceededMust accept K versions or T seconds of staleness
EventualLowestYesHighest: stale reads, conflict resolution

Based on The System Design Primer by Donne Martin (CC BY 4.0). Restructured, rewritten and updated; site content is licensed under CC BY 4.0.