Lab · Simulation

Hash ring

Twenty thousand keys, a handful of cache nodes, and a cluster that won't stay the same size. Decide how keys find their node, then add one, lose one, and survive a key that everyone wants.

Add a node20,000 keys
0/12 ★

Four cache nodes are full. Add a fifth. Every key that changes owner is a cache miss that lands on the database, so move as few as you can — ideally just the fifth that the new node should own.

no ring · owner = hash mod 44 nodesmodulo

dots are a sample of keys · hover a node to trace its arcs

Load per node

average

Keys moved
–
Busiest node
1.02×avg
Ring entries
4
Scheme
mod N
  1. Under half the keys move
  2. Under a quarter move
  3. …and the new cluster is balanced within 15%

Pick your settings, then make the change.

Placement

Scheme

hash(key) mod N. Simple, balanced, and every change of N reshuffles almost everything.

Virtual nodes per server

4 points on the ring. More points, smaller and more even arcs — and a bigger table in every client.

Settings apply to the cluster before and after.

What the lab models

Every key and every node point is placed with the same 32-bit hash a client library would use, so the numbers are real placements, not estimates. Load is the share of requests each node serves, with every key equally popular except, in the last level, one that takes 30% of all reads.

Why modulo hashing hurts

hash(key) % N spreads keys evenly and needs no state. The trouble is the N. Go from four nodes to five and a key stays put only if its hash gives the same remainder under both, which is about one key in five. The other 80% move. For a cache, every moved key is a miss that lands on the database at the worst moment: right when you were adding capacity because things were busy.

Consistent hashing

Put the nodes on a circle of hash values and send each key to the first node clockwise from it. Adding a node only takes over the arc just before it; removing one hands its arc to the next node along. Only about 1/N of the keys move, which is the least any scheme can manage.

With one point per node, though, the arcs are random lengths. One node can own a third of the ring while another owns a sliver, and when a node dies its single successor inherits all of its load. That is how one failure becomes two.

Virtual nodes

The fix is to give every server many points on the ring. Each owns lots of small arcs instead of one big one, so the totals even out roughly as 1/√V. When a server leaves, its arcs are scattered all over the ring and its load spreads across every survivor. The cost is the routing table: every client holds N × V entries and has to agree on them. Somewhere around 100–200 points per server is usually enough; Cassandra used 256 for years before lowering its default.

Hot keys

No placement scheme helps when a single key is the problem. Hashing decides where a key lives, not how popular it is, and a key with 30% of the traffic makes its owner 30% busier whatever you do with the ring. You have to change the key:

  • Replicate it to the next k distinct nodes and let readers pick one. Load divides by k, and every write now updates k copies.
  • Split it into suffixed keys, celebrity#0 to celebrity#k, that hash independently. Same write cost, but the suffixes can collide on one node, which the lab will show you.
  • Bounded loads cap every node at c × the average and push overflow further round the ring. It can't split the hot key, but it can move everything else off the hot node. Tighter bounds push more keys away from their natural home, which costs an extra hop on lookup and more churn when the ring changes.

The levels are deterministic: the same settings always give the same placement, so you can change one thing at a time and see exactly what it did.