Consistent hashing, measured: how many keys move when you add a node

Adding an 11th node to a hash(key) % N cluster moved 90.7% of 100,000 keys in our test; a consistent-hash ring moved about 9%. What virtual nodes change, and what they cost.

Prakhar

Backend engineer · Pressentify

Published

4 min read 943 words
A ring of small blue points with six larger nodes placed around it, one highlighted in amber, and the word hash in the centre.

Spreading keys over servers with hash(key) % N works until the day you add a server. Then N changes, nearly every key gets a new home, and a cache cluster goes cold or a sharded database has to move almost everything. Consistent hashing exists to avoid that. We wanted to see the size of the difference, so we measured it.

Why modulo moves almost everything

A key keeps its server only if hash % 10 and hash % 11 happen to give the same number. For two nearby moduli that is about one key in eleven. So adding the eleventh node should move roughly ten keys in eleven, which is 90.9%. The ideal is different: a new node should take its fair share of the data, one eleventh or 9.1%, and nothing else should move.

For a cache that is a stampede of misses. For a sharded database it is a rebalance of nearly the whole data set, so people avoid growing the cluster at all and pay for it later.

The ring in one paragraph

Consistent hashing, introduced by Karger and colleagues in 1997, hashes both the servers and the keys onto the same circle. A key belongs to the first server point you meet going clockwise from it. Add a server and it only takes over the stretch of the circle just before its own points. Keys elsewhere do not notice. Amazon’s Dynamo paper (2007) added a refinement now called virtual nodes: each physical server owns many points on the ring, not one.

The test

We hashed 100,000 made-up keys (user:0 to user:99999) with MD5, placed 10 nodes, then added an 11th and counted the keys whose owner changed. We tried the modulo scheme and rings with 1, 10, 100, 200 and 1,000 points per node. The code is short enough to read in full:

import hashlib, bisect

def h(s):
    return int.from_bytes(hashlib.md5(s.encode()).digest()[:8], "big")

KEYS = [f"user:{i}" for i in range(100_000)]

def mod_owner(nodes):
    return {k: h(k) % nodes for k in KEYS}

def ring(nodes, vnodes):
    points = sorted((h(f"node{n}#{v}"), n) for n in range(nodes) for v in range(vnodes))
    hashes = [p[0] for p in points]
    def owner(k):
        i = bisect.bisect(hashes, h(k)) % len(points)   # first point clockwise from the key
        return points[i][1]
    return {k: owner(k) for k in KEYS}

def moved(a, b):
    return sum(1 for k in KEYS if a[k] != b[k]) / len(KEYS)

Output from one run (no random seed is involved; the hash is deterministic):

mod N: moved 90.7%, ideal 9.1%
ring,    1 vnodes: moved 22.3%, max load / mean (10 nodes) = 3.38
ring,   10 vnodes: moved 7.8%, max load / mean (10 nodes) = 1.43
ring,  100 vnodes: moved 9.1%, max load / mean (10 nodes) = 1.17
ring,  200 vnodes: moved 8.4%, max load / mean (10 nodes) = 1.17
ring, 1000 vnodes: moved 9.0%, max load / mean (10 nodes) = 1.03
Horizontal bar chart: keys moved when an 11th node is added. hash(key) % N moves 90.7 percent; a ring with 1 point per node 22.3 percent; with 10 points 7.8 percent; 100 points 9.1 percent; 1000 points 9.0 percent. A dashed line marks the ideal 9.1 percent.
Modulo moved 90.7% of the keys. Every ring version moved far fewer, and with enough points the figure sits on the ideal 9.1%.

What the numbers say

  • Modulo: 90.7% moved. It matches the 10-in-11 estimate. This is the case consistent hashing was invented for.
  • Ring, one point per node: 22.3% moved. Far better, but more than twice the ideal. With one point each, the arcs are very uneven, and the new node happened to land in a wide one.
  • Ring, 100 to 1,000 points: 8.4% to 9.1% moved. Close to the ideal 9.1%. The wobble between 8.4% and 9.1% is where the points happened to land, not a trend.

The 10-point row (7.8%) is below the ideal. That is not a free win: the new node got less than its fair share of keys, which means it will be under-used while the others stay loaded. Moving fewer keys is not the goal; moving exactly the new node’s share is.

The second reason for virtual nodes: balance

The same experiment shows why one point per node is not enough, even before you add anything. With 10 nodes and one point each, the busiest node held 3.38 times the average share of keys. At 10 points it was 1.43, at 100 points 1.17, and at 1,000 points 1.03.

Horizontal bar chart of the busiest node's load divided by the average load: 3.38 with 1 point per node, 1.43 with 10, 1.17 with 100, 1.17 with 200 and 1.03 with 1000. A dashed line marks 1.0, perfectly even.
More points per node smooth out the load. The busiest node falls from 3.4 times the average to about 1.03 times.

What it costs

Points are not free. A ring with 10 nodes and 1,000 points each has 10,000 entries to keep sorted and look up (a binary search per key, as in the code above). That is small for a client library. It matters more when every node in a large cluster must hold the ring and keep it consistent as nodes join and leave. A few hundred points per node is the usual compromise; give bigger servers proportionally more points so they take proportionally more keys.

What this test does not show

We measured key movement and balance only. A real system also needs replication (the next few nodes clockwise usually hold copies), a plan for hot keys, and a way to move data to the new node while it keeps serving. We did not test rendezvous hashing or jump consistent hash, which also move about one key in N+1 and are worth reading about if you do not want to keep a ring.

A checklist before you pick a scheme

  1. Will the number of nodes ever change while the system is live? If never, modulo is fine and simpler.
  2. What does a moved key cost you: a cache miss, or a database row copy? That sets how much movement you can tolerate.
  3. Are the servers the same size? If not, give each a number of points in proportion to its capacity.
  4. Do you need the lookup in a client library or a proxy? Either must stay in step with the ring membership.
  5. Measure your own key names with your own hash before trusting any rule of thumb. It takes a minute.

Sources & Credits

  • David Karger et al., “Consistent Hashing and Random Trees: Distributed Caching Protocols for Relieving Hot Spots on the World Wide Web,” STOC 1997: dl.acm.org
  • Giuseppe DeCandia et al., “Dynamo: Amazon’s Highly Available Key-value Store,” SOSP 2007: dl.acm.org
  • John Lamping and Eric Veach, “A Fast, Minimal Memory, Consistent Hash Algorithm,” 2014: arxiv.org

The experiment, the numbers and the charts are our own work (Python 3, MD5, run on 17 November 2026). The papers above are cited for the ideas, not for our numbers. Last verified 2026-11-17. Spotted a mistake? Tell us and we will correct it here with the date.

Share this article

Written by

Prakhar

Backend engineer · Pressentify

Backend engineer building Pressentify — interactive cheatsheets, handbooks and a job-hunt workspace.

Get the next one in your inbox

One practical backend article at a time. No spam; unsubscribe in one click.

Occasional. One-click unsubscribe.