Raft leader election algorithm one vote for term?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Raft is a consensus algorithm designed as an alternative to the Paxos family of protocols. It aims to provide a more understandable approach while ensuring a similar level of fault tolerance and performance. One of the key components in Raft is the leader election process which ensures that a single leader is chosen from among the nodes in a distributed cluster to manage the logs' replication and maintain the system's overall consistency. Unlike multi-leader systems, Raft ensures that there is exactly one active leader at any time, simplifying the management of the replicated log.
Understanding Raft Leader Election
In Raft, time is divided into terms of arbitrary length, and each term begins with an election to choose a single leader. If a leader is stable, terms may last for a relatively long time. However, if the cluster cannot elect a leader or if the leader fails, new elections will be forced, potentially leading to rapid succession of terms.
Election Process
- Start of Election: When a node (follower) does not receive communication from the leader over a period of time (election timeout), it assumes there is no active leader and initiates an election to choose a new one for the next term.
- Election Timeout: To initiate an election, the follower increments its current term and transitions to a candidate state. It then votes for itself and sends
RequestVoteRPCs to all other nodes in the cluster. - Voting: Recipients of the vote request will only grant their vote if:
- They have not voted yet in this term
- The candidate’s log is at least as up-to-date as their own log This ensures that the leader of any given term has all the committed entries from previous terms.
- Election of Leader: The candidate will become the leader if it receives votes from a majority of the nodes in the cluster. It will then send heartbeat messages (empty AppendEntries RPCs) to all other nodes to assert its authority and prevent new elections.
- Handling Split Votes: If votes are split and no single candidate wins a majority, a new election starts with incremented term numbers. The use of randomized election timeouts helps minimize the chances of further split votes.
Leader Functions
Once elected, the leader handles all client interactions, log replication, and ensures consistency across all replicated logs of the followers. If the leader crashes, followers repeat the election process to choose a new leader.
Example Scenario
Consider a cluster of 5 nodes. Suppose nodes 2 and 5 time out and both transition to candidates for the same term and send RequestVote RPCs. If node 2 receives votes from nodes 1, 2, and 3, and node 5 from nodes 4 and 5, node 2 will be elected as the leader because it received a majority of votes (3 out of 5).
Benefits and Limitations
| Aspect | Benefit | Limitation |
| Fault tolerance | High (tolerates up to N/2 - 1 failures) | Limited by number of nodes (more nodes = more robust) |
| Performance | Leader simplifies decision-making & log replication | Election delay can impact response times in case of leader failures |
| Simplicity | Conceptually simpler than Paxos | Requires careful handling of edge cases like log inconsistency and split votes |
Additional Considerations
- Safety vs. Liveness:
- The election ensures that a leader has entries from all committed terms (safety), but it might delay leader election under network partition or severe failure scenarios (affecting liveness).
- Optimizations:
- Techniques like pre-vote (pre-election phase) or adjusting election timeout dynamically based on the network conditions can enhance the robustness and responsiveness of the leader election process.
In conclusion, Raft's one-vote-per-term leader election provides a clear approach to managing leaders in distributed systems, balancing ease of understanding with the sophistication needed for practical, fault-tolerant systems.
Related reading
- Raft loss of data/Log entries on leader re-election
- raft state is not determined
- RAFT term condition to commit an entry
- RAFT What happens when Leader change during operation
- Ramer-Douglas-Peucker path simplification algorithm
- Random-first search?
- Rails Postgresql replication via Octopus gem when in Development env
- Randomly generated group id for Kafka Consumer

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.