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 Consistent hashing

⭕ Consistent hashing: cheat sheet

🎨 The analogyPlates around a round table: when someone sits down or leaves, only their neighbours' plates change hands.
🎓 The regular explanationA way to spread keys across servers so that adding or removing a server moves only a small share of keys. Servers and keys are hashed onto a ring, and each key belongs to the next server clockwise.
Counting off seatsStep 2 of 2
👥Guestsmost keys move🍽️Table 0🍽️Table 1🍽️Table 2🍽️New table 3
  1. Guests are seated by their ticket number. Divide by the number of tables and use the remainder. Ticket 10 goes to table 1.
  2. A fourth table is added. Now you divide by 4, and almost every guest gets told to move.
The round tableStep 3 of 3
👩Ana👨Ben60°–120°🧑Cy👱Dee0°–60°🍰A plate of cake
  1. People sit around a round table. Each plate is put down at a spot on the table and slides clockwise to the next person.
  2. This plate lands just after Ana, so it slides round to Ben.
  3. Dee sits down between Ana and Ben. Only the plates in that gap move, from Ben to Dee. Everyone else keeps theirs.
Several seats eachStep 3 of 3
✕ ✕💫Ana👨Ben50%🧑Cy50%⭕Round table
  1. With one seat each, someone may end up next to a big gap and get far more plates than the others.
  2. So everyone sits in many seats spread around the table. Lots of small gaps even out, and everyone gets about the same.
  3. When Ana leaves, her plates are shared between Ben and Cy, not dumped all on one person.
A node diesStep 5 of 5
🤵Waiter✕ ✕💫Ana👨Benwarm🥫 Pantry
  1. Ana has only one seat. Normally the waiter asks her for her plate of cake, and she has it ready.
  2. Ana leaves in a hurry. The waiter asks her and gets no answer.
  3. Her plates slide clockwise to Ben. Ben has none of that cake on his plates yet, so he has to say no.
  4. Ben runs to the pantry for every missing cake, so the pantry gets swamped. Ben's plates slowly fill up again.
  5. Ben's plates are full again, so the waiter gets cake straight away. The cost was a rush on the pantry.

🧩 Key questions

You seat guests by dividing their ticket number by the number of tables. What goes wrong when you add a table?
Almost everyone has to get up and move. Dividing by a different number changes nearly every remainder.
A new person sits down at the round table. Whose plates change hands?
Only the plates in the gap just before the new person. Everyone else keeps their plates.
Why does everyone take many seats around the table?
So the gaps even out, and a leaver's plates are shared by everyone. With one seat each, someone might sit next to a big empty stretch.
Ana leaves the round table and has only one seat. Her plates all slide to Ben, who has none of that cake ready. What happens?
Ben takes extra plates, says no at first, and the pantry gets swamped until his plates fill up again. The cake is still in the pantry, but one person is doing double work and all the asking goes to the pantry.