Sub topic 3
Followers, candidates, leaders, and terms
This chapter assumes you know what a replicated state machine is and why a majority quorum tolerates crashes. It introduces the three roles a Raft server can hold and the logical clock, called a term, that orders them. Later chapters build elections and replication on top of these.
The three roles
At any moment each server is in exactly one of three roles.
- A follower is passive. It answers requests from leaders and candidates and issues none of its own.
- A candidate is a server that is trying to become leader. It asks the others for votes.
- A leader handles all client requests and replicates log entries to the followers. A healthy cluster has one leader and the rest are followers.
Every server starts as a follower. If a follower hears nothing from a leader for one election timeout, it assumes the leader is gone and becomes a candidate.
Terms as a logical clock
Raft does not rely on synchronized wall clocks. It divides time into terms, which are numbered with consecutive integers. A term begins with an election. If a candidate wins, it stays leader for the rest of that term. If the vote splits and nobody wins, the term ends with no leader and a new term starts shortly after.
Each server stores a current_term. Servers include it in every message they send. This gives three rules for comparing terms:
| Situation | What the receiver does |
|---|---|
| Message term is greater than my term | Adopt the larger term, clear my vote, and become a follower |
| Message term equals my term | Process the message normally |
| Message term is smaller than my term | Reject the request and reply with my term |
The first rule is the important one. A stale leader that was partitioned away learns it is out of date the first time it hears a higher term, and it steps down at once. A candidate does the same when it sees a leader with a term at least as large as its own.
At most one leader per term
Each server may cast one vote per term, and it records that vote in voted_for. A candidate needs votes from a majority of the cluster. Two different majorities of the same cluster always share at least one server. That shared server voted only once in the term, so two candidates cannot both win the same term.
This guarantee covers one term only. Different terms can have different leaders, and an old leader may briefly still believe it leads. Terms are how the cluster detects and resolves that.
Per-server term state in code
The sketch below shows the term rule that every RPC handler applies first. The class and method names are for illustration. The later chapters on RequestVote and AppendEntries use the same logic.
class Server:
def __init__(self):
self.current_term = 0
self.voted_for = None
self.role = "follower"
def observe_term(self, term):
"""Apply the term rule to any incoming request or reply.
Returns True if the message is stale and should be rejected.
"""
if term > self.current_term:
self.current_term = term
self.voted_for = None # a new term means a fresh vote
self.role = "follower" # step down if leader or candidate
return False
return term < self.current_term
s = Server()
s.role = "leader"
s.current_term = 3
print(s.observe_term(2), s.role) # True leader (stale message, ignored)
print(s.observe_term(5), s.role) # False follower (higher term, step down)
print(s.current_term, s.voted_for) # 5 NoneRunning this prints True leader, then False follower, then 5 None.
Common errors
- Forgetting to clear
voted_forwhen the term increases. The server then refuses to vote in the new term, or votes twice across terms in a way that breaks bookkeeping. - Handling a message before comparing terms. Always apply the term rule first, so a stale leader cannot keep acting on old authority.
- Treating a term as a count of elections that succeeded. Terms can pass with no leader at all.
- Letting a leader keep serving after it sees a higher term. It must step down immediately.
Exercise
A leader in term 4 is cut off from the network for a while. The other servers elect a new leader in term 5. When the partition heals, the old leader sends an AppendEntries request with term 4 to a follower. What does the follower do, and what happens to the old leader when it receives the reply?
Show answer
The follower's current term is 5, which is greater than 4, so it rejects the request and replies with term 5. When the old leader sees term 5 in the reply, it adopts that term, clears its vote, and steps down to follower.