Sub topic 8
Election timeouts, split votes, and pre-vote
This chapter assumes you know the Raft roles and terms, and how RequestVote grants at most one vote per term. It covers how timing decides who becomes a candidate, what happens when elections collide, and how pre-vote stops a partitioned server from disrupting a healthy cluster.
Why timeouts are randomized
A follower becomes a candidate when it hears nothing from a leader for one election timeout. If every server used the same timeout, they would all time out together. Each would vote for itself, and no one would reach a majority.
Raft avoids this by giving each server a random timeout, chosen again every time the timer resets.
import random
HEARTBEAT_INTERVAL = 0.050 # seconds, how often a leader sends AppendEntries
def new_election_timeout():
# Randomized per server and per reset, so servers rarely time out together.
# The range must be well above the heartbeat interval and the network round trip.
return random.uniform(0.150, 0.300) # seconds
timeouts = [new_election_timeout() for _ in range(5)]
print(sorted(round(t, 3) for t in timeouts))The server with the shortest timeout usually fires first. It becomes a candidate, increments its term, and sends RequestVote before the others have timed out. Receiving a valid vote request resets their timers, so they stay followers.
Choosing the numbers
Three timescales must stay in this order:
- Broadcast time (one round of RPCs), typically a few milliseconds.
- Election timeout, typically 10 to 100 times larger.
- Mean time between failures of a single server, far larger still.
The heartbeat interval should be well below the minimum timeout, often a third or less. If the timeout is too close to the heartbeat interval, one delayed heartbeat triggers a needless election. If the range is too narrow, collisions become common. If the timeout is too long, the cluster stays leaderless for longer after a real crash.
Split votes
A split vote happens when two or more candidates start in the same term and no one gets a majority. Each server grants one vote per term, so the votes divide.
Take a five-server cluster. S1 and S2 time out almost together in term 4. S3 votes for S1, S4 votes for S2, and S5 is slow or unreachable. Each candidate has 2 votes, and a majority needs 3.
No special recovery step exists. Each candidate waits for its own timer to expire and then starts a new term. Because the timeouts are random again, one candidate is likely to go first and win. A split vote costs time but never breaks safety.
Disruption from rejoining servers
Suppose S5 is cut off from the cluster. It hears no heartbeats, so it times out, becomes a candidate, and increments its term. It never wins, so it times out and does this again, many times. Its term grows to something like 90 while the healthy cluster stays at term 4.
When the partition heals, S5 sends a message with term 90. Every server that sees a higher term updates its own term and steps down. The current leader becomes a follower, and the cluster has to run an election even though nothing was wrong with the leader. S5's log is stale, so it cannot win, but the cluster lost its leader anyway.
Pre-vote
Pre-vote is an extension described in the Raft dissertation. Before becoming a real candidate, a server runs a trial round:
- The server sends
PreVoterequests carrying the term it would use (current_term + 1), but it does not change its own term orvoted_for. - A receiver grants a pre-vote only if it has not heard from a leader within the minimum election timeout, and the requester's log is at least as up to date as its own.
- If the server collects a majority of pre-votes, it increments its term and starts a normal election. Otherwise it stays a follower and tries again later.
The isolated S5 never gets a majority of pre-votes, so its term stays at 4. When it rejoins, it sends no inflated term and the leader keeps its position.
Receivers must also ignore pre-vote requests while a leader is active. Otherwise, pre-vote would just move the disruption one step earlier.
Exercise
A cluster uses a 50 ms heartbeat interval and an election timeout range of 60 to 70 ms. Name two problems you would expect, and suggest a better range.
Show answer
The minimum timeout is barely above the heartbeat interval, so a single delayed heartbeat makes a follower start an unnecessary election. The range is only 10 ms wide, so servers often time out at nearly the same moment and split votes become common. A better choice is a heartbeat of about 50 ms with timeouts of 150 to 300 ms: the minimum is several heartbeats long and the range is wide enough to separate candidates.