Module 5 · Distributed systemsLesson 46 of 888 minAdvanced

Consistency models

A consistency model is a promise about what reads can return when data has several copies.

Try it

Read from replicas and watch "You" sometimes read data older than what you just wrote. Turn on "Read your own writes" and compare your stale reads with your friend's.

With one copy of data, a read returns the last write. With several copies (replicas, caches, regions), different readers can see different versions. Consistency models describe which surprises are allowed.

From strongest to weakest

Linearizable (strong) consistency: the system behaves as if there's one copy. Once a write is done, every later read anywhere sees it. Easy to reason about; costs coordination and latency.

Sequential consistency: everyone sees operations in the same order, but not necessarily in real time.

Causal consistency: things that depend on each other are seen in order. You never see a reply before the message it replies to. Unrelated events may appear in different orders for different people.

Session guarantees, practical promises for one user:

  • Read your writes: you always see your own changes.
  • Monotonic reads: you never see time go backwards (newer data, then older).
  • Consistent prefix: you see events in the order they happened, possibly behind.

Eventual consistency: if writes stop, all copies will eventually agree. Until then, anything goes.

How systems provide them

  • Read-your-writes: send a user's reads to the primary for a short time after they write, or remember the version they wrote and wait until a replica has it.
  • Monotonic reads: keep each user on the same replica.
  • Causal: attach version vectors or dependency info to writes.
  • Linearizable: route everything through a leader, or use a consensus protocol.

Choosing

Ask what would confuse or harm a user. Bank balances and stock counts usually need strong guarantees. A like count can be eventual. A user's own profile edit should at least be read-your-writes. Many systems mix models: strong for the critical path, weaker for everything else.

Quick check

A user posts a comment, refreshes, and it's missing, though everyone eventually sees it. Which guarantee is broken?