28. Consistent Hashing
The picture to keep in your head
Every question below is answered twice: first at the table, so you can picture it, then in the system, so you know exactly how it works. Each answer ends with a memory code to lock it in.
The picture to keep in your head
A wedding has one long dinner and four hosts. The first plan: guests count off by four, and number 1s go to host 1, number 2s to host 2, and so on. It works, until a fifth host arrives. Now everyone counts off by five, and almost every guest ends up with a different host. Total chaos for one extra person.
Better plan: a big round table. Hosts take seats around it. Each guest also gets a seat, by a fixed rule from their name. A guest is looked after by the first host to their right. When a fifth host sits down, only the guests between that new host and the previous host switch to the newcomer. Everyone else keeps their host. If a host leaves, only their guests move one seat along.
And to stop one host getting a huge stretch of table while another gets a tiny one, each host sits in many seats around the table, scattered all over.
That's consistent hashing: put servers and keys on a circle, and a key belongs to the next server clockwise, so adding or removing a server moves only a small share of keys.
Our running example: the bookstore's cache cluster. About 10 million cached book pages are spread across 4 cache servers. For the Big Book Sale, we add a 5th. How many cached pages suddenly become misses? (Counts are assumptions for practice.)
| At the table | In the system |
|---|---|
| Counting off by four, then by five | Hash mod N |
| The round table | The hash ring |
| The first host to your right | Finding a key's server |
| A new host sits down; a host leaves | Adding and removing servers |
| Each host sitting in many seats | Virtual nodes |
| Your host plus the next two as backups | Replication on the ring |
| Where wedding planners use this | Where it's used |
| "Every host bids; the highest bid wins you" | Rendezvous hashing |
Figure: when a host joins or leaves, only the guests next to them change seats.