Saral Shiksha Yojna
Courses/Distributed Systems

Distributed Systems

CS3.401
Prof. Kishore KothapalliMonsoon 2025-264 credits

Leader Election + Log Replication + Safety

NotesStoryCaveman
Unit 9 — Raft Consensus

Chief fall, tribe pick new chief, every cave keep same story

Raft — tribe-magic that keeps many caves telling the SAME story even when caves fall down. Wise-heads Ongaro and Ousterhout carved it in 2014. Before, the tribe used Paxos — old magic, correct but head-hurt. Raft does the SAME correct thing, only simple. Ugg like simple.

Big idea: a replicated state machine — many caves each keep the same wall of scratches (the log) and run the same commands in the same order, so to the hunter outside the tribe looks like ONE cave. The tribe keeps moving forward as long as a majority of caves still stand.

What caves may do wrong

Raft handles only fail-stop trouble: a cave can DIE, and grunt-messages can be lost, late, or out of order. But caves NEVER LIE. Ugg warn: do NOT say "Raft handles liar caves!" Liar caves are Byzantine trouble, needing other magic (PBFT, HotStuff).

Three hunts in one

Raft splits the big problem into three small hunts — carve these three:

  • Leader election — pick ONE chief, notice when chief dies, pick a new one.
  • Log replication — chief takes the hunter's command, scratches it, copies it to every cave.
  • Safety — never lose a scratch already carved in stone.

Every cave wears exactly ONE skin: Leader (chief — beats drum, takes commands, feeds the rest), Follower (copies chief quietly), Candidate (wannabe-chief, only during a vote).

Pick the chief

Every follower holds an election timeout — a sand-timer of RANDOM length (about 150–300 ms). The chief beats a heartbeat (an AppendEntries carrying no new scratch) to say "me alive", and each drumbeat flips the sand-timer fresh. If the sand runs out with no drumbeat, the follower thinks the chief dead and:(1) becomes Candidate.(2) adds one to its term, the season-count — new chief, new season.(3) votes for self.(4) shouts RequestVote to every cave. YES from a majority that season → chief, and it starts drumming.

Ugg warn: if every sand-timer were the same length, many caves wake together, all shout "chief me!", votes split, nobody wins — livelock. RANDOM timers fix it: one cave usually wakes first and wins before the rest stir.

Term is the season-number: always climbs, one chief per season. Ugg law: any cave that sees a BIGGER season in ANY grunt drops to Follower and takes that number — this is how a stale old chief who wandered back bows down. Do not confuse the term (which election) with the index (which spot on the wall); each scratch carries both.

Copy the story

Hunter brings a command to the chief:(1) chief scratches it on its wall, soft, not yet carved.(2) chief sends AppendEntries to every cave at once.(3) when a majority have scratched it too, the chief commits — carves it in stone, runs the command, tells the hunter "done".(4) the next drumbeat tells followers "carve up to here" and they carve too. If a cave stays silent, the chief keeps retrying forever.

Make the walls match

Every AppendEntries carries the index and season of the ONE scratch right BEFORE the new ones. The follower checks for that matching scratch. MATCH → take the new scratches (rubbing out any wrong later ones). NO MATCH → say NO; the chief steps nextIndex back one and tries an older scratch, backing up until the walls agree, then the chief's scratches overwrite the follower's wrong marks — the chief's wall is the truth.

Never lose carved stone

The key safety rock is the election restriction: a cave grants its vote ONLY if the wannabe-chief's wall is at least as up-to-date as its own.

Up-to-date means: compare the LAST scratch's season; bigger season wins; tie → the LONGER wall wins. Why it saves the tribe: any carved stone lives on a majority, any new chief won a majority of votes, and two majorities ALWAYS share a cave —

— so that shared cave holds the stone, and the restriction guarantees the new chief's wall is at least as fresh. So the new chief HAS every stone from every past season; nothing committed is ever lost.

Count a majority right: . For that is 3 — NOT "half". Ugg warn: no say "half the caves"!

The old-season trap (Figure 8)

Ugg warn loud: a new chief must NOT carve an OLD-season scratch just because it sits on a majority. Bad tale: season 2, chief copies scratch to a majority, then dies before carving. Season 3, a new chief lacking copies . Season 4 the old chief wakes, sees on a majority, carves it — but may already be carved elsewhere → two truths at one spot, tribe splits.

Safe law — a scratch is committed only when replicated to a majority IN the current season:

The new chief carves an old scratch ONLY by carving a fresh this-season scratch on top; that transitively carves the old ones under it.

Keep the wall short, grow the tribe

Log compaction squashes old carved scratches into one snapshot of the now-state; a new cave gets the snapshot instead of re-walking every footprint, so the wall stays short. Joint consensus handles changing tribe size (3 → 5): for a while a decision needs YES from BOTH the old-majority AND the new-majority, so two separate majorities can never form and fight; once that joint step commits, switch to the new tribe alone.

Real caves run Raft: etcd, Consul, CockroachDB, TiKV.

Ugg remember

  • Three hunts: election + replication + safety. Three skins: Follower / Candidate / Leader. Raft is fail-stop — no liar (Byzantine) caves.
  • Random timeout → Candidate → majority vote → chief. See a bigger term → bow to Follower.
  • Commit = scratch on a majority in THIS season, then carve and answer the hunter. Majority is , never half.
  • AppendEntries: prev index + season must match, else the chief backs up and overwrites the follower. Election restriction: the fresher-or-equal wall wins the vote — this is what saves every carved stone.
  • NEVER carve an old-season scratch straight (Figure 8) — carve a new one on top so the old carves safely.
End of storyUnit 9 — Raft Consensus · Leader Election + Log Replication + Safety