Choosing a captain
A club keeps one notebook of decisions, but every member has their own copy, and members sometimes fall ill. To avoid arguments, they pick a captain, the only one who takes new requests.
Choosing is simple: when nobody has heard from a captain for a while, someone stands and asks for votes. Anyone who gets votes from most of the club is captain. Each member votes once per round, so there can't be two.
Leader election
Raft is a consensus algorithm, used by etcd (and so Kubernetes), Consul, CockroachDB and Kafka's KRaft controllers. Each node is a follower, candidate or leader. Time is split into numbered terms.
A follower that hears nothing for its (randomised) election timeout becomes a candidate, increments the term,
votes for itself and sends RequestVote. A node votes at most once per term. A candidate with votes from a
majority becomes leader and sends heartbeats so nobody else starts an election.
One official notebook
Requests go to the captain, who writes the new line in their notebook and asks everyone to copy it. As soon as most of the club has it written down, the line is official, and it will never be lost: any future captain must be someone who has it.
Log replication and commit
The leader appends each client command to its log, then sends AppendEntries to the followers. Once a majority
has stored the entry it is committed: the leader applies it to its state machine and replies to the client,
and followers apply it when they learn the new commit index.
Because a candidate only wins votes if its log is at least as up to date as the voter's, any new leader already
has every committed entry. With 5 nodes, the cluster tolerates 2 failures; in general 2f + 1 nodes survive f.
When the captain disappears
If the captain falls ill, the "still here" notes stop. Someone's timer runs out, they stand for the next round, and the club carries on. If the old captain comes back, they see a newer round number and quietly step down.
That is not the only way a club gets stuck. Here are the other ways, and what the club does about each:
- Too many members ill at once. If fewer than most of the club are well, nobody can win the vote and nothing new gets written down. The club waits. What it already wrote stays true; it just can't add to it until enough members are back.
- A captain cut off from the rest. Ana is well, but a snowstorm stops her notes reaching the others. She still thinks she is captain and takes requests, but she can never get most of the club to copy them, so none become official. Meanwhile the others pick a new captain. Visitors to Ana need to give up after a while and try elsewhere.
- Two members stand at once. Each gets some votes and neither gets most. So everyone waits a different, random amount of time before standing again, and soon one stands alone and wins.
- A captain too slow to send notes. If Ana is so slow that her "still here" notes arrive late, members lose patience and call a new vote for no good reason. So the club waits a little longer before calling a vote, and makes sure the captain is never too busy to send "still here" notes.
A club of four needs three members for "most", just as a club of five does. So a club of four survives only one ill member, no better than a club of three.
What can go wrong
When a leader dies, followers time out and elect a new one, usually within a second. A returning old leader sees a higher term in any message and steps down. Terms act as a logical clock: stale leaders can't commit anything.
During a network split, only the side with a majority can elect a leader and commit writes. The minority side can't commit writes, which is the consistent choice in the CAP trade-off.
- Losing a majority. With 5 nodes, losing 3 leaves 2, which can't win an election or commit anything. The cluster
is unavailable, not wrong: committed entries are safe and writes resume when enough nodes return. You'd see client
timeouts, no leader (
etcd_server_has_leaderis 0), and repeated elections (without Pre-Vote, with rising terms). Fix: restore nodes. Restore from backup only as a last resort, because it discards state. - A partitioned old leader. A leader cut off from the majority still believes it leads, until it hears a higher term. (etcd enables CheckQuorum, so a leader that can't hear from a majority for an election timeout steps down by itself.) It can append entries, but never commits them, since it can't reach a majority. Once the partition heals it steps down and its uncommitted entries are overwritten. Clients see timeouts on writes and must handle them: a timed-out write may or may not have been applied eventually, so retries need idempotent requests.
- Split votes. Two candidates in the same term can split the vote so nobody wins, and the term ends with no leader. Raft randomises election timeouts so one node usually times out first. You'd see a few extra term bumps in the logs. Fix: keep the timeout range wide compared with network round trips.
- Leader flapping. A slow disk (the leader must persist entries before acknowledging) or a long GC pause can
delay heartbeats beyond the election timeout, so followers start needless elections. You'd see frequent leader
changes, elections in the logs and spiking write latency. Fix: faster disks, a smaller heap or a better collector,
and an election timeout comfortably above the pauses (in etcd, tune
--heartbeat-intervaland--election-timeout; defaults are 100 ms and 1000 ms). - Even cluster size. 4 nodes need 3 for a majority, so they tolerate 1 failure, the same as 3 nodes, with one more machine to run and more nodes that can fail. Use odd sizes.
| Choice | You gain | You pay |
|---|---|---|
| 5 nodes instead of 3 | Survives 2 failures instead of 1 | More machines, and each write needs 3 copies (2 follower acks) instead of 2 (1 follower ack) |
| Short vs long election timeout | Short: failover in less time. Long: fewer false elections | Short: more false elections when disks, GC or the network hiccup. Long: longer outage after a real leader crash |
| Reads through the leader (linearizable) | Always the latest committed data | The leader must confirm it still leads, and all reads load one node |
| Reads from any follower | Faster, spread across nodes | Possibly stale data (etcd calls this a serializable read) |
Cluster sizes: clusters are usually 3 or 5 nodes, because every write waits for a majority, so each extra pair of nodes buys one more tolerated failure at the cost of slower writes.
Paxos solves the same problem and came first; Raft was designed to be easier to understand and implement, with the same guarantees.