You add a server to a hash ring. Which keys move?
Question
A cache spreads user profiles over servers with a hash ring of positions 0 to 99. A key belongs to the first server at or after its hash position. Past 99, it wraps around to 0.
Servers sit at A = 10, B = 40 and C = 75. You add server D at 60.
- Which of these keys move, and from which server to which? Key positions: 5, 18, 33, 47, 58, 61, 80, 95.
- How many would move with
hash % Ninstead (hash = position), going from 3 to 4 servers?
Show hints (3)
- "WALK THE RING"
Key 47 used to walk up to C at 75. Where does it stop once D sits at 60?
- "WHICH RANGE"
A key moves only if it now meets D first. D's range starts just after the server before it. Which server is that?
- "TRY THE MODULO"
With
hash % N, a key stays put only ifhash % 3equalshash % 4. Check that for each of the eight keys.
Show checked explanation
Only keys between D and the server before it move: positions 41 to 60, all from C to D. Here that is keys 47 and 58. The other six stay. With hash % N, going from 3 to 4 servers moves 7 of the 8 keys.
from bisect import bisect_left def owner(servers, key): points = sorted(servers) i = bisect_left(points, key) % len(points) # past the last: wrap to 0 return servers[points[i]] keys = [5, 18, 33, 47, 58, 61, 80, 95] before = {10: "A", 40: "B", 75: "C"} after = before | {60: "D"} moved = [(k, owner(before, k), owner(after, k)) for k in keys if owner(before, k) != owner(after, k)] print("ring moves:", moved) print("modulo moves:", sum(k % 3 != k % 4 for k in keys), "of", len(keys))
ring moves: [(47, 'C', 'D'), (58, 'C', 'D')] modulo moves: 7 of 8
Code run on CPython 3.12 against the keys above and every position on the ring. Checked by an editor other than the author.
Walkthrough
Each server owns the positions after the server before it, up to and including its own. D at 60 splits C's range:
| server | owns before | owns after |
|---|---|---|
| A at 10 | 76–99 and 0–10 | 76–99 and 0–10 |
| B at 40 | 11–40 | 11–40 |
| D at 60 | nothing | 41–60 |
| C at 75 | 41–75 | 61–75 |
Keys 47 and 58 used to walk up to C at 75. Now they meet D at 60 first. Key 61 is past D and still reaches C. Keys 80 and 95 wrap around to A, as before.
D takes 20 of the 100 positions, all from C. A and B keep every key. This is the point of consistent hashing: adding or removing a server changes only the range of the next server clockwise.
With hash % N, a key stays only if hash % 3 == hash % 4. That holds for 27 of the 100 positions. So 73 move, between all servers, where 25 would be enough. In a cache, that is a wave of misses that all hit the database.
One point per server gives uneven shares: after the change, A owns 35 positions and C only 15. The fix is virtual nodes. Each server gets many points, placed by hashing names like A#1, A#2. A new server then takes a small slice from every old server, and the shares get much closer to even.
Common mistakes
- Thinking D takes keys from both sides. Keys 61 to 75 stay on C. D takes only the range before it, 41 to 60.
- Getting the edges wrong. A key at a server's exact position belongs to that server: key 60 goes to D, and key 40 stays on B.
- Forgetting the wrap. No server sits at or after key 95, so code that only searches upward finds nothing. Key 95 belongs to A, the first server after 0.
Follow-up questions
- "D now owns key 47. Can it serve a read right away?" No. The ring only says where a key belongs. It copies nothing. In a cache, the first read misses and reloads from the database. In a store, copy positions 41 to 60 from C to D first, then switch the ring. Writes made during the copy must reach D too.
- "A celebrity's profile gets 10,000 reads a second. Do virtual nodes help?" No. One key has one position, so all its reads go to one server. Virtual nodes even out key counts, not one key's traffic. Treat a hot key on its own: cache it in the app servers, or keep a few copies under different keys and read a random one.
- "How do you keep three copies of each key?" Put each key on its owner and the next two servers clockwise. Key 47 goes to D, C and A. With virtual nodes, skip points of servers already on the list, so the copies land on three machines.
What a strong answer covers
Names the range that moves, 41 to 60 from C to D, and why nothing else moves. Compares it with hash % N. Adds virtual nodes for even key shares. Says the ring picks owners but moves no data.
Open in interactive practice deck →