BOOK DEEP READ · System Design Interview Vol 1 · Ch 5

Design Consistent Hashing

System Design Interview: An Insider's Guide · Vol 1 · Ch 5 · Alex Xu · 2020

中文 →

What is this chapter about?

Your parcels pass through a sorting hub. A handful of warehouses sit there, and every parcel is routed to one of them by its tracking number. The sorting rule has to do two things: be fast (one glance at the number tells you the warehouse — no ledger lookup) and be stable (next time you look for that parcel, the rule sends you to the same warehouse). This chapter is about that rule, and about what happens to it when the number of warehouses changes.

Something strange first

You would assume that going from four warehouses to five means moving roughly a fifth of the goods into the new one. With the most common rule, you have to physically move about eighty percent of everything — and not into the new warehouse, but from one old warehouse to a different old warehouse. Open one building, and the whole city's freight has to move.

Why the old world was hard

That common rule is: divide the tracking number by the number of warehouses and look at the remainder. Four warehouses, remainders 0 to 3, one each. Simple and fast. The trouble is that "the number of warehouses" is baked into the formula. Go from four to five and the divisor changes, so nearly every parcel gets a different answer. They do not mostly stay put with a few moving to the new building — they all shuffle at once.

The consequence is immediate, because the sorting hub is really a cache. Normally almost everyone is served nearby; the moment the warehouse count changes, everybody looks in the wrong place, and the requests slam into the slow, expensive central store behind it. What kills the system is not the machine you added — it is the move itself.

The core idea: stand the warehouses in a circle

Try another way. Imagine every possible tracking number laid out on a circular running track whose end joins its start. Each parcel lands somewhere on the track according to its number; each warehouse also lands somewhere according to its name. The rule becomes one sentence: walk forward from where the parcel landed, and the first warehouse you meet owns it.

The beauty is that "how many warehouses" has vanished from the formula. Opening one is just dropping a new point onto the track — only the parcels lying in the short stretch behind that new point have to move, and everything else on the track stays exactly where it is. Closing one is the mirror image: its stretch is handed forward to the next warehouse, and nobody else notices.

It still needs a patch

That alone is not even enough. Warehouses land on the track at random, so three may cluster together and leave one enormous gap belonging to the fourth — which then gets crushed. The patch is crude and effective: let every warehouse open dozens or hundreds of "branches" on the track, each with a numbered name and its own landing spot. With enough branches, the long and short stretches get chopped up and averaged out, so each warehouse ends up with roughly its fair share. Those branches are called virtual nodes.

One honest cost: more branches is not simply better — the more little stretches a warehouse's goods are scattered across, the more neighbours it is entangled with, so the day two warehouses fail together, the damage reaches further.

Remember this one line

The old rule wrote "number of warehouses" into the formula, so changing that number moves the whole city. Consistent hashing stands the warehouses in a circle and lets each parcel take the nearest one clockwise, so adding or removing one disturbs a single stretch. Then give every warehouse a pile of branches to even out the uneven arcs.

Want the algorithm, the numbers and the diagrams? → Switch to the deep read