28. Consistent Hashing
1
The picture to keep in your head2
28.1 What's the problem with mod N?3
28.2 What is the hash ring?4
28.3 How do you find a key's server?5
28.4 What happens when you add or remove servers?6
28.5 What are virtual nodes?7
28.6 How does replication work on the ring?8
28.7 Where is consistent hashing used?9
28.8 What is rendezvous hashing, in brief?10
Memory card and where the analogy breaks11
Practice: five levels12
Interview questions13
Solutions14
Sources15
Code lab28. Consistent Hashing
Practice: five levels
3 min read+10 XP
Assignments
Five levels. Do them in order. Answers are in "Solutions" at the end, so try first.
Level 1: Think (no code)
A. True or false? Explain in one line.
- With
hash mod N, adding one server moves about 1/N of keys. - On a hash ring, adding a server only moves keys to the new server.
- Virtual nodes make lookups much slower.
- With virtual nodes, a failed server's keys go to one neighbour.
- Rendezvous hashing needs a ring.
B. Ring, fixed slots, or plain mod N? One line each.
- A fixed set of 8 database shards that will never change.
- A Memcached cluster that grows during sales.
- A Redis Cluster.
- A load balancer that should keep each user on the same backend as backends come and go.
Level 2: Calculate
- 10 million cached pages across 4 servers with mod N. You add a 5th. About how many pages move?
- Same with a hash ring (ideal case). About how many move?
- Going from N to N + 1 servers: what fraction moves with mod N, and with a ring? Work it out for N = 9.
- 20 servers, 200 virtual nodes each. How many points on the ring? About how many binary-search steps per lookup?
- One server holds 1.95× its fair share of keys. If each server can handle 30,000 requests a second and the cluster gets 100,000 a second spread over 4 servers, is that server overloaded?
Level 3: Build
In Python:
moved_fraction(before, after)and show mod 4 → mod 5 moving about 80% of keys.- A
Ringwithadd,removeandlookupusing a stable hash and binary search. - Show that adding a server moves about 1/(N + 1) of keys, all to the new server, and removing one moves only its keys.
- Add virtual nodes and measure the busiest server's share with 1 vs 100 vnodes.
replicas(key, n)returning the next n distinct servers, andrendezvous(key, servers).
Level 4: Debug
After a disk failure on one storage node, the team discovered that some keys had lost all their data, even though the system is configured to keep 3 replicas of everything. The replica code:
python
- Explain how a key with "3 replicas" lost all its data when one node failed.
- Fix it, and name one more improvement real systems make.
Level 5: Design
Design the key placement for the bookstore's cache cluster: 10 million cached pages, 4 servers normally, growing to 8 during the Big Book Sale, with servers of two sizes (16 GB and 32 GB). Some pages are very hot.
Deliver:
- Ring, fixed slots or rendezvous, and why.
- How many virtual nodes (or slots), and how bigger servers get more keys.
- How clients learn about servers joining and leaving.
- What happens to hit ratio and the database when you add 4 servers at once, and how to soften it.
- How very hot pages are handled, since hashing alone puts each key on one server.