Data & storage / 6 / 11
Consistent hashing
A profile API keeps hot data in three cache servers. Adding a fourth one should help, but with `hash % N` it empties almost the whole cache. How a hash ring moves only a quarter of the keys, and why it needs virtual nodes.
- 12 min
- Intermediate
- 9 steps
- Cache hit rate
- 95 %
- DB reads
- 250 /s
- Keys moved
- 0 %
- request
- reply
- redelivery
- duplicate
- lost
Numbers are a model scenario, not measurements
The real request
Profile API → Cache BRequest
GET user:u_4212
Response
"plan=pro;lang=uz"
GET—hash("user:u_42") = 2718281827, and2718281827 % 3 = 1, so the key lives on cache B.u_42— The user id inside the key. Every API server computes the same cache for it, with no lookup table.
Course: 42 lessons
01 Foundations
02 Scaling
03 Data & storage
04 Communication
05 Reliability
06 Architecture
07 Security
08 Shipping
09 Whole systems
10 Business processes