InterviewHubPRO
Section Overview
Q44

Question 44 of 60

Part 4: In-Memory Caching & Fast Data Stores (Redis) (41 - 50)

73%
Databases & System Design•Part 4: In-Memory Caching & Fast Data Stores (Redis) (41 - 50)

Cache Stampede vs Penetration vs Avalanche:

In Simple Words:

  • Stampede (Thundering Herd): Hot key expires; thousands hit DB at once. Fix: Mutex lock on miss.
  • Penetration: Requests for non-existent keys bypass cache. Fix: Cache null values or use Bloom Filter.
  • Avalanche: Hundreds of keys expire at the same second. Fix: Add random jitter to TTLs.
typescript
// Preventing Cache Avalanche with TTL Jitter:
const baseTTL = 3600; // 1 hour
const jitter = Math.floor(Math.random() * 300); // 0-5 mins
await redis.set(key, value, 'EX', baseTTL + jitter);