Notes
The goal
Consensus means a group of machines agreeing on a value, and in practice on an ordered log of commands: set x to five, the lock belongs to server three. If every node applies the same log in the same order, they all end up in the same state. That's a replicated state machine, and it must keep working while some nodes crash.
- agree on one value, or one
- ordered log of commands:
- 1. set x = 5
- 2. lock owner = server-3
- 3. leader = B
- even if some nodes crash
- and messages are delayed
- every node applies the same
- log → same state
Majority
The key trick is majorities. With five nodes, a decision needs three. Any two groups of three overlap in at least one node, so two conflicting decisions can never both win. Five nodes tolerate two failures, three nodes tolerate one, and four nodes still tolerate only one, which is why clusters use odd numbers.
- 5 nodes → majority is 3
- any two majorities overlap in
- at least one node
- so two different decisions
- can never both win
- 5 nodes survive 2 failures
- 3 nodes survive 1 failure
- 4 nodes: still only 1 (no gain)
Leader election
Raft, the most popular consensus algorithm, starts by electing a leader. The leader sends regular heartbeats. If a follower hears nothing for a random timeout, it becomes a candidate, increases the term number and asks for votes. Each node votes once per term, and a candidate with a majority becomes leader. Random timeouts keep candidates from colliding.
- leader sends heartbeats
- follower times out (random
- 150–300 ms) → becomes candidate
- increments the term, asks votes
- each node votes once per term
- majority → new leader
- random timeouts avoid
- split votes
Replication
All writes go to the leader. It appends the command to its log as entry seven and sends it to the followers. Once a majority, counting itself, has stored it, the entry is committed: it's applied and the client gets success. A slow or crashed follower simply catches up later.
- all writes go to the leader
- leader appends entry #7 and
- sends it to followers
- majority stored it →
- COMMITTED, apply, reply
- slow or dead follower?
- it catches up later
Split brain
Now a network split cuts node A off. A can't reach a majority, so it can't commit anything, even if it thinks it's a leader from an old term. The other three still form a majority and carry on. When A rejoins, it sees a higher term, steps down and copies the current leader's log. Two leaders can never both commit.
- A is cut off (1 of 5)
- A cannot get a majority
- → cannot commit anything
- B, C, D (3 of 5) carry on
- when A rejoins it sees a
- higher term, steps down and
- copies the leader's log
- no split brain, by design
Paxos
Paxos is the original consensus algorithm, proven correct and famously hard to understand. Raft was designed later to give the same guarantees in a way engineers can follow. ZooKeeper uses its own protocol, Zab. They all share the same core: majority quorums, numbered terms or ballots, and a single leader at a time.
- Paxos: the original (1990s),
- proven, famously hard to
- understand; Multi-Paxos for logs
- Raft (2014): same guarantees,
- designed to be understandable
- Zab: ZooKeeper's protocol
- all: majority quorums +
- terms/ballots + one leader
In practice
You'll meet consensus inside etcd, which stores all of Kubernetes' state, in ZooKeeper and Kafka's controllers, in Consul, and in databases like CockroachDB and Spanner, which run a consensus group per shard. You almost never implement it yourself; you use one of these for leaders, locks and configuration.
- etcd (Kubernetes state)
- ZooKeeper (Hadoop, older Kafka)
- Kafka KRaft controllers
- Consul
- CockroachDB, TiDB, Spanner
- (per-shard Raft/Paxos groups)
- you rarely implement it:
- you use one of these for
- locks, leaders, config
Say it like this
In an interview, if you need a leader, a lock or cluster membership: use etcd or ZooKeeper, which run Raft or Zab on three or five nodes with majority quorums. Pair leadership with leases and fencing tokens. Keep consensus off the hot path; every write waits for a majority, so store only small, critical metadata there.
- "How do you elect a leader?"
- use etcd / ZooKeeper (Raft/Zab)
- 3 or 5 nodes, majority quorum
- leader lease + fencing token
- (episode 31)
- writes slower: majority round
- trip; keep it off the hot path
- store small, critical metadata