Consistent Hashing 00:00:01 Walk clockwise to the owner Three cache owners sit at different places around a circle. When a fourth arrives, which keys should have to move? The answer determines how much previously warm data an origin must suddenly supply. Each node is hashed to a position: N1 at 45, N2 at 135, N3 at 225 degrees. The key k1 lands at 100. It sits between N1 and N2. The first token at or after 100 is 135. So N2 owns the key. order:7, drawn as k2, lands at 200 and walks to N3 at 225. k3 lands at 300, after the last token. The walk passes zero and reaches N1. Hash both keys and node tokens onto the same ordered space. Starting from a key, choose its first clockwise node, wrapping when needed so every position has an owner. 00:01:13 Insert one more token A new node takes the stretch between its predecessor and itself. Four keys: k1 at 100 and k4 at 120 belong to N2, k2 to N3 and k3 to N1. Its predecessor is N1 at 45. Its successor is N2 at 135. Keys after 45 and up to 110 move from N2 to N4. That is only k1. The token is added, so reads for k1 go to N4. N2 deletes its copy. Only keys in the interval newly claimed by that token change owners under the specified ring rule. Data outside that interval continues to map as before if the remaining tokens stay fixed. 00:02:10 Remove the owner of an interval The stretch of a lost node falls on the next one clockwise. N2 holds k1 and k4, N3 holds k2, N1 holds k3. Its successor is N3 at 225. k1 and k4 now walk on to N3. Keys of N1 do not move. N3 now owns 180 of 360 degrees: 45 to 225. Its stretch was 90 before. Its load doubled. The next clockwise owner inherits the departed interval. Replication or cache reconstruction determines whether the correct mapping turns into an immediately available value. 00:02:59 Give a machine several token positions Every machine gets many tokens, so the shares even out. A at 180, B at 288, C at 0. A owns 180 degrees, B 108 and C 72: 50%, 30% and 20%. Each machine is hashed three times. The stretches are shorter and mixed. 20,000 keys. The busiest node against the average falls as tokens are added. If A dies, the stretches of A0, A1 and A2 go to B, C and B: more than one machine helps. Virtual nodes divide a physical machine's ownership among separate intervals. More uniform keyspace does not guarantee uniform demand when one individual key attracts a disproportionate number of readers. 00:03:58 Compare a remainder assignment The simple method and why it breaks when N changes. Hashes 7, 5 and 9 modulo 3 give nodes B, C and A. Now modulo 4. Every one of the three keys has a new home. For hashes 0 to 11 only 0, 1 and 2 have the same remainder: 9 of 12 move, which is 75%. Recomputing every key modulo a changed server count can send many keys elsewhere. In the displayed example, inspect each key rather than treating the observed movement fraction as a universal constant. 00:04:40 Grow a hot cache gradually A shop adds a cache server before a sale. The database can take 3,000 reads a second. 10,000 reads a second, 90% answered by the cache. The database gets 1,000. 80% of keys move. Hits fall from 9,000 to 1,800, so the database is asked for 8,200 a second. 20% of keys move. Hits fall to 7,200, so the database gets 2,800. It holds, and the new node fills up. Add the new node's tokens in four steps. Each step costs about 450 extra reads a second for a short time. Moving intervals in stages spreads the cold-start pressure over time. Put a ceiling on additional origin work and monitor misses during migration, not just the final distribution after all caches are warm. A ring gives a repeatable ownership rule, not free capacity. Measure the actual load, move ownership deliberately and protect the backing store while the new owner learns its keys.