Requests for data that does not exist, and many keys expiring together: two ways a cache fails to protect you.
These are two different ways a cache stops protecting your database. Cache penetration is when requests ask for data that does not exist anywhere, so the cache can never help. Cache avalanche is when many keys expire at the same moment (or the whole cache goes down) and the database is hit by everything at once.
A cache works because most requests ask for things that were asked before. The cache keeps the answer, and the database is left alone. Both problems on this page are cases where that promise breaks.
Normally a cache miss is fine: the app reads the database, stores the answer in the cache, and the next request is fast. But what if the database has no answer? Suppose someone asks for /user/99999999 and that user was never created. The app finds nothing in the cache, asks the database, finds nothing there, and has nothing to store. The next request for the same id repeats all of it.
A few such requests are harmless. A flood of them, from a bug or from an attacker who tries random ids on purpose, goes straight through the cache to the database. The cache does nothing, so the database does all the work.
Imagine a nightly job that fills the cache with 100,000 keys, all with a life of one hour. An hour later all 100,000 expire in the same second. Every request now misses, and the app tries to rebuild 100,000 entries from the database at the same time. The database slows down, requests wait longer, and more pile up.
The same happens when the cache itself fails (a node crashes or the cluster restarts). The cache comes back empty and every key has to be rebuilt together.
| Defence | How it works | Good and bad |
|---|---|---|
| Validate the id first | Reject ids that cannot be valid (wrong format, negative, too long) before touching the cache or database. | Cheap and always worth doing. It cannot catch well-formed ids that simply do not exist. |
| Cache the “not found” answer | Store a marker (a null) for the missing id, with a short TTL such as 30 to 60 seconds. | Great when the same missing ids repeat. Weak against an attacker who uses a new random id every time, and the markers use memory. |
| Bloom filter of valid ids | A small structure that says “definitely not here” or “maybe here”. Check it before the cache. | Stops almost all missing ids using little memory. It has false positives (a few missing ids slip through) and must learn about new ids when they are created. |
| Rate limiting | Limit how many requests one client can send. | Slows a single attacker. Does not help against many clients, or against a bug in your own code. |
| Defence | How it works | Good and bad |
|---|---|---|
| Jitter on TTLs | Add a random amount to every TTL (for example base ± 10%), so keys expire at different times. | The simplest and most effective fix for expiry avalanches. Costs nothing. |
| Staggered warming | Warm the cache in small batches over time instead of all at once. | Avoids a synchronised start. Slightly slower to fill. |
| High availability for the cache | Replicas and automatic failover, or a cluster, so one crash does not empty the cache. | Protects against the “cache is down” case. More machines to run. |
| Two cache levels | A small local cache in each app in front of the shared cache. | Soaks up some load when the shared cache is cold. Adds a freshness trade-off. |
| Load shedding and circuit breaker | Reject or slow down some requests, or stop calling the database for a while, when it is overloaded. | Keeps the database alive during the spike. Some users get errors or a fallback. |
| Graceful degradation | Serve a simpler page, an old copy or a default when the database cannot be reached. | Users see something. Needs design work in advance. |
A simplified, deterministic model. The capacity (300/s), the 2% ordinary miss rate, the 60 s null TTL and the 20/s background load are illustrative numbers, not measurements. A rebuilt key costs one query, and each rebuilt key gets a new random life from a fixed seeded generator.
| Term | Meaning in easy words |
|---|---|
| Cache penetration | Requests for data that exists neither in the cache nor in the database, so each one reaches the database |
| Cache avalanche | Many keys expire at the same moment, or the cache goes down, so the database is flooded |
| Cache breakdown / stampede | One hot key expires and many requests rebuild it at the same time |
| Negative caching | Storing the answer “does not exist” (a null marker) so it is not asked again soon |
| Bloom filter | A small structure that answers “definitely not here” or “maybe here”, with no false “not here” |
| False positive | A Bloom filter says “maybe here” for something that is not there. A few missing ids slip through |
| TTL (time to live) | How long a cache entry is kept before it expires |
| Jitter | A small random amount added to a TTL so keys do not expire together |
| Cold cache | A cache that is empty, for example after a restart, so nearly every request misses |
| Load shedding | Rejecting some requests on purpose so the rest can still be served |
| Graceful degradation | Giving a simpler or older answer instead of failing completely |
| Choice | We gain | We lose |
|---|---|---|
| Longer null TTL | Fewer repeat queries for missing ids | A newly created record may look missing for longer |
| Shorter null TTL | New records appear quickly | More repeat database queries for missing ids |
| Lower Bloom false-positive rate | Fewer bad requests slip through | A bigger filter and more hash work |
| More jitter | Flatter database load | Less predictable freshness, some keys live longer |
| Short TTLs overall | Fresher data | More rebuilds, so more database load and more avalanche risk |
| Load shedding on | The database survives a spike | Some requests are rejected on purpose |
TTL = 3600 copied everywhere is an avalanche waiting for a bulk load.