Add one server and a naive database moves most of its data. Here is the ring trick that moves only a small slice instead.
Imagine a library with 3 shelves, where a rule picks the shelf for each book. Add a fourth shelf and the rule changes, so most books have to move. That's roughly what a badly built database does when you add one more server.
The fix has a scary name, consistent hashing, but the idea is small: when you add a server, only a small slice of the data should move, not everything. Let's break it first, then fix it.
You start with one server. All the data lives there, and life is simple. Then it gets slow, or the disk fills up. You can buy a bigger machine, but there's a ceiling on how big one machine gets.
So the standard fix is sharding: split the data into pieces and give each piece to a different server (each piece is a shard). The app needs one rule: this key lives on which server?
Hash mod N. A hash function turns any text into a number. Same text, same number, every time. To pick a server, take the number and use the remainder after dividing by the number of servers (that's "mod").
It spreads keys evenly and needs no lookup table. So what's wrong with it?
Say you have 3 servers and add a 4th. Now every key is divided by 4 instead of 3, so the answers change.
Try it yourself. Click "Add a server" and count the key chips that get highlighted as they move. Guess the number first.
Key mod 3: 12 keys on 3 servers. Add a server to see what moves.
Here's the arithmetic (my own toy example, not from a paper). With 3 servers, key k goes to k mod 3. With 4, it goes to k mod 4. Only keys 0, 1 and 2 land on the same server both times. The other 9 of 12 move.
That's 75% of the data moving in this toy case, just because one server joined. It's like a classroom where adding one desk makes almost everyone change seats.
Now flip "Use a ring" and add a server again. Fewer chips move. Why? Next section.
Karger and colleagues, in a 1997 paper about relieving "hot spots" on the web, called the better idea consistent hashing. Their abstract describes a consistent hash function roughly as one that changes minimally as the range of the function changes.
Picture the hash numbers bent into a circle (a "ring"), where the biggest number wraps back around to the smallest. The Dynamo paper describes it this way. Click Next and follow one key.
Hash the key to get its spot on the ring
Notice the rule is only: walk clockwise until you hit a server. Each server owns the stretch of ring between it and the server before it. Now add a server. It lands in one gap and takes over only that gap's keys, from its one neighbour. The Dynamo paper puts it plainly: the principal advantage is that a node arriving or leaving only affects its immediate neighbors.
Add a server, move only one slice of keys.
Add a server, most keys change owner.
So we're done? Not quite.
Nope. Random spots can land close together, so one server gets a huge slice and another gets a tiny one. The Dynamo authors list this as a problem: random placement gives non-uniform data and load distribution. They also note the basic version ignores that some machines are stronger than others.
It's like cutting a pizza into uneven slices. Some people get big slices and some get tiny ones.
The fix in Dynamo: instead of one spot, each real server gets many spots on the ring. A virtual node looks like a single node to the rest of the system, and one machine can be responsible for several.
One of many positions on the ring that belong to the same physical server.
Dynamo's paper lists the payoff:
Think of many thin slices of pizza per person instead of one big wedge. The totals even out. Is a ring the only way to do this?
Redis Cluster takes a different route. Per its docs, there are exactly 16384 hash slots. A key's slot is the CRC16 of the key modulo 16384, and each node owns a range of slots.
| Node | Slots (from the Redis docs example) |
|---|---|
| A | 0 to 5500 |
| B | 5501 to 11000 |
| C | 11001 to 16383 |
To add a node D, Redis moves some slots from A, B and C over to D. Per the docs, moving slots doesn't require stopping operations, so adding or removing nodes needs no downtime. Same idea as the ring (move small pieces), but the pieces are fixed boxes you can count.
Try these on paper: