Sub topic 3
Followers, Candidates, Leaders, and Terms
This chapter assumes you know what a replicated state machine is and why a majority quorum survives crashes and partitions. It also assumes you can read short Python and understand request/response RPCs. It covers the three roles a Raft server can hold and the term number that orders them. How votes are requested and counted is left to the next chapter.
Three roles, one at a time
Every Raft server is in exactly one of three roles at any moment. The role decides which messages the server sends and how it reacts to the messages it receives.
| Role | What it does | Enters this role when | Leaves this role when |
|---|---|---|---|
| Follower | Answers RPCs from leaders and candidates. Sends no RPCs of its own. | It starts up, or it learns of a higher term, or it loses an election. | Its election timeout expires with no valid contact. |
| Candidate | Asks every other server for a vote in a new term. | A follower's election timeout expires. | It wins, it sees a legitimate leader, or its election times out. |
| Leader | Accepts client commands, replicates log entries, and sends periodic heartbeats. | A candidate receives votes from a majority. | It sees a higher term. |
A heartbeat is an AppendEntries RPC that carries no log entries. Its only job is to tell followers that the leader is still alive, which resets their election timers.
Followers are passive on purpose. All data flows from the leader to the followers. A follower never forwards its own log to anyone, and it never decides on its own what should be in the log. This keeps the protocol easy to reason about, because there is one source of truth at a time.
Servers all start as followers. A cluster that has just booted has no leader, so the first server whose election timeout fires becomes a candidate.
Notice that a leader has exactly one way out: learning that the cluster has moved on to a higher term. Nothing else demotes it in the basic protocol.
Terms are a logical clock
Raft divides time into terms. A term is an integer that starts at 0 and only ever increases. Each term begins with an election. If a candidate wins, it serves as leader for the rest of that term. If the vote splits and nobody wins, the term ends with no leader, and a new term begins shortly after.
Every server stores a current_term. Every RPC and every reply carries the sender's term. This makes terms a logical clock. Servers cannot agree on wall-clock time, but they can always agree on which of two terms is later. A message with a larger term is newer information, no matter when it arrives.
Two properties hold in every execution:
- At most one leader per term. A candidate needs votes from a majority, and each server grants at most one vote per term. Any two majorities of the same cluster share at least one server, so two candidates cannot both win the same term.
- Terms never decrease on any server. A server only moves its
current_termforward.
A term can have zero leaders, but never two. Terms are also not durations. Term 7 might last an hour, and term 8 might last 40 milliseconds before another election supersedes it.
The term rules
Servers compare terms on every incoming message. Three rules cover all cases.
- Higher term seen. If a request or a reply carries a term greater than
current_term, setcurrent_termto that term, clearvoted_for, and become a follower. This applies to every role, including leaders. - Lower term in a request. Reject the request and reply with your own
current_term, so the sender learns it is out of date. - Lower term in a reply. The reply belongs to an old request. Ignore it.
If the terms are equal, process the message normally.
The reason for clearing voted_for in rule 1 is that the vote is per term. Having voted for someone in term 5 says nothing about term 6. Without the reset, a server would be unable to vote in the new term, and elections could stall.
Here is a small runnable model of these rules.
from dataclasses import dataclass
from typing import Optional
@dataclass
class Server:
id: int
current_term: int = 0
voted_for: Optional[int] = None
role: str = "follower"
def observe_term(s, term):
"""Rule 1: apply to every incoming request and every reply."""
if term > s.current_term:
s.current_term = term
s.voted_for = None
s.role = "follower"
return True
return False
def start_election(s):
"""A follower or candidate whose election timeout expired."""
s.role = "candidate"
s.current_term += 1
s.voted_for = s.id # a candidate votes for itself
return s.current_term
# Server 1 leads term 3, then gets cut off from the others.
old_leader = Server(1, current_term=3, voted_for=1, role="leader")
# Server 2 stops hearing heartbeats and starts an election.
challenger = Server(2, current_term=3)
new_term = start_election(challenger)
print(challenger)
# The partition heals. The old leader receives a message with term 4.
changed = observe_term(old_leader, new_term)
print(changed, old_leader)Running it prints:
Server(id=2, current_term=4, voted_for=2, role='candidate')
True Server(id=1, current_term=4, voted_for=None, role='follower')The old leader never needed to know why term 4 exists. The bigger number alone is enough for it to step down.
What each role does with terms
The rules above are the same for everyone, but each role has some specific consequences.
Followers
A follower resets its election timer in two cases. It does so when it receives a valid AppendEntries from the current leader, meaning one whose term is at least its own. It also does so when it grants a vote to a candidate. Messages from stale leaders do not count, and they do not reset the timer. If they did, a deposed leader could keep followers from ever starting an election.
Candidates
A candidate increments current_term and votes for itself as the first step of an election. While it waits for votes, it may receive an AppendEntries RPC from another server that claims to be leader. It then compares terms:
- If the sender's term is at least the candidate's own, the sender is a legitimate leader. The candidate returns to follower.
- If the sender's term is lower, the candidate rejects the RPC and stays a candidate.
The "at least" matters. Equal terms are enough to concede, because only one server can win a given term. If the sender's term equals the candidate's term, then the sender won that very election.
Leaders
A leader sends heartbeats to all followers on an interval much shorter than the election timeout. When a reply arrives with a higher term, rule 1 forces the leader to step down. Leaders also treat a lower-term reply as stale and ignore it.
Stale leaders in action
A leader cannot tell the difference between a quiet network and a partition. During a partition it keeps believing it is leader. This sequence shows what happens when the partition ends.
While isolated, Server 1 could not commit anything. It could not get a majority to acknowledge its entries. Any uncommitted entries it holds may later be overwritten by the new leader. A later chapter on log matching covers how that repair works.
Edge cases
- Isolated servers inflate terms. A server cut off from the majority keeps timing out and incrementing its term. It can reach term 40 while the healthy cluster stays at term 4. When it reconnects, its high term forces the healthy leader to step down, and the cluster holds an unnecessary election. Pre-vote, covered later, prevents this.
- A leader does not step down by itself. In the basic protocol, a leader that can no longer reach a majority just keeps sending heartbeats into the void. Some implementations add a check-quorum rule so that a leader steps down if it hears from fewer than a majority within an election timeout. Treat that as an extension, not part of the core paper.
- Terms are not time. Two servers cannot compare terms to find out how long ago something happened. Never use a term as a timestamp.
- Simultaneous candidates. If two servers time out at nearly the same moment, both start the same new term and each votes for itself. Neither can win with the others' votes split, so the term ends without a leader. Randomized timeouts make this rare.
- Persistence.
current_termandvoted_formust survive a crash. A server that forgot it had voted in term 5 could vote again and allow two leaders in one term. The persistence chapter explains when to write them to disk.
Common mistakes
| Mistake | What goes wrong |
|---|---|
| Not clearing `voted_for` when moving to a higher term | The server refuses to vote in the new term, so elections stall. |
| Ignoring the term on replies | A leader treats an old acknowledgement as current and miscounts replicas. |
| Candidate stays a candidate on an equal-term AppendEntries | Two servers act as leader-eligible in one term, which breaks the single-leader argument. |
| Resetting the election timer on any message | A stale leader's heartbeats prevent a needed election. |
| Using wall-clock time to decide which message is newer | Clock skew produces wrong decisions. Compare terms instead. |
Summary of the model
- A server is a follower, candidate, or leader.
- Followers are passive. Candidates ask for votes. Leaders replicate the log.
- A term is a monotonically increasing integer that acts as a logical clock.
- Each term has at most one leader, because each server votes once per term and majorities overlap.
- A higher term observed anywhere makes a server a follower and clears its vote.
- A lower term in a request is rejected. A lower term in a reply is ignored.
Exercise
A candidate in term 6 receives an AppendEntries RPC with term 6 from another server. A second candidate in term 6 receives an AppendEntries RPC with term 5. What should each one do, and why?
Show answer
The first candidate should return to follower. The sender holds term 6, which equals its own, and only one server can win a given term, so the sender is the legitimate leader. The second candidate should reject the RPC and reply with its own term. A term of 5 means the sender is a stale leader, and the reply lets it learn that and step down.
Exercise
Server A is a follower at term 8 and has voted for server B in term 8. It then receives a RequestVote with term 9 from server C. Before any log comparison, which fields on A change, and what would break if voted_for were not cleared?
Show answer
A sets current_term to 9, sets voted_for to none, and stays a follower. If voted_for still pointed at B from term 8, A would believe it had already used its term-9 vote. It could refuse to vote for C or anyone else, and elections in term 9 could stall.