BOOK DEEP READ · Database Internals · Chapter 10

Leader Election

Database Internals · Ch 10 · Alex Petrov · 2019

中文 →

What is this chapter about?

If a team had to hold a full vote on every small decision, nothing would ever get done. So the group picks one person to call the shots — you bring things to them, and their word is final. Every time you transfer money, send a message or upload a photo, the few dozen machines behind it are doing exactly that: picking one machine to be in charge. Chapter 10 of Database Internals is about how that pick is made, and why it is far harder than it sounds.

A strange thing first: the boss gets replaced while alive

The most common reason for replacing the boss is not that the machine actually broke. It is that its disk got busy and it answered half a second late. Nobody hears it speak up, everyone assumes it is gone, and a new election starts — while the machine itself, blissfully unaware, is still working normally. Replacing the boss is usually triggered by a misunderstanding, not by a failure.

Why the old world was hard: two bosses beat no boss

With no boss, work stalls — but it fails loudly, and someone comes to fix it. The real disaster is two bosses at once: the old one never actually died, it just went unheard, and the new one has already taken office. Two people writing in the same ledger at the same time can bill your payment twice, and afterwards nobody can tell which ledger to believe — both are internally consistent. This is the accident the whole chapter is built to prevent.

The core ideas: a few ways to pick, and one more important seal

The chapter lays out several classic approaches, each a step past the last:

But the thing most worth remembering is none of the above. It is the seal: give each boss an official stamp engraved with their term number, and rule that documents count only if they bear the newest term. Now it no longer matters if the old boss wakes up still issuing orders — the stamp in their hand is last term's, so everything they stamp is void.

How to choose

The real trade-off is not "which picking method is cleverest." It is two things: do not keep swapping (every swap stops everyone while the handover happens), and once you swap, the old boss must truly lose power. The first comes from "don't move unless something actually broke"; the second comes from that term-numbered seal.

The honest cost: no picking method can guarantee there is only ever one boss — the best any of them promises is "eventually only one is left, after a messy stretch." The safety net was never in the picking. It is in the stamping.

One sentence to remember

Picking a boss is easy; voiding the previous one is the hard part. So stop expecting the election itself to be both fast and correct — stamp every term with a number and make everyone honour only the newest one. Then even when you get it wrong and briefly have two bosses, the ledger stays intact.

Want the actual algorithms, message costs and diagrams? → Switch to the deep read