TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to Consensus (Raft)

๐Ÿ—ณ๏ธ Consensus (Raft): cheat sheet

๐ŸŽจ The analogyA club choosing a captain and keeping one official notebook, where a decision counts once most members copy it.
๐ŸŽ“ The regular explanationHow a group of servers agrees on one ordered history even when some crash. Raft elects a leader by majority vote, replicates a log, and commits an entry once most servers have it.
Choosing a captainStep 4 of 4
๐Ÿ‘ฉAnafollower๐Ÿ‘จBenleader, term 1๐Ÿง‘Cyfollower
  1. Three club members share one notebook of decisions. Right now nobody is captain, and each has an egg timer running, set to a slightly different time.
  2. Ben's timer rings first. He says "I'll be captain for round 1" and votes for himself.
  3. Ana and Cy haven't voted this round, so they both say yes. Three out of three is a majority.
  4. Ben is captain. He keeps sending short "still here" notes, so nobody else's timer runs out.
Writing it down togetherStep 4 of 4
Ben's notebookFrikey trip = Friday: Fri0123committedAna's notebookFrikey trip = Friday: Fri0123committedCy's notebookFrikey trip = Friday: Fri0123committed๐Ÿ™‹Club member๐Ÿ‘จBen (captain)๐Ÿ‘ฉAna๐Ÿง‘Cy
  1. A member asks the captain to record "trip on Friday". Only the captain takes requests.
  2. The captain asks everyone to copy the line into their own notebook.
  3. Once most of the club has it written down, it's official. The captain marks it agreed and tells the member.
  4. With the next "still here" note, the others learn it's official and mark it agreed too.
When the captain disappearsStep 4 of 4
๐Ÿ‘ฉAna๐Ÿ‘จBenfollower๐Ÿง‘Cyleader, term 2
  1. Ben is captain for round 1, sending his "still here" notes.
  2. Ben falls ill. The notes stop, and the others' egg timers keep running down.
  3. Cy's timer rings first. He stands for round 2, and Ana votes yes. Two out of three is still a majority, so Cy is captain.
  4. Ben comes back still thinking he's captain. He sees round 2 on Cy's note, realises he's out of date, and becomes a regular member again.
Losing a majorityStep 5 of 5
๐Ÿ™‹Visitorโœ• โœ•๐Ÿ’ซAna๐Ÿ‘จBen๐Ÿง‘Cy๐Ÿ‘ฉโ€๐ŸฆฐDeeleader, term 5๐Ÿ‘ดEli
  1. Five members, with Ana as captain. A visitor's request is copied down by most of the club, so it is official.
  2. Ana, Ben and Cy all fall ill at once. Only Dee and Eli are left, and nobody is in charge.
  3. The visitor asks Dee for something new. Dee stands for captain, but two votes out of five is not most of the club. Nothing is written down, and the visitor waits.
  4. Ben and Cy get better. Now Dee can reach three others, which is most of the club.
  5. The visitor tries again. Dee gets it copied by most of the club, so it is official. The wait is over.

๐Ÿงฉ Key questions

How does someone become captain of the club?
By getting votes from most of the club in a round. Each member votes once per round, so there can't be two captains.
When does a new line in the notebook become official?
Once most of the club has copied it down. Then it's never lost: any future captain must be someone who has it.
The old captain comes back after being ill and sees a newer round number. What does it do?
Quietly steps down. The newer round wins, so the club never has two captains at once.
Three of the five club members fall ill. What happens to new requests?
Nothing new becomes official until enough members are back, but what is already written stays true. Two out of five is not most of the club, so nobody can be chosen and nothing can become official. The club is stuck, not wrong.
A captain cut off by a snowstorm keeps taking requests. What happens to them?
They never become official, because most of the club can't copy them. Official needs most of the club, and the others have moved on to a new captain.