Raft Leader Election
Raft is a consensus algorithm in which a cluster elects one node as leader for a numbered term, or election epoch, and uses that leader to coordinate replicated state. Leader election gives the cluster a clear authority even when messages are delayed, nodes fail, or several nodes try to take control at once.
The problem leader election solves
A replicated service cannot safely accept competing decisions from multiple nodes. If two replicas both believe they are in charge, they might assign different orders to the same operation or overwrite each other's state. Network failures make this especially difficult: a healthy node may be unable to tell whether the current leader has failed or is merely unreachable.
Without a protocol, nodes might repeatedly declare themselves leader, producing split brain—multiple apparent leaders operating in the same cluster. Raft avoids that by requiring a candidate to win a majority and by attaching every election-related message to a term.
A Raft node is normally one of three roles:
- A follower accepts messages from a leader or candidate and does not start elections while the cluster appears healthy.
- A candidate is a node asking peers to vote for it.
- A leader sends regular heartbeats and coordinates replicated operations.
These roles are local states, not permanent identities. A follower can become a candidate, and a candidate or leader can step down when it learns about a newer term.
Terms provide a common ordering
A term is a monotonically increasing integer stored by each node. It is not a clock duration; it is a logical version of the cluster's leadership history. Each election starts a new term, and at most one leader can be elected for a given term because each node may grant at most one vote in that term.
Every node includes its current term in Raft messages. When a node receives a message with a higher term, it updates its stored term and becomes a follower. A message from an older term is stale: the receiver rejects it and does not let it change the receiver's current role or state.
This rule handles delayed traffic. For example, a candidate from an earlier, unsuccessful election might finally reach a node after a newer leader has been elected. The old candidate cannot regain authority merely because its message arrived late.
How an election starts
A follower waits for a leader's heartbeat. In Raft, a heartbeat is an AppendEntries message that may carry log entries or may be empty. Each follower has an election timeout, the maximum period it will wait without hearing from a valid leader before starting an election.
The timeout is randomized independently for each node within a configured range. That matters because if every follower used the same fixed timeout, they could all time out together and compete in the same election. Randomization makes one node likely to time out first, while the others are still waiting for its new leader's heartbeat.
When a follower's timeout expires, it:
- Increments its term.
- Changes its role to candidate.
- Votes for itself.
- Resets its election timer.
- Sends a
RequestVotemessage to every other node.
The candidate includes its term, its own identifier, and information about its replicated log. The log information lets receivers avoid electing a node whose log is behind. In simplified form, the request says: “I am standing for election in term T; here is the last entry in my log; will you vote for me?”
How RequestVote produces a leader
A follower receiving RequestVote first compares terms:
- If the request's term is older than the follower's term, it rejects the request.
- If the request's term is newer, the follower updates its term, becomes a follower, and considers the request under the new term.
- If the terms match, it grants the vote only if it has not already voted in that term and the candidate's log is at least as up to date as its own.
“Up to date” is determined by the last log entry's term first, then by its index—the position of the entry in the log. A log ending in a higher-term entry is newer; if the ending terms match, the longer log is newer. This prevents a candidate from becoming leader while missing committed operations that other nodes know about.
A candidate becomes leader after receiving votes from a majority, more than half of all cluster members. In a five-node cluster, that means three votes including its own. The candidate then immediately sends heartbeats, establishing that it is the leader for that term and causing followers to reset their election timers.
A majority is important for both safety and availability. Any two majorities overlap in at least one node, so two different candidates cannot both receive a majority in the same term. A minority partition may still contain a candidate, but it cannot become a legitimate leader because it cannot collect enough votes.
Split votes and another round
A split vote occurs when multiple candidates start in the same term and divide the available votes so that none reaches a majority. This is possible despite randomized timeouts, especially under timing collisions or temporary network problems.
When a candidate discovers that it cannot win—typically because its election timeout expires without a majority—it starts another election with a higher term. It votes for itself again, sends new RequestVote messages, and resets its timer. Randomized timeouts make it likely that one candidate starts the next round before the others and collects enough votes.
If a candidate receives a valid leader heartbeat in its current term, it returns to follower. If it receives a RequestVote message for a newer term, it also steps down and processes that newer election. Thus, an old candidate does not keep campaigning after the cluster has moved on.
What stale candidates explain in practice
Logs often show messages such as “higher term,” “rejecting vote,” or “stepping down.” These usually do not mean that the node is corrupt. They often mean that a delayed candidate request, heartbeat, or response crossed the network after a newer election had already happened.
Raft's combination of randomized election timeouts for liveness, majority voting for uniqueness, and term comparisons for rejecting stale information turns an unreliable network into a controlled election process. The cluster may need several rounds during a failure, but once a candidate obtains a majority, the term and heartbeat rules give the rest of the cluster a single authority to follow.