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.
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%.
- A2,893 28.9%
- B2,262 22.6%
- C2,604 26%
- 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
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- estimatorBack-of-envelope estimatorTurn daily users into requests per second, storage and bandwidth, with the working shown.Open tool
- estimatorQueue latency estimatorWhy latency explodes as servers get busy: utilisation, waiting time and Little’s law.Open tool
- estimatorCache hit rate estimatorEstimate hit rate, mean latency and origin load for an LRU and an ideal cache under Zipf-skewed traffic.Open tool