Sub topic 3
Modulo Hashing and Why Resizing Hurts
This chapter assumes you know how hash functions and hash tables work, what a cache shard is, and how to read simple Python. It covers the simplest way to spread keys over nodes and shows why it fails when the cluster changes size.
The modulo scheme
Suppose you run a cache on N servers, numbered 0 to N-1. To pick the server for a key, hash the key to an integer and take the remainder after dividing by N:
node = h(key) mod NThis is cheap and needs no shared state. Every client that knows N and uses the same hash function picks the same node for a key. If the hash is uniform, each node gets about 1/N of the keys.
The scheme works well while N never changes. The trouble starts when you add a node for capacity or lose one to a failure.
What happens when N changes
Changing N changes the divisor, so most remainders change too. A key stays on its node only when h(k) mod N and h(k) mod (N+1) happen to be equal.
Take a cluster that grows from 4 to 5 nodes. The two remainders repeat together with period lcm(4, 5) = 20. A key keeps its node only when h(k) mod 20 is 0, 1, 2, or 3. In those cases the remainder is smaller than 4, so both moduli give the same answer. That is 4 of 20 residues, so about 20% of keys stay and about 80% move.
You can measure this directly:
import hashlib
def h(key):
return int(hashlib.md5(key.encode()).hexdigest(), 16)
keys = [f"key{i}" for i in range(10000)]
moved = sum(1 for k in keys if h(k) % 4 != h(k) % 5)
print(moved / len(keys))The printed value is close to 0.8. The exact number varies slightly because the keys are a finite sample.
In general, going from N to N+1 nodes moves about N/(N+1) of the keys. A larger cluster is hit harder. With 100 nodes, adding one moves about 99% of the keys, although the ideal answer is only 1/101, about 1%.
Why this hurts
Moved keys are a real cost, and the form it takes depends on what the nodes hold.
- Caches: A key that now maps to a different node is a miss, because the new node has never seen it. About 80% of requests fall through to the database at the same moment. This burst can overload the database, and the cache is effectively cold until it refills.
- Sharded storage: Each moved key means data must be copied to its new node. Moving 80% of the data is slow and consumes network and disk capacity while the system serves traffic.
- Node failure: Removing a node changes N just as adding one does. A single crash can reshuffle nearly every key.
The cost is proportional to the size of the data set, not to the size of the change. Adding one node should only require giving it a fair share of the keys, about 1/(N+1). Modulo hashing moves far more than that.
Common workarounds and their limits
Teams sometimes try to live with modulo hashing:
- Fixed large N: Hash into many fixed slots, such as 1024, and map slots to nodes. This reduces the problem, but the slot-to-node table is extra shared state that you must keep consistent.
- Resize only in maintenance windows: This avoids the live impact but rules out automatic scaling and does not help with unplanned failures.
- Double the cluster size: Going from N to 2N moves only about half the keys, but you must provision twice the capacity.
Each workaround adds state or constraints. What we want is a rule that needs no coordination, like modulo, but moves only about 1/(N+1) of the keys when a node joins. The next chapters build that rule, starting with the hash ring.
Exercise
A cache cluster grows from 9 to 10 nodes using h(key) mod N. Roughly what fraction of keys keeps its node, and what is the ideal fraction that would stay if only the new node took keys?
Show answer
Keys keep their node only when h(k) mod 90 is below 9, so about 9/90 = 10% stay and about 90% move. This matches the general estimate N/(N+1) = 9/10 moved. Ideally only the new node's share, about 1/10 of keys, would move, so 90% of keys would stay.