CS PAPERS DEEP-READ · PAPER 74

Consistent Hashing and Random Trees

Karger, Lehman, Leighton, Levine, Lewin & Panigrahy · MIT · STOC 1997

中文 →

What did this paper do?

In 1997 a group of theorists at MIT (Karger and colleagues) set out to cure a plague of the early web — a page suddenly goes viral and the server buckles. Their answer was consistent hashing: a rule for deciding which machine holds which thing, whose virtue is that adding or removing machines almost never reshuffles everything. A year later two of the authors founded Akamai on it. The videos you stream, your Amazon cart, databases like Cassandra — when they decide which machine a piece of data lives on, they still use this idea.

The old pain: add one machine, everybody moves

To split many things across a few machines, the natural move is to divide by the count and take the remainder: turn the name into a number, divide by how many machines you have, and whoever owns that remainder owns the item. With ten machines everyone gets a share, fairly evenly.

The trouble starts when the count changes. Add an eleventh machine and the divisor goes from 10 to 11, so nearly every item changes hands — the whole cluster shuffles data at each other, the network saturates, and nobody gets any real work done. All you wanted was one more machine.

The idea: put machines and data on the same clock face

Consistent hashing lays things out differently. Picture a clock face. Every machine computes a position from its own name and is pinned there; every piece of data computes a position from its own name too. One rule: a piece of data belongs to "the first machine you meet walking clockwise from its position."

Why that stops the mass migration

The key is that positions are computed independently: where an item lands on the clock face depends only on its own name and not at all on how many machines exist. So adding a machine just means driving a new pin into the face: it takes over only the items in the short stretch immediately behind it, from the neighbour clockwise of it, and nothing anywhere else on the face changes by a single character. Losing a machine is the mirror image — its stretch passes to the next pin along. Every change is confined to one small arc.

There was another benefit that mattered even more at the time: different browsers knew about different sets of caches. Under "divide and take the remainder," a list that differs by one machine gives completely different answers; under the clock-face rule, lists can disagree slightly and almost all data still lands on the same machine — so nobody has to agree in advance to end up agreeing.

One machine needs many pins

A single pin per machine has a side effect: the arcs the face gets cut into vary wildly, so an unlucky machine minds a huge stretch while a lucky one minds a sliver. Uneven work.

The fix is crude: give every machine dozens or hundreds of pins (many "alter egos" on the face). With that many slices, long and short cancel out and each machine's total comes out about the same. A bonus: when a machine goes offline, its dozens of stretches are picked up by many different machines rather than dumped whole onto one unlucky neighbour.

One honest caveat: what it evens out is the number of items, not how often they're wanted — a single record the whole internet is hammering still lives on exactly one machine, and can still flatten it.

Remember one thing

Don't assign by "divide by the machine count and take the remainder." Pin both machines and data onto one clock face by name, and give each item to the first machine clockwise. Adding or removing a machine then disturbs only a small arc — migration drops from "nearly everything" to "about one machine's worth" — and scattering many pins per machine evens out the load.

Want the ring diagram, the four properties, and the guarantees? → switch to the deep read