consistency models
linearizable, serializable, causal, eventual. a map of what distributed databases actually promise.
- address
- everything.scale.consistency_models
- part
- part 17, scale and distribution
- chapter
- chapter 7 of 10
- reading time
- 8 min
a consistency model says which values a read is allowed to return when data has several copies. from strong to weak: linearizable (it behaves like one copy, in real time), sequential (one agreed order, maybe behind real time), causal (causes before effects), the session guarantees (read your writes, monotonic reads), and eventual (copies agree once writes stop). isolation levels answer a different question, about transactions, and the two get confused all the time.
the picture
you know how "consistent" means four different things depending on who's talking? in ACID it's constraints, in CAP it's linearizability, in a cloud database's docs it might mean "eventually". a consistency model pins it down by saying which histories are allowed: which values reads may return, given the writes and when everything happened.
think of a group chat. linearizable: everyone sees every message the instant it's sent, in the same order. sequential: everyone sees the same order, but your phone might be a few seconds behind. causal: a reply never shows up before the message it answers, but two unrelated messages can arrive in either order on different phones. eventual: messages can arrive in any order and late, but once everyone stops typing, every phone shows the same chat.
here's a checker. each bar is one operation on a register x (it starts at 0), from the moment it was called to the moment it replied. pick a history, click a read to change what it returned, or select an operation and move it earlier or later, and see which models still allow it:
- ✗ linearizableno order fits both real time and every read: some read missed a write that had already finished.
- ✓ sequentialthere's an order that keeps each process's own order and fits every read.
- ✓ causalevery process sees causes before their effects.
- ✓ read your writesno process reads something older than its own last write.
- ✓ monotonic readsno process sees time go backwards.
- ~ eventualcan't be broken by a finite history: it only promises that replicas agree once writes stop.
the models, strongest first
linearizable (Herlihy and Wing, 1990). there's one order of all operations, every read returns the latest write before it in that order, and the order respects real time: if A finished before B started, A comes first. it's what a single copy would do. it's the C in CAP, and it costs coordination on every operation.
sequential (Lamport, 1979). there's still one order that every process agrees on, and each process's own operations appear in the order it ran them, but the order doesn't have to match the wall clock. a read can be stale, as long as everyone is stale in the same consistent way. the "stale read" history above is sequential but not linearizable.
causal. only operations that could have influenced each other have to be seen in order: a process's own earlier operations, and a write it read before writing. unrelated writes can show up in different orders for different readers. Mahajan, Alvisi and Dahlin showed in 2011 that a variant of it (real-time causal) is the strongest model a system can keep while staying available during a partition, which is why it keeps coming up.
read your writes, monotonic reads (two of Terry et al.'s session guarantees, 1994). promises to one client about its own session: it sees its own writes, and it never sees time go backwards. weak on their own, but exactly what users notice: "I saved it and it's gone" and "the comment disappeared when I refreshed". read replicas break both unless reads are routed with care.
eventual. if writes stop, all copies eventually return the same value. it says nothing about what reads return in the meantime, so no finite history can break it. that's why the checker shows it as a squiggle rather than a tick: it's a promise about convergence, not about any read.
P1: w(x, 1) ──ok
P2: r(x) → 0isolation is a different axis
isolation levels are about transactions: groups of reads and writes over many objects, running concurrently, and which interleavings are allowed (serializable: as if one at a time). consistency models are about single operations on copies and time. neither implies the other:
- linearizableone copy, real time: once a write finishes, everyone sees it
- sequentialone order everyone agrees on, but it may lag real time
- causalcauses before effects; unrelated writes in any order
- read your writes · monotonic readsper session: your own writes, and never backwards
- eventualcopies agree once writes stop
- serializablethe same result as some one-at-a-time order
- snapshot isolationpostgres's REPEATABLE READ
- read committedpostgres's default
- read uncommittedthe standard's floor; postgres runs it as read committed
- a serializable database can still return stale results if transactions are ordered in a way that ignores real time, for example by serving read-only transactions from a lagging copy. serializability only promises some serial order.
- a linearizable key-value store can still let two multi-key operations interleave badly, because it promises nothing about groups of operations.
- strict serializability asks for both: serializable transactions in an order consistent with real time. it's what spanner advertises, and what a single database server tends to give you for free, because there's one copy and one clock.
on one postgres server you rarely have to think about this axis: there's one copy of the data, and every new snapshot includes every transaction that committed before it was taken. the moment you add an asynchronous read replica, reads routed there drop down the consistency ladder, whatever the isolation level says.
| the read goes to | linearizable | read your writes | monotonic reads |
|---|---|---|---|
| the primary | yes | yes | yes |
| one async replica, always the same | no | no | yes |
| any of several async replicas | no | no | no |
| a replica, after waiting for the LSN | no | yes | yes |
keylines
- a consistency model says which values reads may return when data has copies.
- linearizable ⊃ sequential ⊃ causal ⊃ the session guarantees; eventual only promises convergence.
- the gap between linearizable and sequential is real time.
- isolation (transactions) and consistency (copies and time) are different axes.
- strict serializable = serializable + linearizable.