14. Replication
14.10 How does leaderless replication work, and what are quorums?
At the bank: No head office at all. To update a notice, you give it to at least two of three branches. To read it, you ask at least two of three and trust the newest version you're shown. Because any two branches out of three always share at least one, you're always shown the latest notice.
In the system: In leaderless replication (the style of Amazon's Dynamo paper, used by Cassandra and Riak), clients write to and read from several replicas directly, with no leader:
- N = how many replicas hold each piece of data.
- W = how many must confirm a write.
- R = how many are asked on a read; the reader keeps the highest version.
Figure: with W + R > N, every read overlaps the latest write in at least one replica.
The rule: if W + R > N, every read set overlaps every write set in at least one replica, so a read sees the latest written version (barring some edge cases, like concurrent writes and replicas being replaced). In the chapter's code with N = 3: after a write with W = 2, a read with R = 2 returns the new value even when the stale replica answers first; with R = 1 it can return the old value.
| Setting (N = 3) | Good for | Cost |
|---|---|---|
| W = 2, R = 2 | Balanced; survives 1 node down for both reads and writes | Every request waits for 2 |
| W = 3, R = 1 | Fast reads | Any node down blocks writes |
| W = 1, R = 1 | Fastest, always available | Reads can be stale (W + R = 2, not > 3) |
How stale replicas catch up:
- Read repair: a reader that notices an old version writes the new one back.
- Anti-entropy: a background process compares replicas (often with Merkle trees of hashes) and copies missing data.
- Hinted handoff: if a replica is down, another node holds its writes and hands them over when it returns. (With "sloppy" quorums that accept writes on stand-in nodes, the overlap guarantee weakens.)
Cassandra exposes this per query as consistency levels like ONE, QUORUM and ALL.
Memory code: "Write to most, read from most, and the newest version wins." W + R > N.