28. Consistent Hashing
28.1 What's the problem with mod N?
At the table: Counting off by four works perfectly, right up until a fifth host arrives. Then you count off by five, and nearly every guest has a new host. All the chatting and familiarity is lost.
In the system: The simplest way to spread keys over N servers is server = hash(key) mod N. It spreads keys evenly and every client can compute it. The trouble is changing N:
- Going from 4 to 5 servers, a key stays put only if
hash mod 4happens to equalhash mod 5. That's about 1 in 5 keys. - So about 80% of keys move. In the chapter's code, 79.8% of 100,000 cached book pages map to a different server.
Figure: changing N reshuffles almost everything.
For a cache, a moved key is a miss. You add a server to handle more load, and at that exact moment 4 in 5 lookups miss and go to the database (a self-inflicted stampede, Chapter 13). For a database or storage system, a moved key means copying data between machines: 80% of everything, over the network.
The same happens when a server fails (N drops by one) and when it comes back. In general, going from N to N + 1 servers moves about N ÷ (N + 1) of the keys.
Memory code: "Count off again, and everyone changes host."