Leader Election: Triggering Elections
You'll learn how followers in Raft detect when a leader has failed and automatically initiate a new election to restore cluster leadership.
The Problem of a Silent Leader
In a distributed consensus system like Raft, followers depend on the leader to coordinate all decisions. But what happens when the leader crashes or becomes unreachable? The cluster can't make progress without a functioning leader, so followers must detect this failure and trigger a new election.
Timeout-Based Failure Detection
Raft uses election timeouts as the trigger mechanism. Here's how it works:
Each follower maintains an election timer that continuously counts down. When the follower receives an AppendEntries RPC from the leader (even if it's just a heartbeat with no log entries), the follower resets this timer back to its starting value.
If the timer expires before receiving any message from the leader, the follower assumes the leader has failed. This is the trigger point.
The Transition to Candidate
When a follower's election timeout fires:
- The follower transitions from Follower state to Candidate state
- It increments its current term (the logical clock)
- It votes for itself
- It sends
RequestVoteRPCs to all other servers in the cluster - It resets its election timer with a new randomized timeout
The randomization is crucial—it prevents multiple followers from timing out simultaneously and causing split votes. Think of it like adding a small random delay before raising your hand to volunteer, reducing the chance everyone volunteers at once.
Key Takeaway: Followers use randomized election timeouts to detect leader failure; when the timeout expires without hearing from the leader, a follower becomes a candidate and starts an election by requesting votes from peers.