System Design Playbook
06 · Data & databases6 min read

Consistent hashing and virtual nodes

How to spread keys across servers so that adding or losing a server moves only a small slice of the data, and why every server owns many small arcs instead of one big one.

In one sentence

Put servers and keys on the same hash ring; each key belongs to the next server clockwise. When a server joins or leaves, only the keys of one arc move, about 1/N of them, instead of almost all.

The problem with hash mod N

The obvious way to pick a server is hash(key) % N. It spreads keys evenly, until N changes. Go from 4 to 5 cache servers and about 80% of the keys now map to a different server. For a cache cluster that means a sudden wave of misses hitting the database; for a sharded database, moving most of the data.

The ring

  1. Hash every server's name to a position on a circle (0 to 2³²).
  2. Hash every key to a position on the same circle.
  3. A key belongs to the first server clockwise from it.
  4. Add a server: it only takes the keys between itself and the previous server. Remove one: only its keys move, to the next server.
A hash ring with 120 keys
Routing
ABCDKeys per serverA33B20C62D5average
Servers
4
Keys moved by last change
—
Busiest server vs average
2.07×
Keys
120
Servers and keys are hashed onto the same circle; each key belongs to the next server clockwise. Adding or removing a server only moves the keys of one arc. With one point per server the arcs are uneven; virtual nodes give every server many small arcs, so the load and the moved keys spread evenly.
Add or remove servers and count the keys that move (outlined). Then make server B fail with and without virtual nodes, and compare the load bars.

Virtual nodes: fixing the uneven arcs

With one point per server, the arcs are random sizes: one server can own twice the keys of another. And when a server dies, all its keys land on a single neighbour, which may then overload too.

The fix: give each server many points (virtual nodes), for example 100 to 256. Each server owns lots of small arcs scattered around the ring, so the load evens out, and a failed server's keys are spread over all the others. A bigger machine can simply get more virtual nodes.

Where it's used

SystemWhat it distributes
Amazon Dynamo, DynamoDB, CassandraRows across storage nodes
Memcached / Redis clientsCache keys across cache servers
CDNs and load balancersRequests to servers, keeping the same user or URL on the same machine
Chat and realtime systemsWhich gateway server holds each user's connection

Key takeaways

  • 01hash mod N reshuffles almost every key when N changes.
  • 02On a hash ring each key goes to the next server clockwise, so changes only move one arc.
  • 03Virtual nodes give each server many small arcs: even load and graceful failures.
  • 04It's the routing behind Dynamo-style databases, distributed caches and many load balancers.

Check your understanding

1 of 3

On a hash ring, which server owns a key?

Related concepts