Redis Cache Penetration, Breakdown, and Avalanche: What We Actually Fixed
We had three Redis incidents in one quarter and only understood them afterward. The names are textbook — penetration, breakdown, avalanche — but the textbook versions never mention what the graphs looked like or which fix actually held up. Here is our version.
Cache penetration: the query that should never exist
Someone wrote a script that requested user profiles by ID from a CSV. Half the IDs had been deleted a year earlier. Every request went: Redis miss, database query, empty result, nothing cached. So the next request for the same dead ID did the same thing.
At 400 requests per second the database took the full hit. It was not a huge table, but the connection pool filled up and the API started timing out.
The classic answer is a Bloom filter. We did something simpler first: cache the empty result with a short TTL.
SET user:missing:8821 1 EX 60
A dead ID now costs one Redis lookup for a minute. The tradeoff is that a newly created user with that ID would be "missing" for up to 60 seconds, which was acceptable for us. We added the Bloom filter later, when the list of valid IDs grew too large for this trick to be comfortable.
If you have not measured how many of your misses are for keys that can never exist, you cannot tell whether you have this problem. Log the misses for an hour before building anything.
Cache breakdown: one hot key expires
Our homepage cached a configuration object under a single key with a 10-minute TTL. At expiry, about 600 requests arrived in the same second. All of them missed, all of them queried the database, and all of them tried to rewrite the key.
Two fixes work. The first is never to let that key really expire: refresh it asynchronously before the TTL ends. The second is a lock, so only one request rebuilds it:
SET config:lock 1 NX EX 10
If the lock is set, wait a short moment and read the key again. If you got the lock, load from the database, write the cache, then delete the lock. Use a random value with the lock and delete only your own value; otherwise a slow rebuild can delete somebody else's lock.
We picked the background refresh because our config changes rarely and the code is shorter. The lock is the right answer when the data is expensive to rebuild and you cannot predict when it will be read.
Cache avalanche: everything expires together
This one was self-inflicted. A deploy wrote 40 keys with the same 3600-second TTL. An hour later they all disappeared together. The database saw a step change, not a spike, and stayed high for several minutes while everything rebuilt.
The fix is to add jitter to expiry times:
ttl = base + random.randint(0, 300)
Five minutes of spread was enough for us. We also stopped writing all keys during deployment; each key now gets refreshed when it is read and close to expiry.
If your monitoring shows a vertical wall in the database graph, look at key expiry times before looking at traffic. The traffic did not change.
What we would do differently
We now alert on Redis hit rate, not just on latency. A drop from 96% to 70% is a leading indicator; by the time latency moves, the database is already in trouble. We also log the top ten missing keys every minute, which is how we found the deleted-ID script in the first place.
The textbook names are useful shorthand, but the real fixes were mundane: cache empty results briefly, spread expiry times, and keep one hot key warm. None of that required a new component. It required looking at which keys were missing and why.