Why do many systems create far more partitions than machines, and how does that relate to hash mod N and consistent hashing?
Sketch your own answer first, then compare it with the one below.
A strong answer
If partition equals hash modulo the number of machines, adding a machine changes the divisor and moves most keys. To avoid that, create many partitions up front, say a few hundred per machine, and assign whole partitions to machines. The key to partition rule never changes. Only the map from partitions to machines does, and adding a machine means moving a few partitions to it.
Consistent hashing with virtual nodes reaches a similar result by a different route. Keys hash onto a ring, the ring is cut into many small arcs, and a joining node takes over some of them. Both limit movement to roughly the new machine's fair share.
The partition count is a real decision. Too few and load cannot be balanced finely or the cluster grown far. Too many and each partition carries overhead. The partition is also the unit of movement, replication and failure.
Sources
- David Karger and others (1997). Consistent Hashing and Random Trees.
- Martin Kleppmann (2017). Designing Data-Intensive Applications.