Book Deep Read · Database Internals · Chapter 14

Consensus

Database Internals · Ch 14 · Alex Petrov · 2019

中文 →

What is this chapter about?

When you tap "transfer sent," it isn't one machine keeping the books. It's three or five machines, each keeping its own copy. This chapter answers a blunt question: how do you guarantee those ledgers stay the same ledger? Not "roughly in sync" — but "nobody can ever produce two contradictory versions." Computing has a name for that job: consensus.

An analogy first

Picture five people in five cities with nothing but patchy walkie-talkies, trying to settle on a dinner time. Signals drop, people step away, and worst of all: you can never tell whether someone left or is just frozen for three seconds and about to come back. You have to settle on a time, and once settled, everyone who asks later must get the same answer — even the two who never heard it.

Why the old world struggles

The obvious fix is "pick a chief; the chief decides." The trouble starts when the chief goes quiet: the rest promote themselves at the same time, and now there are two chiefs and two contradictory ledgers — the condition engineers call split brain. Worse, the old chief may only have been stalled; he still believes he's in charge and keeps writing. This isn't hypothetical: one garbage-collection pause or one flaky cable is enough to manufacture two very confident chiefs.

The mechanism: two almost comically simple ideas

The first idea is "more than half." No decision counts unless more than half the group nods. The beauty is this: any two groups that each hold more than half must share at least one member — among five people, any two trios overlap. That overlapping person is a living archive. The moment a new round opens, they speak up: "hold on, we already settled on nine o'clock." So a new decision can never overturn an old one; it can only repeat it.

The second idea is "seniority check at every changeover." Each new chief gets a numbered term that only ever goes up. To become chief you need votes from more than half, and people only vote for someone whose records are no shorter than their own. So the incoming chief already holds everything that was ever settled and can't erase it on arrival. Add one rule — "the moment you see a higher term number, step down" — and the stale chief is told he's last season's news the instant he speaks.

How to think about it

In practice the world runs on two tellings of one story: Paxos, correct and famously hard to follow, and Raft, which deliberately retells the same thing as a manual you can implement from — the one behind etcd, Kubernetes, CockroachDB and modern Kafka. The honest cost: every write waits for more than half the group to nod, and if three of five machines die, the system would rather stop writing altogether than produce two versions.

One line to remember

Consensus isn't "majority rule by vote." It's a set of no-take-backs rules: everything needs more than half to agree, and any two such groups must overlap in at least one member who remembers the last decision — so history can only be continued, never rewritten.

Want the two phases of Paxos, Raft's terms and logs, and what Byzantine tolerance actually costs? → Switch to the deep read