Log inStart free
LESSON 33.07 · 5 STAGES · 3 CONTROLS · 2 CHECKSPRO · PREVIEW

Consensus and leader election

Watch five nodes elect a leader, commit a write and survive a crash.


Pro lesson. Stage 1 is free to watch. Pro opens all 5 stages, 3 controls and 2 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 33.07 · SESSION STORE5 NODES · MAJORITY 3 · 0 DOWN
01
Stage 1 of 5 · Election timers
Each node starts as a follower with a random timer. N2's timer runs out first, at 179 ms.
Explain it simply

Each node waits to hear from a leader, the one node that takes the writes. The ochre ramp on each lane is its timer running down. Each node picks its own wait between 150 and 300 milliseconds. So one node, N2, gives up first and asks to lead.

5 lanes, one per node. Timeouts: N1 224 ms, N2 179 ms, N3 297 ms, N4 180 ms, N5 252 ms.

followercandidate, timerleader, votewritedown, lost (dashed)refused voteheartbeat
020406080100120140160180200time, ms179 msN1follower · term 0N2candidate · term 1N3follower · term 0N4follower · term 0N5follower · term 0election timersclientlogshortest timeout: N2 = 179 ms · range 150–300 ms020406080100120140160180200time, ms179 msN1follower · term 0N2candidate · term 1N3follower · term 0N4follower · term 0N5follower · term 0election timersclientlogshortest timeout: N2 = 179 ms · range 150–300 ms
Toy: the 33.05 session store on nodes N1–N5, 15 ms per message
STAGE 1 / 5
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
Ti=150+h(i,term) mod 151 ms\sy{T}{param}{T_i} = 150 + h(i, \text{term}) \bmod 151\ \text{ms}
TN2=150+29=179 msT_{\text{N2}} = 150 + 29 = 179\ \text{ms}

Each node draws its own wait from 150 to 300 ms.

Common mix-ups

MYTHFour nodes survive more failures than three nodes.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
2 CHECKS · PRO
Q01In a cluster of 5 nodes with 2 down, does a new leader win after the crash?

Make your guess, then test it in the diagram. All 2 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 5
Unlock the full explainer
  • 4 more stages and 3 live controls
  • 2 mix-up cards and 2 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.