Deep-Read · DDIA · Chapter 9

Consistency and Consensus

Designing Data-Intensive Applications · Ch 9 · Martin Kleppmann · 2017

中文 →

What is this chapter about?

Every large website you use runs not on one computer but on hundreds or thousands working together. They must agree on certain facts: who is the leader (who's in charge), whether an order actually went through, whether a username is already taken. This chapter is about how a bunch of computers — each of which can crash, and whose messages can be lost — reliably get on the same page. That's consistency and consensus.

An analogy

Picture a room of people sharing one notebook — except each person actually holds their own photocopy. Ideally: the instant anyone writes a line, everyone's next glance shows the same latest value, and nobody ever reads a stale one. That effect — a pile of copies that behaves as if there were only one — is this chapter's most important idea, called linearizability. The catch: syncing copies takes time, so a careless glance shows you what someone wrote three seconds ago.

Why the old world is hard

If the computers disagree, chaos follows: two machines both think they're the leader (called split brain) and a charge gets applied twice; two people both grab the same username. Worse, the network partitions — half the machines suddenly can't reach the other half, and nobody can tell whether the other side truly died or is just temporarily unreachable. Making one unified decision in that half-light is the hardest thing in distributed systems.

The core trick: majority vote

The secret is surprisingly simple — vote, and require a majority. To get everyone to accept a fact, have more than half the machines vote for it. Why more than half? Because any two majorities must share at least one member, so you can never simultaneously elect two contradictory outcomes — split brain is blocked at the root. Electing a leader is one such vote; ordering events is another — everyone copies what happened into one shared ledger in the same order, nobody cuts in line, and the order is unified. This "reach agreement by majority" machinery is called consensus.

How to use it

Good news: this stuff is fiendishly hard to get right, so don't build it yourself — off-the-shelf coordination services like ZooKeeper and etcd already package it; just call them for leader election, locks, and ordering. And remember the famous trade-off (CAP): once the network partitions, you can keep either consistency or availability, not both. Honestly, agreement isn't free — waiting for a majority on every decision is inherently slower than a single machine; that slowness is what buys you "never gets it wrong."

Remember this

For a crowd of computers that crash and lose contact to reliably agree, the tool is a majority vote — which in one stroke handles leader election, ordering, and grabbing a unique name. The price is more latency, and when the network splits you must pick consistency or availability. Don't build it yourself — use ZooKeeper / etcd.

Want the real machinery — linearizability, total order broadcast, consensus and 2PC? → Switch to the deep read