A cache cluster uses hash(key) mod 4 to pick one of four servers. Traffic grows and you add a fifth. Roughly what fraction of keys now map to a different server, and what does that do to the system?
Sketch your own answer first, then compare it with the one below.
A strong answer
A key stays put only if hash mod 4 equals hash mod 5. Looking at hash mod 20, which covers both, that holds for residues 0 to 3, which is 4 of 20. So about 20 percent of keys stay and 80 percent change servers. In general, going from n to n+1 servers leaves only about 1 in n+1 keys where they were.
For a cache, moved keys are misses. Four fifths of requests fall through to the database at the same moment, which can overload it and turn a scaling step into an outage. For a database or a store of record, 80 percent of the data has to be copied between machines, while serving traffic.
The fix is to separate the two decisions. Use a stable key to partition rule, such as hash mod 1000, with many more partitions than servers, and keep a table assigning partitions to servers. Adding the fifth server then moves about 1000 / 5, or 200, partitions: a fifth of the data, and only to the new machine. Consistent hashing with virtual nodes gets the same effect by placing servers on a ring, so a joining server takes over only the arcs next to its positions.
Either way, add capacity ahead of need, and warm the new node before it takes traffic.
Sources
- David Karger and others (1997). Consistent Hashing and Random Trees.
- Martin Kleppmann (2017). Designing Data-Intensive Applications.