In the Raft Consensus Protocol, a cluster is organized around a single authoritative Leader.
The Leader is responsible for accepting all client mutations, appending them to its local log, and managing replication across Follower nodes.
If the Leader crashes or becomes partitioned from the cluster, Follower nodes detect the absence of heartbeats and initiate a Leader Election.
1. The 3 Raft Node States
At any given time, every node in a Raft cluster exists in exactly one of three states:
2. Terms as Logical Clocks
Time in Raft is divided into arbitrary-length Terms, numbered with consecutive integers ().
Terms act as a Lamport Logical Clock, allowing nodes to detect obsolete information (such as staled leaders or partitioned candidates).
The Term Rules:
- Higher Term Always Wins: If any node receives a message containing a term , the node immediately updates its term to and transitions to Follower.
- Obsolete Messages are Rejected: If a node receives a request with term , the request is immediately rejected.
- At Most One Leader Per Term: A node can vote for at most one candidate in a given term (first-come, first-served).
3. How Randomized Election Timers Prevent Split Votes
If all nodes timed out simultaneously (e.g., exactly at ), all followers would become candidates at the exact same instant, vote for themselves, and split the remaining votes equally. No candidate would achieve a majority quorum, leading to an endless election loop.
Raft solves this with Randomized Election Timers:
- Each node chooses an election timeout chosen randomly from a fixed range (typically ).
- Because timers are randomized, one node (e.g. Node 1 at ) will time out before its peers ( and ).
- Node 1 increments its term, requests votes, wins the majority, and broadcasts heartbeats before any other node's timer expires.
4. Code Deep-Dive: Raft Election State Machine
5. Production Failure Postmortem: Flapping Elections on Congested Networks
Incident Overview:
In 2021, an enterprise Kubernetes cluster experienced recurring API server outages caused by etcd entering a continuous leader-election loop during routine backup operations.
What Happened:
- An automated backup script saturated the 1Gbps network interface of the active etcd leader.
- The leader's periodic
AppendEntriesheartbeats were delayed in TCP send queues by over . - Follower nodes configured with a fixed election timeout of assumed the leader was dead and triggered new elections.
- Because the network congestion affected all nodes intermittently, elections constantly timed out and split votes, preventing any candidate from sustaining leadership for more than .
Remediation:
- Adjusted the election timeout window to to tolerate transient queue spikes.
- Isolated consensus replication traffic to a dedicated physical VLAN with strict QoS network prioritization.
6. Landmark Capstone #3: Build a Distributed Raft Leader Election Engine ⚔️
Test your understanding of logical terms, majority vote calculation, and candidate state transitions in our interactive sandbox:
👉 Launch Capstone: Build a Raft Leader Election Engine
- Supported Languages: TypeScript & Python 3
- Challenge Focus:
- Implement term increments and self-voting on election timeouts.
- Enforce single-vote per term rules in
handleRequestVote. - Process vote responses and trigger transitions to
LEADERwhen majority quorum is attained. - Step down to
FOLLOWERwhen encountering higher terms.