03. Data & Storage / Lesson 9 of 11
Consensus & Leaders
How a group of machines agrees on one leader and one order of writes.
- 12 min
- Advanced
- 11 steps
- Working leaders0 (warning)
- Votes to win—
- Crashes survived—
- Failover time—
- Request
- Response
- Write
- Health check
- Failed request
Step 1 of 11: Five copies, and nobody in charge
Read every step as text
Step 1: Five copies, and nobody in charge
A database keeps 5 copies of its data on 5 machines. If every machine accepts writes, two clients can change the same key to different values at the same moment, and the copies drift apart.
The usual fix is a leader: one node puts all writes in a single order and the others follow it. The hard part is choosing the leader when machines can crash and messages can be lost.
Consensus means a group of nodes agreeing on one value, such as who the leader is, even when some of them fail.
Step 2: Naive rule: the lowest id leads
A simple rule: each node follows the lowest-numbered node it can reach. When all is well, n1 leads. Then the network splits into {n1, n2} and {n3, n4, n5}.
n3 cannot reach n1 or n2, so it decides it is the lowest it can see and becomes leader. Now two leaders accept different writes. This is split brain, and the data forks.
A node cannot tell "the leader died" from "I cannot reach the leader". The rule must not depend on that guess.
Step 3: A leader needs a majority of all the nodes
New rule: to lead, a node must collect votes from a majority of all 5 nodes, which is 3. Not a majority of the nodes it can reach.
Any two groups of 3 out of 5 share at least one node, and a node votes only once, so two leaders cannot both win. The side with {n1, n2} has 2 votes and can never elect anyone; {n3, n4, n5} has 3.
A majority is the smallest group that any other majority must overlap with. That overlap is what makes agreement safe.
Step 4: Terms: a number for every election
Time is cut into terms, numbered 1, 2, 3 and so on. A term starts with an election, and each node votes for at most one candidate per term. So a term has at most one leader.
Every message carries the sender's term. A node that sees a higher term updates its own and steps down if it was a leader. An old leader with term 4 can never override a new one with term 5.
This is the idea behind Raft and Paxos. Raft is the easier one to learn, and etcd, Consul and Kafka's KRaft use it.
Step 5: n1 is cut off, n3 starts an election
The network splits again. n3 hears nothing from n1 for longer than its timeout. It becomes a candidate: term 5, votes for itself, and asks the others for their votes.
n4 and n5 have not voted in term 5, so they say yes. n3 has 3 of 5 votes, a majority, and becomes leader. n1 and n2 cannot be reached, which is fine.
Each follower picks a random timeout, so two nodes rarely become candidates at the same instant. If they do, the vote splits and a new term starts.
Step 6: Heartbeats say "I am still the leader"
The leader sends a small message to every follower every 50 ms. Each message resets the follower's election timer.
A follower that hears nothing for a random 150 to 300 ms assumes the leader is gone and starts an election. That timeout is at least 3 heartbeats long, so one lost message does not cause an election.
Too short a timeout causes needless elections on a slow network; too long means longer downtime after a real crash.
Step 7: Writes go into a log that the leader copies
The leader keeps an ordered log of commands. A client sends
x = 5. The leader adds it as entry 7 and sends the entry to every follower.A follower appends the entry in the same position and acknowledges it. Because every node applies the same log in the same order, they all end in the same state.
Consensus is not only about electing a leader: it is also how the group agrees on the order of every command.
Step 8: Committed once a majority has it
n3 has entry 7 and so do n4 and n5: that is 3 of 5, a majority. The entry is now committed: it will survive any 2 crashes. n3 applies it and answers the client.
n1 and n2 are still cut off and have not seen it. That does not matter. The cluster does not wait for every node, only for a majority.
A committed entry is safe because any future leader needs a majority, and that majority must include a node that has the entry.
Step 9: The leader crashes and a new one takes over
The network heals and n1, n2 start catching up. Then n3 crashes. The others stop hearing heartbeats, and within 150 to 300 ms one times out. Say n4 wins term 6; the whole switch takes about 155–305 ms, and writes wait for it.
A node votes only for a candidate whose log is at least as complete as its own. n1 and n2 are behind, so they can never win and never throw away entry 7.
While there is no leader, writes pause. Reads from followers may still work but can be old.
Step 10: Why clusters have 3, 5 or 7 nodes
A cluster of n nodes needs a majority of ⌊n/2⌋ + 1 and survives n minus that. Four nodes need 3 votes and survive 1 crash, no better than three nodes (2 votes, 1 crash). The fourth machine adds cost and no safety.
Even sizes also split badly: with 4 nodes, a 2 and 2 split leaves both sides without a majority, so nobody can lead.
Use 3 for small clusters and 5 when you want to survive two failures. More than 7 makes every write slower for little gain.
Step 11: Consensus in one picture
A leader needs votes from a majority of all nodes (3 of 5), so two leaders can never exist in the same term. Terms and heartbeats detect a dead leader, and a new election takes about 155–305 ms.
A write is committed when a majority holds it, so it survives any minority of crashes. Real systems that do this for you: etcd, Consul, ZooKeeper, Kafka's KRaft, and Paxos-based stores such as Spanner.