Log inStart free
LESSON 33.06 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Consistent hashing

Watch 24 sessions find their nodes on a hash ring as nodes join and leave.


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

Go Pro · $8.25/mo
EXPLAINER · 33.06 · RING BAYMOD 4 · 24 SESSIONS ON 4 NODES
01
Stage 1 of 6 · Hash mod n
Each session goes to node hash mod 4. N1 to N4 hold 3, 11, 6 and 4 sessions.
Explain it simply

A hash function turns each session's id into a big number. Divide it by 4 and keep the remainder: 0, 1, 2 or 3. The remainder picks the node, like dealing cards into 4 piles. Each chip is one session, and its height shows its size in MB.

Four node columns, N1 to N4, stack 24 session chips placed by hash mod 4. The tallest stack is N2.

sessionsnodes, positions, 64 MB lineownermoved, or over 64 MBnode down (grey box)
64 MB nodeN1mod 4 = 026 MBN2mod 4 = 150 MBN3mod 4 = 234 MBN4mod 4 = 330 MBS01S02S03S04S05S06S07S11S14S17S20S21S22S23hash mod nnodes by hash mod 4node = h(id) mod 464 MB nodeN1mod 4 = 026 MBN2mod 4 = 150 MBN3mod 4 = 234 MBN4mod 4 = 330 MBS01S02S03S04S05S06S07S11S14S17S20S21S22S23hash mod nnodes by hash mod 4node = h(id) mod 4
Toy: 24 sessions (140 MB) on N1–N4, 64 MB each; spare N5
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
node=h(id) mod n\sy{o}{out}{\text{node}} = \sy{h}{data}{h(\text{id})} \bmod \sy{k}{param}{n}
h(S09)=2791906933, 2791906933 mod 4=1→N2h(\text{S09}) = 2791906933,\ 2791906933 \bmod 4 = 1 \to \text{N2}

The remainder of the hash divided by n picks the node.

Common mix-ups

MYTHThe ring gives every node an equal share.

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

Predict, then reveal
3 CHECKS · PRO
Q01At 16 positions per node, do N3's sessions all go to one node?

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

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 3 live controls
  • 2 mix-up cards and 3 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.