Distributed caching
How one cache is spread over several machines: which node holds which key, what happens when a node joins or fails, and how to keep the database from being trampled.
In 30 seconds
A distributed cache spreads cached entries over several machines, so it holds more than one machine could and survives the loss of one.
A hash of each key decides which node stores it. Consistent hashing arranges this so that adding or removing a node moves only a small share of the keys.
Copies on a second node cover failures, but every copy must then be invalidated when the data changes. And when a popular key expires, a lock lets one request refill it instead of a stampede hitting the database.
Distributed caching, step by step
Toy numbers: six keys and a ring of 60 positions. Real rings use a huge hash range and many virtual points per node to even out the load, and replication is optional in many caches.
- Step 1: One cache node runs out. A cache keeps answers in memory so the slow database is asked less often. One machine only has so much memory: here it holds four entries, and the shop has six popular keys, so two are fetched from the database every time. And if this one machine fails, all six are.
- Step 2: Spread keys with a hash. Use three nodes instead. To decide which node holds an entry, turn its key into a number with a hash function (the six keys here hash to 12, 16, 23, 31, 38 and 45) and take the remainder after dividing by the number of nodes. Every app server gets the same answer, so no lookup table is needed.
- Step 3: Adding a node reshuffles. The trouble starts when the cluster grows. With a fourth node the rule becomes mod 4, and most remainders change: four of the six keys now belong on a different node, where they have not been cached. Each is a miss, so adding capacity sends a burst of extra work to the database.
- Step 4: Consistent hashing: a ring. Consistent hashing fixes this. Hash values are laid out on a ring, nodes sit at positions on it, and each key belongs to the next node clockwise. A new node N3 at position 30 takes over only the arc from 20 to 30, so only key 23 changes owner. The other five stay where they were.
- Step 5: Replicas cover a failure. If a node dies, its entries are gone, and every request for them becomes a miss. To soften that, each entry can also be copied to the next node round the ring. When N1 fails, a request for key 38 simply carries on clockwise to N2, finds the copy, and the database is not disturbed.
- Step 6: Invalidate every copy. Copies on several nodes must all hear about a change. The app updates the price to $25 in the database and tells both nodes to delete their copy. The message to N2 is lost, so N2 keeps answering $20 while N1 reloads $25: the nodes disagree. A time to live on every entry, here 60 seconds, limits how long that can last.
- Step 7: Stop the stampede. When a popular entry expires, every request arriving in that instant misses, and each one asks the database the same question: a cache stampede. The usual guard is a lock on the key: the first request recomputes the value while the others wait, or are served the old value, and then read the new copy.
Try it yourself
Learn this first
Spotted a mistake on this page?