Raft Leader Election
Trigger elections, kill nodes, partition the network, and watch consensus emerge.
How Raft Leader Election Works
Raft is a consensus algorithm designed to be understandable. Leader election is the first piece: a cluster of nodes must agree on a single leader to coordinate operations.
Every node starts as a follower, waiting to hear from a leader. Each follower has a randomized election timeout — if it doesn't receive a heartbeat before the timer expires, it assumes the leader is gone and starts an election.
To start an election, a node becomes a candidate, increments its term number (a logical clock for the cluster), votes for itself, and sends RequestVote messages to all other nodes. If it gets votes from a majority of the cluster (3 of 5 here), it becomes the leader.
The leader then sends periodic heartbeats to all followers to maintain authority and reset their election timers. If a leader sees a message with a higher term, it immediately steps down to follower.
The randomized election timeout is critical — it makes split votes (where no candidate gets a majority) unlikely, and when they do happen, the randomization means one candidate will almost always time out first in the next round. Combined with the majority quorum rule, this prevents split-brain: you can never have two leaders in the same term.
Try killing the leader and watch a new election happen. Partition the network so the leader is isolated from the majority — the majority side will elect a new leader, while the old leader on the minority side can't get a quorum. Heal the partition and the old leader recognizes the new leader's higher term and steps down.