Difficulty: Advanced
Explain cache penetration, cache breakdown and cache avalanche (snowslide). How would you prevent each one?
All three are ways a cache stops protecting the database. Cache penetration is repeated requests for data that does not exist anywhere, so every request misses the cache and hits the DB. Cache breakdown (hotspot invalid) is a single very hot key expiring and thousands of concurrent requests rebuilding it at once. Cache avalanche (snowslide) is a large number of keys expiring together, or the cache going down entirely, sending a wave of traffic to the DB.
Typical cause: bots or bugs requesting product:999999999, or an attacker iterating IDs. The DB returns nothing, the app caches nothing, and the next request repeats it.
"__NULL__" with a short TTL (30-60 seconds).BF.ADD, BF.EXISTS, built into Redis 8 and earlier available via RedisBloom). If the filter says "definitely not present", reject without touching the DB. False positives are possible, false negatives are not.Typical cause: the homepage banner or a flash-sale product during Big Billion Days expires, and 10,000 requests miss within the same millisecond.
SET lock:key token NX PX 5000 and rebuilds; others wait briefly and retry the cache, or serve a stale copy.expiresAt but no Redis TTL. When a reader sees it is logically expired, it triggers an async refresh and returns the old value.Typical cause: a warm-up job loads 1 million keys with the same EX 3600, so they all expire at the same minute. Or a Redis node crashes.
EX 3600 + random(0, 600) spreads expiry over time.| Problem | Keys involved | Root cause | Primary fix |
|---|---|---|---|
| Penetration | Non-existent keys | Nothing to cache | Cache nulls, Bloom filter |
| Breakdown | One hot key | Concurrent rebuild on expiry | Mutex or logical expiry |
| Avalanche | Many keys or whole cache | Synchronised expiry or outage | TTL jitter, HA, circuit breaker |
The terms "breakdown" and "snowslide" are common in Chinese-origin interview material; in Western writing you will also hear "cache stampede" or "thundering herd" for breakdown and "cache avalanche" for snowslide.
| Penetration | Breakdown | Avalanche | |
|---|---|---|---|
| Trigger | Requests for missing data | One hot key expires | Mass expiry or cache down |
| Scope | Many unknown keys | Single key | Many keys |
| First fix | Cache null values | Rebuild under a lock | Random TTL jitter |
| Second fix | Bloom filter | Logical expiry | Replicas and circuit breaker |
const NULL = '__NULL__';
const ttl = (base) => base + Math.floor(Math.random() * 300); // jitter
async function getProduct(id) {
if (!(await redis.bf.exists('bf:products', id))) return null; // penetration
const key = `product:${id}`;
const cached = await redis.get(key);
if (cached === NULL) return null;
if (cached) return JSON.parse(cached);
// breakdown: only one caller rebuilds the key
const got = await redis.set(`lock:${key}`, '1', { NX: true, PX: 5000 });
if (!got) {
await new Promise((r) => setTimeout(r, 50));
return getProduct(id); // retry; add a max-retries guard in real code
}
try {
const row = await db.products.findById(id);
await redis.set(key, row ? JSON.stringify(row) : NULL,
{ EX: row ? ttl(3600) : 60 }); // avalanche: jittered TTL
return row;
} finally {
await redis.del(`lock:${key}`);
}
}
The Bloom filter blocks unknown IDs, null results are cached briefly, a short lock prevents a stampede on rebuild, and random TTLs keep keys from expiring together.
Cache Penetration, Cache Breakdown, Cache Avalanche, Bloom Filter, TTL Jitter, Mutex Lock