28. Consistent Hashing
28.4 What happens when you add or remove servers?
At the table: A new host sits down between two others. Only the guests sitting between the previous host and the newcomer now look right and see the newcomer first. Everyone else sees the same host as before. If a host leaves, their guests just look further right to the next one.
In the system:
Figure: only the arc next to the change moves.
- Add a server: it takes over the keys between its position and the previous server's position, which used to belong to the next server clockwise. No other key moves.
- Remove a server (or it fails): its keys move to the next server clockwise. No other key moves.
On average, adding one server to N moves about 1 ÷ (N + 1) of keys: the new server's fair share. In the chapter's code, going from 4 to 5 servers moves 22.9% of keys (close to the ideal 20%), versus 79.8% with mod N. And every moved key goes to the new server; no keys shuffle between old servers.
For the bookstore's 10 million cached pages, that's about 2 million misses instead of about 8 million.
One catch: with a single point per server, a removed server's whole load lands on one neighbour, which may then be overloaded. Virtual nodes fix that too (28.5).
Memory code: "A new host only takes the guests right beside them."