How a Raft leader election works
Every node starts as a follower with a random timer. The first timer to expire asks for votes; whoever collects a majority becomes leader and keeps everyone calm with heartbeats.
Tweak it
Break it
Normal run: a leader is elected and heartbeats keep the cluster stable.
What to notice
- A follower that hears no heartbeat before its election timer expires becomes a candidate, bumps the term and requests votes. Each node votes once per term, and a majority wins.
- Break it: followers notice within one timeout, the cluster elects a higher-term leader, and the old leader comes back as a follower.
- Set the randomness to 0: every timer expires together, every node votes for itself, and nobody ever gets a majority. Random timeouts are the fix for split votes.
- The strip at the bottom shows leadership over time. Red means the cluster cannot accept writes. Try a bigger latency or a smaller timeout and watch the gaps change.
- Simplified: this shows leader election only (no log replication) and models a crash, not a network partition.