$ man consistency # what can a read return?

Consistency vs Eventual Consistency

A consistency model is a promise about what a read can return when data is replicated. Strong consistency (linearizability) promises that once a write is confirmed, every later read, on any replica, sees it: the system behaves like a single copy. Eventual consistency only promises that if writes stop, all replicas will eventually agree; until then, a read may return an older value.

The trade is simple: strong consistency needs replicas to coordinate on every operation, which costs latency and availability. Eventual consistency lets each replica answer on its own, which is fast and survives partitions, but pushes stale reads and conflicts onto the application. See CAP and PACELC for the theory.

Quorums: tuning between the two

Leaderless (Dynamo-style) databases such as Cassandra, ScyllaDB and Riak let you choose, per request, how many replicas must answer. With N replicas, a write waits for W acknowledgements and a read asks R replicas.

$ cqlsh -e 'CONSISTENCY QUORUM'
N = 3 replicas: a stale read, a quorum read, and repair
WRITER READER REPLICA A cart = v1 REPLICA B cart = v1 REPLICA C cart = v1 N = 3 write v2 cart = v2 cart = v2 W = 2: wait for A and B to ack R = 1 → v1 (stale) R = 1: fast, but may miss the write R = 2 → {v2, v1} → newest: v2 R + W > N 2 + 2 > 3, so read and write sets always overlap REPLICA C cart = v2 read repair: C gets v2, all agree
  1. Three replicas each hold version v1 of a shopping cart.
  2. A write of v2 with W = 2 succeeds once A and B confirm. The copy to C is dropped.
  3. A read with R = 1 happens to ask C and returns v1. Fast, available, and stale.
  4. A read with R = 2 must ask two replicas. Any two overlap with the two that took the write, so it sees v2 and returns the newest. That is the R + W > N rule.
  5. The coordinator notices C is behind and writes v2 back (read repair). Background anti-entropy does the same for data nobody reads. The replicas have eventually converged.

Quorums make stale reads much less likely, but on their own they are not full linearizability: sloppy quorums, concurrent writes and clock-based conflict resolution can still surprise you. Systems that need a hard guarantee use consensus (Raft, Paxos) instead.

The spectrum of consistency models

Model Promise Example
Linearizable (strong) Behaves like one copy; reads see the latest confirmed write etcd, ZooKeeper writes, Spanner, DynamoDB strongly consistent reads
Sequential Everyone sees operations in the same order, not necessarily real-time order ZooKeeper reads
Causal If B depends on A (a reply to a comment), nobody sees B without A MongoDB causal sessions, many CRDT systems
Read-your-writes You always see your own updates Session guarantees, sticky routing to the leader
Monotonic reads You never see time go backwards Pinning a user to one replica
Eventual Replicas converge if writes stop; no promise about when DNS, Cassandra at ONE, async read replicas

Stronger models are easier to program against and slower to run. Many systems mix them: strong for money and inventory, eventual for likes, view counts and recommendations.

When replicas disagree: resolving conflicts

Eventually consistent systems that accept writes on several replicas must merge concurrent updates:

  • Last-write-wins (LWW): keep the value with the highest timestamp. Simple, but silently drops writes, and clock skew can make the “last” write the older one. See Clock.
  • Version vectors: detect that two writes were concurrent and keep both versions (siblings) for the application to merge.
  • CRDTs (conflict-free replicated data types): counters, sets and maps designed so that any merge order gives the same result. Used in Riak, Redis Enterprise active-active, and collaborative editors.

Real settings you will meet

  • Amazon S3: strong read-after-write consistency for all objects since December 2020. Older “S3 is eventually consistent” advice is outdated.
  • DynamoDB: eventually consistent reads by default; ConsistentRead=true for strong reads at twice the read cost (not available on global secondary indexes).
  • Cassandra: consistency level per query (ONE, QUORUM, LOCAL_QUORUM, ALL).
  • PostgreSQL / MySQL read replicas: the primary is strong; async replicas are eventually consistent. See Replication.
  • Data pipelines: a CDC stream, a search index or a cache fed from a database is an eventually consistent copy of it.

Common mistakes

  • “Eventually” with no bound. Measure replication lag and set an alert; “eventually” in production should mean milliseconds or seconds.
  • Reading your own write from a replica. Use leader reads or session tokens for read-after-write flows.
  • Relying on LWW for data you cannot lose, such as counters or carts. Use CRDTs or conditional writes.
  • Choosing one model for the whole system. Decide per entity and per query.

Learning Resources

Articles and papers

Books

  • Designing Data-Intensive Applications (2nd ed.), Martin Kleppmann and Chris Riccomini: chapters on replication and consistency

Videos