How it works
The simple rule hash(key) mod N works when a fixed set of N servers is expected. Every client can compute the same index cheaply, with no shared state beyond N. Kafka’s default partitioner uses this idea to pick a partition for a keyed message, which works because partition counts rarely change.
But changing N changes the remainder for many keys, including keys whose previous server is still healthy. A key with hash 14 stays put going from 4 servers to 3, since 14 mod 4 and 14 mod 3 are both 2. A key with hash 17 moves from 1 to 2. The effect is not that every key moves; it is that movement is spread across all servers rather than limited to the one added or removed. Going from 4 servers to 3 changes the index for about 75% of keys, if the survivors keep their numbers. Growing from N to N+1 leaves only about 1 key in N+1 in place, so 9 to 10 servers moves about 90%.
In a cache, that causes widespread misses and a sudden load increase on the backing store. With an example 100,000 reads per second and a 90% hit rate, the database normally sees 10,000 per second. If 75% of keys remap, 75,000 reads miss at once, and the other 25,000 add 2,500 ordinary misses: 77,500 per second, nearly eight times normal. In durable storage, it calls for a large migration. Modulo stays a fine choice when membership is stable or a cold start is cheap.
A four-node cache loses one node, then the service switches from hash(key) mod 4 to mod 3. Why does the database spike?
Many keys now map to different healthy cache nodes and miss, not only those on the failed node. The database receives the refill traffic.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Modulo is simple for a fixed membership set.
- Membership changes can remap many unaffected keys.