Want the details?
What problem does it solve?
A big cache or database is split over many servers. For each key (a user id, a page address) we need a rule that says which server holds it, and every client must use the same rule. The first rule most people write is hash(key) mod N: turn the key into a number and take the remainder after dividing by the number of servers.
It works well until N changes. Add one server, and the remainder changes for most keys, so most keys must move. In a cache this means most lookups miss at once and fall through to the database; in a database it means copying most of the data. Consistent hashing is a rule where adding or removing a server moves only about 1 / N of the keys.
How to read the bench
| Part on the bench | What it means in a real system |
|---|---|
| Turntable | The hash ring: every hash value from 0 to 359 is a place on its edge. |
| Dots on the edge | Where the 24 keys land when hashed. |
| Posts with letters | Servers, placed by hashing their name. Shown only for the ring method. |
| Small pegs | Virtual nodes: 15 more places for each server. |
| Bins at the front | What each server holds. A dark letter means the server is off. |
| Lifted ball | A key that has to move to another server after a change. |
The numbers
- 24 keys, named user-1 to user-24; servers A to E. Hash: FNV-1a with MurmurHash3's final mixing step, taken modulo 360.
- mod N, A B C → add D: 16 of 24 keys move (in theory 3 of 4 do).
- Ring with one place per server: loads 1, 10 and 13. Adding D moves 2 keys.
- Ring with 16 virtual nodes: loads 8, 7 and 9. Adding D moves 6 keys, all to D, and the loads become 5, 6, 7 and 6.
- With 24 keys the numbers are small and a little lumpy; with millions of keys the ring with virtual nodes moves very close to 1 / N.
The page computes everything with the same small model that the test checks, including the two rules of a ring: adding a server moves keys only to it, and removing a server moves only its keys.
In real systems
- Amazon DynamoDB and Apache Cassandra place data on a ring with virtual nodes (Cassandra calls them vnodes).
- Memcached clients (for example the "ketama" method) use a ring so that adding a cache server does not empty the whole cache.
- Load balancers and CDNs use consistent hashing to send the same user or the same file to the same server, which keeps caches warm.
- Other methods with the same goal: rendezvous hashing (each key picks the server with the highest score) and jump consistent hash (fast, but servers can only be added at the end).
Words to know
- Hash
- A function that turns any key into a number that looks random, but is always the same for the same key.
- Partition (or shard)
- The part of the data one server holds.
- Hash ring
- Hash values arranged in a circle; a key belongs to the next server clockwise.
- Virtual node
- One of several places one server takes on the ring, to even out the load.
- Rebalancing
- Moving data between servers after the set of servers changes.