Three clients share one value stored on three replicas. We ran 200 seeded sessions through a linearizability checker. Reads from the nearest copy broke the one-copy promise in 43. Majority reads still broke it in 5. Majority reads that write the value back broke it in none, at 2.7 times the read time.
Pick how reads work and replay the 200 sessions. Then we build each part from zero: the promise, the checker, quorums, CAP, latency, logical clocks, consensus and coordination services.
You need a little Python and what a replica is. We build the rest from zero.
Illustrative and seeded. 3 replicas; each session has 3 clients doing 4 operations each, 70% reads. A message takes 0.1 ms to the client's own replica or 0.5 ms to another, plus random jitter; 30% of messages straggle by about 6 ms more. Writes use two majority rounds in every mode, so only the read path differs.
Chapter 0
Read from the nearest copy and watch a finished write disappear
A theatre sells seats through an app. The seat map lives on three replicas, so one crash loses nothing. Ana buys seat 14 and the app says "booked". A moment later Ben opens the map, his phone asks the nearest replica, and seat 14 shows as free.
Nothing crashed. Ben's replica had not yet received Ana's write. This delay is replication lag. Each replica is correct about what it has seen, but together they act like several copies, each with its own past.
Each lane is one client. Blue bars are writes, teal bars are reads, each labelled with its value. A red read returned an old value after a newer write had finished. Step through the broken sessions.
Illustrative and seeded, the hero's model with reads from the client's own replica. Times in milliseconds.
The promise Ben needed has a name. A linearizable system acts as if there is one copy of the data. Each operation takes effect at one instant between its start and its end. Once a write finishes, every read that starts later must see it, or something newer.
In the hero's 200 sessions, nearest-copy reads broke that promise in 43. That is one session in five, with only 12 operations each.
This lesson follows Chapter 10 of Designing Data-Intensive Applications, 2nd edition, by Martin Kleppmann and Chris Riccomini (O'Reilly, 2026). We explain its ideas in our own words, with our own examples and simulations. Its questions: what does "acts as one copy" promise, what does it cost, and why is agreement among nodes one problem with many faces? Failure Detection and Time, Consensus and Replication and Coordination Avoidance simulate the clocks, the Raft cluster and the CRDTs. We link to them and keep to the book's angle.
Failure. Symptom: a user completes an action, refreshes, and sees the old state. Measure: reads that return a version older than the newest acknowledged write. Cause: reads served by a replica that lags. Fix: decide which reads need the one-copy promise, and send only those down a linearizable path (Chapter 3).
Chapter 1
Drag one instant into each operation and find out whether any order explains the results
Herlihy and Wing defined linearizability in 1990. Every operation has an invocation, when the client sends it, and a response, when the answer arrives. Between the two, the operation must take effect at one instant, its linearization point.
Replay the operations, in the order of their points, on one plain variable. Every read must return what the replay says. If no choice of points works, the system did something no single copy could do.
Drag each green dot along its bar. The order of the dots is replayed on one variable that starts at 0. Pick a history, then try to make every read come out right.
Three small histories with made-up times. "Search" runs the checker of Chapter 2 and moves the dots to an order it found, if one exists.
One rule does the work. Name each operation's invocation time inv and its response time res. Call two operations a and b.
a comes before b, if res of a is less than inv of b.
If operation a finished before operation b started, a's point goes first. If the two overlap, either order is allowed. Search the allowed orders for one where every read returns the value of the last write before it.
With made-up numbers, the "New, then old" history: the write of 1 runs from 0 to 6. Read B runs from 1 to 2 and returns 1. Read C runs from 3 to 4 and returns 0. res(B) = 2 < inv(C) = 3, so B comes before C. B saw 1, so the write's point is before 2. Then C, after 3, must see 1. It saw 0, so no order works.
Where does this matter? A lock or a leader election: two nodes must not both win. A unique username: two sign-ups must not both get "ada". Two channels: a service stores photo 7, then queues "resize photo 7", and the resizer reads a lagging copy and finds nothing.
Linearizability is not serializability. Serializability, from Chapter 8, says transactions over many objects act as if run one at a time, in some order, which may ignore real time. Linearizability is about one object and does respect real time. A database that gives both is strictly serializable.
Failure. Symptom: two workers both believe they hold the same lock. Measure: overlapping holds in the lock service's history. Cause: the lock was read from a store that is not linearizable. Fix: take locks only from a linearizable store, and fence the resource (fencing tokens).
Chapter 2
Run a brute-force checker on recorded histories, then switch off real time and see what slips through
Testers check real databases this way. Run clients against the system, record every operation's invocation, response and result, then search for a legal order. Jepsen does this with its checker Knossos. Porcupine reports that it runs 1,000 to 10,000 times faster than Knossos on Jepsen's histories.
The search is the one from Chapter 1, done by a program. Pick any operation that no remaining operation must precede. Apply it to the variable. If it is a read with the wrong value, back up. Gibbons and Korach proved in 1997 that this problem is NP-complete in general, so the worst case grows fast.
A seeded session from the hero's model, read from the nearest copy. Green dots show the order the checker found. Press next, then switch real time off.
Illustrative and seeded: 3 clients, 4 operations each. The checker is the Wing and Gong search with a memo, the idea behind Lowe's checker and Porcupine. Without real time it keeps only each client's own order: sequential consistency for one variable.
Why is it fast here? A memo. The checker's position is the set of operations already placed plus the variable's current value. Two paths that reach the same position need only one search. Call the number of operations n, the number of values the variable can hold V, and the number of positions the checker can visit S.
S is at most two to the n, times V.
Each position is a set of placed operations and a value. There are two to the n sets and V values, so S is at most their product. Trying every order instead costs n factorial.
Seeded session 1 has n = 12 operations and 4 writes, so V = 5. S ≤ 4,096 × 5 = 20,480 positions, against 12! = 479,001,600 orders. The checker visited 38. Over the 200 sessions it visited 20 on average and 58 at most.
Now switch real time off. The checker then keeps only each client's own order. That is sequential consistency: everyone agrees on one order, but it may lag behind real time. Of the 43 broken sessions, 19 pass this weaker test; only the clock on the wall shows their stale read.
Failure. Symptom: a test suite checks that all replicas end with the same value, passes, and the bug ships. Measure: run a history checker, not a final-state check. Cause: the copies converged, but a read in the middle returned an old value. Fix: record invocation, response and result for every operation, and check the whole history.
Chapter 3
Read from two of three replicas while a write is still arriving, then make the reader write back
The usual fix for lag is a quorum. A write waits for W replicas. A read asks R replicas and keeps the newest version it hears. Quorums Without a Leader shows why the sets must overlap. Call the number of replicas N.
R plus W is greater than N.
If R plus W is more than N, any R replicas and any W replicas share at least one. So a read always reaches a replica that holds every acknowledged write.
N = 3, W = 2, R = 2: 2 + 2 = 4 > 3, so the sets share at least 4 − 3 = 1 replica. With R = 1 and W = 1, 1 + 1 = 2 is not more than 3, and the seeded showcase broke 97 of 200 sessions.
The rule covers acknowledged writes. It says nothing about a write still on its way. Suppose a write reached one replica and not yet the other two. Reader A asks two replicas, one of them the new one, and returns the new value. Reader B starts after A finishes, asks the two old replicas, and returns the old value. That is "new, then old" from Chapter 1.
Three replica lanes and two readers. The write of 1 reaches replica 1 fast and the others late. Step through, then turn on write-back and step again.
A scripted example with made-up times. The counts in the last step come from the hero's seeded model.
The fix is from Attiya, Bar-Noy and Dolev (1995), often called ABD. Before it answers, a reader writes the value it found to a write quorum. Once A returns 1, a majority holds 1, so B must see it. In the seeded model, majority reads broke 5 of 200 sessions; with write-back, none. The median read rose from 2.13 ms to 5.77 ms, because a read is now two round trips.
| How copies are kept | Linearizable? | Why |
|---|---|---|
| Single leader, reads at the leader | Can be | Only if the leader checks it is still the leader (read paths) |
| Consensus (Raft, Paxos, Zab) | Yes, if reads go through it | One agreed log orders every write |
| Multi-leader | No | Two leaders accept conflicting writes at once |
| Leaderless quorums | Probably not | In-flight writes and last-write-wins clocks break it; write-back fixes reads only |
Failure. Symptom: a dashboard shows a new price, and a refresh one second later shows the old one. Measure: pairs of reads where the later one returns an older version. Cause: majority reads during a slow write, with no write-back. Fix: write back before answering, or read through a leader that checks its lease.
Chapter 4
Split two sites for a few minutes and count what each choice costs: refused requests or stale answers
Five replicas sit in two sites: three in site A, two in site B. The link between the sites fails for six minutes. Clients in site B can still reach their two replicas. What should those replicas do?
They can keep answering from their own copy. Then a read in B misses writes made in A, and writes in B clash with A's. Or they can refuse, because two of five is not a majority. Then B's clients get errors until the link heals. There is no third choice.
Each dot is a request over one hour; the lower band is site B. During the yellow cut, pink dots were refused and red dots got a stale answer or a clashing write. Pick a choice, then move the cut.
Illustrative and seeded: 6,000 requests spread evenly over 60 minutes, 20% of them writes, all on one value. The cut starts at minute 20. Site A holds the majority and keeps working either way.
Gilbert and Lynch proved this in 2002 as the CAP theorem. Their C is linearizability. Their A says every request that reaches a working node gets an answer. With a partition, no system has both. Call the share of requests from the minority site s, the cut's length Tp, the period T, and the share of requests refused U.
U equals s, times T-p over T.
Requests arrive evenly, so the cut covers T-p over T of them. Of those, the share s come from the minority site, and those are refused. Multiply the two shares.
With made-up numbers: s = 40%, T-p = 6 minutes, T = 60 minutes. U = 0.4 × 6/60 = 4.0%. The seeded hour refused 242 of 6,000 requests: 4.03%. Choosing to stay available instead gave 197 stale reads and 38 writes that clash with site A's.
Brewer, who stated the conjecture in 2000, wrote in 2012 that "2 of 3" was always misleading. Partitions are rare, so there is little reason to give up C or A when the network is whole. CAP is also narrow: one consistency model, one kind of fault. It says nothing about delay, crashed nodes or the cost of a read on a quiet day. That cost is the next chapter.
Failure. Symptom: after a link heals, two sites hold different values for the same record. Measure: writes accepted on the minority side during the cut. Cause: the store chose availability, perhaps by default. Fix: choose per operation. Bookings and locks refuse in the minority; a shopping cart may answer and merge later.
Chapter 5
Place replicas across regions and see what a linearizable read costs on a day with no faults
A local copy answers in about a millisecond. A linearizable read cannot trust a local copy, because the copy may lag. It must hear from a majority, or from a leader that has just heard from one. So distance sets its time, every day, with or without a partition.
Attiya and Welch proved a floor in 1994. Call the uncertainty in message delay u: the gap between the slowest and fastest possible delivery. In any linearizable read/write register, some read takes at least u/4 and some write at least u/2. A sequentially consistent register can make all reads, or all writes, instant. Linearizability costs time for a reason deeper than any one algorithm.
Bars are round trips from the client to each replica, shortest first. The dashed line marks the majority reply: a linearizable read waits for it. Move the client.
Illustrative round-trip times between regions, rounded, with 1 ms inside a region. Not measurements of any provider.
Call the round trip from the client to each replica its RTT, sort them from shortest to longest, and call the majority size q. Call the time of a linearizable majority read tlin.
t-lin equals RTT sub q, the q-th smallest round trip.
Ask every replica at once. Sort the round trips. The read can answer when the q-th reply arrives, so its time is the q-th smallest round trip.
Five regions, client in Ireland: round trips 1, 75, 140, 185 and 220 ms. q = 3, so t-lin = 140 ms. The local copy takes 1 ms. With three regions it is 75 ms. A client in Tokyo with three regions has no local copy: 105, 150, 220, so t-lin = 150 ms.
This is why many systems drop linearizability. They do it for speed on every request, not for partitions. They offer it per operation instead: etcd reads are linearizable by default, and a "serializable" read option trades that for a faster, possibly stale answer.
Failure. Symptom: page loads in one region are 100 ms slower than in another, with no errors. Measure: which calls use a linearizable read, and where its majority sits. Cause: a strong read on a hot path, far from most replicas. Fix: keep strong reads for the few operations that need them; place a majority near the users who need them.
Chapter 6
Let two nodes claim one username with Lamport timestamps and see when each can know it won
Many systems need IDs that come out in order, such as log positions or versions. Wall clocks fail at this, as Chapter 9 showed. A logical clock counts events instead of seconds. Lamport clocks, vector clocks and hybrid logical clocks each get a simulation there.
Each node keeps a counter L. It adds one for each event and stamps every message it sends. A message carries its sender's stamp Lmsg.
L becomes the max of L and L-msg, plus one.
When a message arrives, take the bigger of your counter L and the message's stamp L-msg, and add one. So anything you do after hearing about an event gets a bigger stamp than that event. Ties between nodes break by node name.
With made-up numbers: node B is at L = 4 and receives a message stamped L-msg = 5. B's counter becomes max(4, 5) + 1 = 6. The stamp pairs (5, A) and (5, B) tie on the number, so A's comes first.
This gives a total order that never puts an effect before its cause. Is that enough for a unique username? Nodes A and B both receive a sign-up for "ada" at the same moment, and both stamp it (5, A) and (5, B). A's is smaller, so A should win.
Three node lanes on a time line. Pink claims wait for a verdict; green ones are final. Step through, then take node C down, then try one counter service.
A scripted example with made-up one-way delays: A to B 40 ms, any node to C 90 ms, any node to the counter 5 ms.
Each node learns the order only after the fact. A node cannot answer "you got the name" until it has heard from every other node, since any of them might hold a smaller stamp. In the example, A waits 90 ms for C. If C is down, nobody can ever answer. Lamport order is right, and it is not enough.
What works is a linearizable ID generator: one counter that hands out numbers in real-time order. Ask it, and the first claim wins in one round trip. But one counter is a single point of failure. Make it survive crashes, and you have built consensus, the next chapter.
Failure. Symptom: two accounts share one username after a burst of sign-ups. Measure: duplicate names whose sign-ups overlapped in time on different nodes. Cause: each node checked its own copy and its own counter. Fix: claim the name through one linearizable operation, such as a compare-and-set on a consensus store.
Chapter 7
Build a log from compare-and-set, a lock from a log and a commit from consensus, and see they are one problem
In consensus, several nodes each propose a value and all must decide one. Three properties define it. Agreement: no two nodes decide differently. Validity: the decided value was proposed by someone. Termination: every node that does not crash decides.
Many problems turn out to be the same one. A compare-and-set (CAS) register sets a value only if it still holds what you expect. The first proposer to CAS from "empty" wins, and everyone reads the winner. A shared log, also called total order broadcast, delivers the same entries in the same order to every node; the first entry is the decision. Each can build the other, as can a lock and a unique-ID service. Chandra and Toueg showed in 1996 that, with crash failures, total order broadcast and consensus are equivalent.
Three clients race. Purple boxes are log slots or the decided value. Pick a construction and step through it.
Seeded toy with three clients. In the CAS log, each CAS is one linearizable operation on one slot, and a loser retries at the next free slot.
Racing has a cost. Call the number of clients that want to append at once c, and the CAS attempts it takes until all are in the log A.
A equals c times c plus one, over two.
All c clients try the first free slot and one wins. The other c minus one try the next slot, and so on down to one. Add c, then c minus one, down to one.
c = 3: 3 + 2 + 1 = 6 attempts for 3 entries. c = 5: 15 attempts. With one leader that orders all requests, 5 entries take 5 appends.
That is why practical consensus algorithms elect a leader: one proposer, no race. Raft, Zab and Multi-Paxos all do, and Electing One Leader shows how. Atomic commit fits too. Let consensus decide "commit" or "abort", and let any "no" vote force abort. Then a coordinator crash no longer blocks everyone, as it does in plain two-phase commit (When the Coordinator Dies).
Termination has a catch. Fischer, Lynch and Paterson proved in 1985 that with no bound on message delay and one possible crash, no deterministic algorithm can guarantee a decision. Real algorithms keep agreement always and decide once delays settle (system models).
Failure. Symptom: a home-made lock, built on a key-value store's "set if absent", grants the same lock twice after a failover. Measure: whether that store's CAS stays linearizable across leader changes. Cause: an asynchronously replicated leader lost the first CAS. Fix: use a store whose CAS goes through consensus.
Chapter 8
Hand out shards through leased keys, stop a worker, and time how long its shards sit without an owner
Few teams write consensus code. They run a coordination service: ZooKeeper, which uses its own protocol Zab, or etcd, which uses Raft. Both keep a small amount of critical data on three or five nodes and order every change. ZooKeeper stamps each change with a zxid; etcd gives each change a revision that only grows.
A key bound to a lease (etcd) or an ephemeral node (ZooKeeper) vanishes when its owner's session ends. So a crash shows up as a deleted key. A watch tells clients when a key changes. A CAS or a sequential node elects a leader. The zxid or revision serves as a fencing token.
Four worker lanes over 60 s; yellow ticks are lease renewals. At 30 s, worker 3 stops. Pink is time its shards have no owner. Move the lease TTL, then try a pause instead of a crash.
Illustrative: each worker renews every TTL/3; the leader's watch fires 50 ms after the key is deleted. 16 shards, 4 each.
When does a dead worker's work move? Call the lease's time to live TTL, the watch delay d, and the time from the stop to reassignment Tre.
T-re is at most TTL plus d.
The last renewal came at or before the stop. The key expires one TTL after that renewal, and the leader acts d later. So the gap is at most TTL plus d.
With made-up numbers: TTL = 10 s, d = 0.05 s. A worker that renewed at 30 s and crashed right after leaves its 4 shards with no owner for up to 10.05 s. A 2 s TTL cuts that to 2.05 s, but then a 6 s pause also loses the lease.
So TTL is a trade: short means fast recovery but leases lost in pauses. A worker that wakes without its lease must stop, and the storage must refuse its token.
These stores are for small, critical data: who leads, who owns which shard, the configuration, and which instances are alive (service discovery). etcd caps a request at 1.5 MiB by default. Reads differ. etcd reads are linearizable unless you ask for "serializable". ZooKeeper answers reads from the server you are connected to, which may lag, unless you call sync first.
Failure. Symptom: after a long garbage collection, a worker and its replacement both process shard 7. Measure: the worker's pause against the lease TTL. Cause: the worker kept working after its key expired. Fix: check the lease before acting, and fence writes with the key's revision.
Chapter 9
Turn every knob of the hero's register and see which settings keep the one-copy promise, and at what price
Back to the hero, with every knob: replicas, both quorum sizes, the read path, stragglers and clients. Every setting replays the same 200 seeded sessions through the checker.
Each tile is one session: green passed the checker, red did not. Bars give the median read and write time. Try R = W = 1, then write-back, then N = 5.
Illustrative and seeded, the hero's engine. Quorums cannot exceed N. Writes ask R replicas for the newest version, then store on W, in every mode.
At the hero's settings, majority reads broke 5 sessions and write-back broke none. Set R = W = 1 and the quorums no longer overlap. 97 sessions break with plain reads. 72 break even with write-back, because the next reader may miss the replica written back to. W = 3 with R = 1 still broke 26: every replica gets the write, but a read can catch it half-arrived. With five replicas and majorities of 3, plain majority reads broke none of the 200. That shows the window is narrow, not closed.
Stragglers set the price. With none, write-back reads take a median 2.73 ms. At 50% they take 10.6 ms, and plain majority reads break 7 sessions.
Failure. Symptom: a store passes every test in the lab and returns old values in production. Measure: the delay tail between replicas in each place. Cause: the lab network has no stragglers, so the in-flight window never opens. Fix: test with injected delay, and check recorded histories.
Chapter 10
Keep the numbers, the rules and the code for the next system you design
Linearizability promises one copy: each operation takes effect at one instant between its start and end, so no later read undoes a finished write. A search of recorded histories checks it. Lag and in-flight writes break it; majority reads need a write-back. It costs availability during a partition and a round trip to a majority on every quiet day.
Logical clocks order events but cannot enforce uniqueness without hearing from everyone. That gap is consensus, which also appears as a CAS register, a shared log, a lock and an atomic commit. Coordination services package it for the small data that must be right.
| Idea | Rule | This lesson's number |
|---|---|---|
| One copy | one point per operation, between inv and res | nearest-copy reads: 43 of 200 sessions broke |
| Real-time order | a → b if res(a) < inv(b) | 19 of the 43 were still sequentially consistent |
| Checker work | S ≤ 2n · V | 12 ops: 20,480 positions against 12! orders; 38 visited |
| Quorum overlap | R + W > N | majority reads 5 of 200 broke; write-back 0, read 2.13 → 5.77 ms |
| CAP | U = s · Tp/T | 40%, 6 min: 4.0% refused, or 197 stale reads |
| Latency | tlin = RTT(q) | Ireland, 5 regions: 140 ms against 1 ms |
| Attiya and Welch | read ≥ u/4, write ≥ u/2 | u = delay uncertainty |
| Lamport clock | L ← max(L, Lmsg) + 1 | max(4, 5) + 1 = 6 |
| CAS race | A = c(c + 1)/2 | 3 clients: 6 attempts; a leader: 3 |
| Lease | Tre ≤ TTL + d | 10 s TTL: up to 10.05 s with no owner |
pythondef linearizable(ops, real_time=True): # ops: (client, kind, value, inv, res); kind 'w' or 'r'; the register starts at 0 n, seen = len(ops), set() def may_go(done, i): for j, p in enumerate(ops): if j == i or done >> j & 1: continue if (p[4] < ops[i][3]) if real_time else (p[0] == ops[i][0] and p[3] < ops[i][3]): return False # p must be placed first return True def search(done, val): if done == (1 << n) - 1: return True if (done, val) in seen: return False seen.add((done, val)) for i, (c, kind, v, inv, res) in enumerate(ops): if done >> i & 1 or (kind == 'r' and v != val) or not may_go(done, i): continue if search(done | 1 << i, v if kind == 'w' else val): return True return False return search(0, 0) new_then_old = [(0, 'w', 1, 0, 6), (1, 'r', 1, 1, 2), (2, 'r', 0, 3, 4)] print(linearizable(new_then_old), linearizable(new_then_old, real_time=False)) # False True def refused_share(s, t_cut, t_total): return s * t_cut / t_total # CAP, minority refuses def lin_read_ms(rtts): return sorted(rtts)[len(rtts) // 2] # the q-th smallest round trip print(round(refused_share(0.4, 6, 60), 3), lin_read_ms([1, 75, 140, 185, 220])) # 0.04 140
Now press Present or Teach and explain four things from memory. Why is "new, then old" impossible for one copy? Why does a majority read need a write-back? What does a partition force you to give up, and what does a quiet day still cost? Why does a unique username need consensus? Then go back to the hero and find the cheapest read path that never breaks.