How Raft agrees on a leader
Five servers must agree on one leader and one order of writes, even when machines crash and messages arrive late. Here is how Raft does it with terms, votes and majorities, drawn one message at a time.
01 Why replicas need to agree on one order
You keep three or five copies of a database so that losing a machine loses nothing. The copies only help if they hold the same writes in the same order. If one copy applies x=1 then x=5 and another applies them the other way round, they now disagree for ever, and a client that switches between them sees time run backwards.
The easy fix is one leader that decides the order and tells the others. The hard part is what happens when the leader dies, or only looks dead because the network is slow. Two servers must never both believe they lead and accept conflicting writes. Raft (Diego Ongaro and John Ousterhout, 2014) is a set of rules that makes this safe, and it was designed to be easier to follow than Paxos. etcd, Consul, CockroachDB, TiKV and Kafka's KRaft mode all use it.
- One leader per term. Time is cut into numbered terms, and each term has at most one leader.
- A majority decides. A leader is elected by a majority, and a write counts once a majority has stored it.
- The log is the truth. Every server applies the same log of commands in index order, so the same log gives the same state.
02 Terms, roles and the election timeout
Every server is in one of three roles, and keeps a number called the current term. Every message carries the sender's term. The rule that holds it all together: if you see a higher term than yours, you adopt it and become a follower at once. If you get a message with a lower term, you reject it.
| Role | What it does | Becomes something else when |
|---|---|---|
| Follower | Answers the leader and candidates; never starts anything | Its election timeout passes with no heartbeat: it becomes a candidate |
| Candidate | Moves to a new term, votes for itself, asks for votes | It wins a majority (leader), sees a leader of its term or higher (follower), or times out (new election) |
| Leader | Takes client writes, sends log entries and heartbeats | It sees a higher term in any message: it steps down to follower |
Terms work like a logical clock. A server that was cut off and comes back with an old term learns, from the first message it gets, that the world has moved on. A stale leader cannot do harm for long: the first follower that answers it with a higher term makes it step down.
The election timeout is random for each server and each election, for example between 150 and 300 ms as in the Raft paper. If all followers timed out together, they would all stand at once, split the votes and try again for ever. With random timeouts, one server usually starts first and wins before the others wake up.
03 An election, one message at a time
Here is a five-server cluster losing its leader. Watch the term numbers: they decide everything. F, C and L mean follower, candidate and leader.
- candidate
- just updated its term
- just sent or stored
- leader
- crashed
Five servers. S1 led term 1 and has just crashed. The other four are followers in term 1, each waiting for a heartbeat that will not come. Each has its own randomised election timeout running.
A server grants a vote only if all of these hold: the candidate's term is at least its own, it has not already voted for someone else in this term, and the candidate's log is at least as up to date as its own. Up to date means: the last entry has a higher term, or the same term and the log is at least as long. This last rule is what stops a server with missing writes from becoming leader and erasing them.
If two candidates split the vote, say 2 and 2 with one server down, nobody wins. Both time out again, each after a fresh random wait, and one usually gets ahead the second time.
04 Log replication: committed means on a majority
Once elected, the leader takes every write. It appends the command to its log, copies it to the followers with AppendEntries, and waits until a majority hold it. Only then is the entry committed: Raft promises it will never be lost or changed, as long as a majority of servers survive.
- just appended
- committed
- nothing there yet
- server down
Term 2, leader S3. Entries 1 and 2 are committed everywhere. S1 is still down, holding a y=0 it appended in term 1 but never copied. A client asks S3 to set x to 5.
Every AppendEntries carries the index and term of the entry just before the new ones. A follower accepts only if its own log has that same entry. Because each check is made against the entry before, and that one was checked against the one before it, a match at one index means the two logs are identical up to there. Raft calls this the log matching property.
05 When a follower’s log has diverged
Crashes leave followers with logs that are behind, or that hold entries an old leader wrote but never committed. The leader does not try to merge anything. It finds the last entry where the follower agrees with it and overwrites everything after that.
- rejected
- checked, mismatch
- last matching entry
- deleted
- just appended
S1 comes back. While it led term 1 it had appended y=0 at index 3, but crashed before copying it anywhere, so it was never committed. Index 3 now disagrees with the leader.
Overwriting is safe only because of the voting rule. A committed entry is on a majority, a new leader needs votes from a majority, and the two groups share at least one server. That server refuses to vote for a candidate whose log lacks the entry. So every leader already holds every committed entry, and the only entries it ever overwrites are uncommitted ones.
A leader counts copies only for entries from its own term. An old entry that happens to sit on a majority is committed indirectly, when a newer entry after it commits. The Raft paper shows a case (its Figure 8) where counting old-term copies would let a committed-looking entry be overwritten later.
06 Why majorities: what a cluster survives
Any two majorities of the same cluster overlap. That single fact gives you both guarantees: two leaders cannot win the same term, and a new leader always meets a server that saw every committed write. The price is that the cluster needs a majority alive to do anything.
| Servers | Majority | Can lose | Note |
|---|---|---|---|
| 1 | 1 | 0 | No fault tolerance |
| 3 | 2 | 1 | The usual minimum |
| 4 | 3 | 1 | No better than 3, and one more machine to fail |
| 5 | 3 | 2 | Common for control planes such as etcd |
| 7 | 4 | 3 | Each write now waits for 4 copies |
Consider a network split: S1 and S2 on one side, S3, S4 and S5 on the other, with S1 the leader. S1 can still accept a write, but it can never get it onto 3 servers, so it never commits it or tells the client OK. The other side elects a new leader in a higher term and carries on. When the network heals, S1 sees the higher term, steps down, and has its uncommitted entries overwritten.
07 The vote rule in code
The receiver side of RequestVote is short. This is a simplified version: it leaves out persistence, timers and networking. A real server must write term, voted_for and the log to disk before replying, or a restart could let it vote twice in one term.
That output is from a real run with Python 3.14. S5 is refused because S4 already voted in term 2. S1's term 3 request is refused because its log is shorter, yet S4 still moves to term 3. The last request is refused because term 1 is in the past, however long its log is.
On a real etcd cluster, etcdctl endpoint status --cluster -w table lists each member with columns including IS LEADER, RAFT TERM and RAFT INDEX, so you can watch the term go up when you stop the leader.
08 Where Raft clusters go wrong
3 servers in one rackA majority that shares a faultThree servers on one switch or in one zone are one failure from losing the majority. Spread members across failure domains, accepting that each write then waits on a cross-zone round trip.