System Designsimulator

Consistent hashing simulator

The simple way to spread keys over N servers is hash(key) mod N. It spreads keys evenly, but when N changes almost every key maps to a different server, so a cache goes cold or a database has to move most of its data.

Somewhat uneven

Node A holds 2,893 keys against a fair 2,500; adding a node moves 19.3% of keys, where hash mod N moves 79.4%.

Busiest node1.16×Its keys ÷ a fair share
400ring positions
Outer ring: the arc each node owns. Dots: the first 60 keys, coloured by owner.
  1. A2,893 28.9%
  2. B2,262 22.6%
  3. C2,604 26%
  4. D2,241 22.4%

Ring settings

Servers or shards on the ring, named A, B, C…

Positions each node takes on the ring. More positions even out the load.

Balance and keys moved

Busiest node1.16×Somewhat uneven: node A has 2,893 keys against a fair 2,500
Quietest node2,241Node D
Standard deviation26910.8% of the mean
Keys moved on add19.3%hash mod N moves 79.4%; the new node’s even share is 20%
Keys moved on remove22.4%Node D’s keys; hash mod N moves 75.3%

What it shows

Consistent hashing puts servers and keys on the same ring of hash values. Each key belongs to the first server position at or after it, going clockwise. Adding a server takes keys only from its neighbours. Removing one hands its keys to the next servers along.

With one position per server, the arcs are uneven, so the load is too. Many positions per server, called virtual nodes, even it out. The tool hashes real keys onto a real ring so you can watch both effects.

How to read it

  • The outer ring shows which node owns each arc. The dots inside are the first keys, coloured by the node they land on.
  • The bars count every key per node. The thin line on each bar is a perfectly even share.
  • Busiest node is its key count divided by the even share. 1.00× is perfect. Plan capacity for the busiest node, not the average.
  • Keys moved on add and on remove compare the ring with hash mod N for the same keys. On the ring, adding a node moves about that node’s even share. With mod N, going from N to N + 1 nodes moves about N ÷ (N + 1) of all keys.
  • Add a node and Remove a node change the cluster so you can see the new spread.

Questions

Why does hash mod N move so many keys?

A key stays put only if hash mod N equals hash mod (N + 1). For well-mixed hashes that happens for about 1 key in N + 1, so going from 4 to 5 nodes moves about 80% of keys.

What are virtual nodes?

Each physical node is hashed onto the ring at many positions instead of one. Its share of the ring becomes the sum of many small arcs, which varies much less than one large arc. Amazon’s Dynamo paper describes this technique.

How many virtual nodes should I use?

More positions give a more even spread but a larger ring to store and search. Use the tool to see how the busiest node’s load changes for your node count, then pick the smallest number that gives a spread you can live with.

Which hash function does the tool use?

32-bit MurmurHash3, for both node positions and keys. Node positions hash the text node-A#0, node-A#1 and so on; keys hash key-0, key-1 and so on. Results change with the hash function, so treat exact counts as one sample.

Learn it properly

Sources

Last updated

More tools

All tools

Your first senior-level
lesson is free.

Choose a course, open a free lesson, and explore an idea you’ve wanted to understand.