Databaseschapter 3 of 5 · 4 lessons
Consistent Hashing
Distribute data across nodes so adding/removing a node only moves a small fraction of keys.
You run four cache servers and decide which one holds a key by hashing it and taking the remainder. It works perfectly for a year.
Then you add a fifth, at 9am on a Monday, to handle growth.
Roughly 80 percent of your keys now hash to a different server than they did a minute ago. Every one of those is a miss. Your hit rate falls from 95 percent to almost nothing, and the full read load lands on the database the cache existed to protect.
Adding capacity took the site down. That is the landmine inside the obvious answer, and consistent hashing is how the whole industry defused it.
Lessons
4 in this chapter- The Resharding Problemhash(key) % N works perfectly, right up until N changes.3 min
- The Hash RingHash servers and keys into the same circular space, and only neighbors matter.3 min
- Virtual NodesOne physical server, many ring positions, much smoother load.3 min
- Where It Shows UpThe same ring sits inside caches, databases, CDNs, and load balancers.3 min