Let us start with a problem you may meet in your first year of working on a busy system. You have four cache servers. Each key, say a user ID, has to live on exactly one of them so that every application server knows where to look. The obvious rule: hash the key to a number, divide by four, take the remainder. Remainder 0 goes to server 0, remainder 1 to server 1, and so on. It is fast, needs no lookup table, and spreads keys evenly. You would be forgiven for stopping there.

The day you add a fifth server

Traffic grows and you add a fifth machine. The rule now divides by five. Look at what happens to a key whose hash is, say, 13. Divided by four, the remainder is 1. Divided by five, it is 3. The key has a new home, and the new server has never seen it. Every application server now looks in the wrong place, gets a miss, and goes to the database.

How many keys are affected? I checked over a long run of hash values: going from four servers to five, only one key in five keeps its old server. Eighty percent of your cache becomes effectively empty in the same second. If that cache was shielding the database from most of the load, the database now takes five times the traffic it was used to. Teams have brought down their own databases while “just adding capacity”.

The same thing happens in reverse when a server crashes: going from five to four reshuffles most keys, right when you are already having a bad day.

Draw a circle instead

Consistent hashing changes the geometry. Imagine a circle with positions numbered 0 to 99 (real systems use a vastly bigger range, but the idea is the same). Hash each server name to a position on the circle. Say server A lands at 10, server B at 40 and server C at 75. Now hash each key to a position as well. The rule is: walk clockwise from the key until you hit a server. That server owns the key.

So keys at positions 11 to 40 belong to B. Keys from 41 to 75 belong to C. Keys from 76 around the top through 10 belong to A, since after 99 the circle wraps back to 0. Each server owns the stretch of circle just behind it.

Now add server D, which hashes to position 60. Which keys change owner? Only those between 41 and 60, which used to walk clockwise to C and now meet D first. That is 20 of the 100 positions. A and B are untouched; C loses part of its range and keeps 61 to 75. Everything else stays put. If a server dies, only its own keys move, to the next server clockwise. The rest of the cache survives.

In general, adding the Nth server moves roughly 1/N of the keys, which is about the minimum you could possibly move, since the new server needs its fair share from somewhere. Compare that with the 80 percent from before.

The unevenness problem

Look again at the circle. A owns 76 to 10, which is 35 positions. B owns 30. C owns 35. That happens to be close, but with three random points it easily might not be. One server could end up with half the circle. And when a server dies, its whole range lands on a single neighbour, which may double that neighbour’s load.

The standard fix is virtual nodes. Instead of placing each server once, place it many times, perhaps a hundred or more, under names like “A-1”, “A-2” and so on. Each machine now owns many small slices scattered around the circle. Averages smooth out, and when a machine leaves, its slices go to many different neighbours instead of one. Bigger machines can be given more virtual nodes so that they take proportionally more traffic.

Where it is used

The idea was published in 1997 in a paper about distributed caching on the web, and it became well known through Amazon’s Dynamo design. Variants appear in Cassandra’s token ring, in client libraries for memcached, and in load balancers that want requests for the same key to keep reaching the same backend. Many systems also replicate by giving each key to the next few distinct servers clockwise, so a copy survives if the first owner fails.

What it does not do

It balances keys, not load. If one key is enormously popular, say a product page on a sale day, it still lands on one server, however neat the circle. That is a hot key problem and needs a different answer, such as replicating that key or caching it locally. It also does not move data by itself: when a server joins, someone must still copy over the keys it now owns, or accept misses until the cache refills.

And if your cluster size never changes, plain modulo is perfectly fine. Reach for the ring when machines come and go, because that is exactly the situation where modulo hurts most.

Figures and names in the examples are illustrative.

Leave a Reply

Trending