System design lab · Episode 10

Consistent hashing: adding a server without moving every key

When data or cache entries are split over many servers, each key needs a server. The obvious rule, hash mod N, works until N changes: then almost every key moves. A hash ring moves only the keys next to the new server. Follow the lessons next to the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
AECBDABCDE
A key that just moved to another serverPosts on the ring: servers. Small pegs: their virtual nodesBins at the front: the keys each server holds

Controls

Method
Virtual nodes
Servers
Time

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 benchWhat it means in a real system
TurntableThe hash ring: every hash value from 0 to 359 is a place on its edge.
Dots on the edgeWhere the 24 keys land when hashed.
Posts with lettersServers, placed by hashing their name. Shown only for the ring method.
Small pegsVirtual nodes: 15 more places for each server.
Bins at the frontWhat each server holds. A dark letter means the server is off.
Lifted ballA 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.