Step 1 · The pattern
One leader, and a heartbeat
Five nodes, one leader. Clients send every write to it. The leader sends a heartbeat to each follower, and every heartbeat restarts that follower's election timer: the bar under each node. While heartbeats arrive, no bar fills up.
- heartbeat / leader
- write
- accepted
In short
Leader election (Raft): how it works and how it breaks
A Raft cluster has one leader, and it proves it is alive by sending heartbeats. Each follower runs an election timer that every heartbeat restarts. If the timer runs out, the follower assumes the leader is gone: it moves to the next term, votes for itself and asks every other node for a vote. A node gives one vote per term, to the first candidate that asks, and a candidate needs votes from a majority of the whole cluster. Two majorities always share a node, so one term can never have two leaders. That rule keeps elections safe, but it does not make them finish. If several followers time out together, each votes for itself and nobody reaches a majority. Raft avoids this by having every node pick its timeout at random from a range, so one node usually starts first and wins before the others begin.
How this comes up in interviews
"The leader of a five-node Raft cluster crashes. Walk me through how a new leader is chosen and why there can never be two leaders in one term."
Then they ask
- Several followers time out at the same moment. What happens, and how does Raft avoid it?
- Can a node whose log is missing committed entries win the election? What stops it?
- A node cut off from the cluster returns with a much higher term. What does that do to a healthy leader, and how can it be prevented?
A strong answer covers
- The leader sends heartbeats, and each heartbeat restarts a follower's election timer.
- When the timer runs out, a follower moves to the next term, votes for itself and asks every other node for a vote.
- A node gives one vote per term and a candidate needs a majority of the whole cluster. Two majorities share a node, so a term cannot have two leaders.
- The majority rule makes elections safe, not certain to finish: followers that time out together each vote for themselves and nobody wins.
- Each node picks its timeout at random from a range, so one usually starts first and wins before the others begin.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: Ongaro and Ousterhout: In Search of an Understandable Consensus Algorithm (2014) ↗ · The Raft consensus algorithm (project site) ↗ · Ongaro: Consensus: Bridging Theory and Practice (dissertation, section 9.6 on Pre-Vote) ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.