Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Consensus: who holds the pen

Chapters

Narration

The bouncer at the door could decide alone: one machine, one counter, one instant no. But the chai stall's ledger has only one pen, and four episodes back we watched the clerk holding it collapse. Which copy picks the pen up? And what if two of them do? Two pens means two books, and lines that can never be merged again. That is split brain. The fix is arithmetic. A decision counts only if a majority agrees, three of five. And here is why three is enough. Take any two majorities of five and count the members: they always share at least one. That shared member will not vote for both, so two different decisions can never both win. Quorum is N over two, plus one, and that is why clusters come in odd numbers. A cluster of two cannot split fairly. Never build one. So how do five machines pick one? Raft. Everyone starts as a follower, listening for a heartbeat. Miss enough of them and a follower's timer runs out. It raises the term, becomes a candidate, and asks the other four for their votes. Win a majority and it is the leader, and it starts sending heartbeats of its own, which keeps everybody else a follower. The timers are random on purpose, so one machine wakes first and ties break themselves. Now the pen gets used. Every write goes to the leader, and only to the leader. It appends the line to its log and ships it out to the followers. Then the word that matters: committed. A line is committed once a majority have stored it, and not a moment before. Only then is it applied and acknowledged. A minority can never reach a majority, so it can never commit, and the write simply waits. But a majority settles who should hold the pen. It settles nothing about the clerk who never heard the news. So the pen carries a number: the term, an epoch clock that ticks up once per election. A leader from an old term is simply ignored, its heartbeats rejected, its writes turned away by the paper itself. And a vote goes only to a candidate whose log is at least as current as your own, so a new leader already holds every committed line. Now cut the wire. Three machines on one side, two on the other. The majority side can still elect, can still commit, and keeps serving. The minority side cannot reach three, so it cannot elect and cannot commit. It goes unavailable, on purpose. That is CAP's CP, mechanised. And you already depend on it. Etcd is the brain of a Kubernetes cluster, so every apply you run is a Raft commit. Consul, ZooKeeper and Kafka's KRaft are the same idea, and CockroachDB runs one Raft group per shard. So the pen is not something a machine owns. It is something a majority lends it, for one term, and takes back the moment they cannot see it. There is a lab on the site where you can split the room yourself and watch the two books come apart.

Saved in this browser, and to your account when you sign in.