System Design (50 Qs)•Part 1: Foundational Distributed Systems Concepts & Trade-offs (1 - 10)
Cache Stampede, Thundering Herd, and Cache Invalidation Strategies.
In Simple Words:
- Cache Stampede (Thundering Herd): When a heavily requested hot cache key expires (e.g. homepage top stories or flash sale product), thousands of concurrent requests experience a cache miss simultaneously and hammer the primary database, crashing it.
- Cache Penetration: Malicious or buggy clients query keys that exist neither in cache nor in the database (e.g.
id = -9999), constantly hitting the DB.- Fix: Cache null objects with short TTL (e.g. 60s) or filter queries using Bloom Filters.
- Cache Breakdown: A single super-hot key expires.
- Fix: Mutex distributed lock or Probabilistic Early Recomputation (XFetch).
typescript
// Thundering Herd Protection using Mutex Distributed Lock in Node.js
export async function getWithMutexLock<T>(
key: string,
dbFetcher: () => Promise<T>,
redis: any,
ttl: number
): Promise<T> {
let val = await redis.get(key);
if (val) return JSON.parse(val);
const lockKey = `lock:${key}`;
// Try acquiring lock for 5 seconds
const acquired = await redis.set(lockKey, 'locked', 'NX', 'PX', 5000);
if (acquired) {
try {
const data = await dbFetcher();
await redis.set(key, JSON.stringify(data), 'EX', ttl);
return data;
} finally {
await redis.del(lockKey);
}
} else {
// Another worker is fetching from DB: sleep and retry
await new Promise(resolve => setTimeout(resolve, 80));
return getWithMutexLock(key, dbFetcher, redis, ttl);
}
}