Term — the logical clock

Raft replaces the wall clock with a simple round counter called the term, and one universal rule — when any server sees a bigger round number than its own, it adopts that number and becomes a plain follower — is what keeps the whole cluster's sense of time honest.

Previously

You know from scene 01 that safety has to hold even when messages arrive late, out of order, or not at all — and that the wall clock can't be trusted. So before Raft can do anything else, it needs its own notion of time: a counter the whole cluster can agree on without asking a clock. That counter is what this scene builds.

Scene 02

Term — Raft's round counter

  1. Watch
  2. Try it
  3. Predict
  4. Capture
S1LEADERT:1vote:S11t12t1S2FOLLOWERT:1vote:S11t12t1S3FOLLOWERT:1vote:S11t12t1✓AppendEntries · T1✓AppendEntries · T1term colors: 1 blue · 2 teal · 3 amber · 4 violet · 5 roseAppendEntriesheartbeatRequestVoteInstallSnapshot▲commitIndexCalm. We are on round 1. S1 is the one server in charge; S2 and S3 are just listening. All three agree we are still in round 1.
currentTerm — each server's own copy of 'what round do I think we're on?' →
S1 is the one leader of round 1; S2 and S3 are followers (just listening) →
What to watch for

You know from scene 01 that the cluster has to agree on a history without trusting the wall clock. The question this scene answers is: what does Raft use instead? Look at the three servers. They all say they are on round 1. One server (S1) is currently in charge — it is allowed to make decisions for round 1 and tell the others what to do. The other two are just listening. Three new words for what you see. First, the round counter itself: think of it like the season number of a TV show — once you start season 4, season 3 is over forever. The precise term is term — a monotonically increasing integer, shared by the whole protocol, where at most one server is ever in charge during a single term. Second, each server keeps its own copy of 'what season do I think we are in?' — the precise name for that field is currentTerm. Third, the role each server is playing right now: passive listener is called a follower, the one in charge is called the leader, and (you'll meet this one next phase) a server running for the job is called a candidate.

Implementation

Highlighted lines are the ones running in the diagram right now.

Server.onReceiveRPC
universal step-down — runs at the TOP of every RPC handler
def onReceiveRPC(rpc):
if rpc.term > currentTerm:
currentTerm = rpc.term # adopt newer term
votedFor = None # clear vote for new term
role = follower # step down
persist(currentTerm, votedFor) # §5.2 persist barrier
if rpc.term < currentTerm:
reply(term=currentTerm, success=False)
return
# rpc.term == currentTerm: dispatch to kind-specific logic
Server.state
per-server state — every leader / candidate / follower holds these
# Persisted (fsync'd before responding to dependent RPCs):
currentTerm : monotonic int # latest term observed
votedFor : ServerId | None # vote granted in currentTerm
log : list[{term, cmd}] # replicated entries, term-tagged
# Volatile on every server:
commitIndex : int = 0
lastApplied : int = 0
# Volatile on leader only:
nextIndex : dict[ServerId, int]
matchIndex : dict[ServerId, int]

Where this sits in Build Raft — consensus you can defend

Scene 02 of 12. A term is a monotonic int that segments time, with at most one leader per term. The universal step-down rule (any server seeing T' > currentTerm becomes a follower at T') is the simplest mechanism in Raft and underpins everything else.

Up next. Now that every message and every server carries a round number, the question 'who is the leader right now?' simplifies to 'who got enough votes in round T?' — a counting problem, which the next scene answers using one fact about majorities.

All 12 scenes in Build Raft — consensus you can defend · Every curriculum

Built with Arqly
Every scene in Build Raft — consensus you can defend builds on the one before it.All 12 Build Raft — consensus you can defend scenes