Skip to content
advancedPhase ·

Cache Stampede

Prevent thundering herd when many requests hit a cold cache.

40m
0 problems
Topic Progress0%

Cache Stampede

The Problem

Key expires → 1000 requests simultaneously → all miss → all hit DB

Solutions

  1. Mutex/Lock — One request regenerates, others wait
  2. Probabilistic expiration — Early refresh on access
  3. Background refresh — Refresh before expiry
  4. Stale-while-revalidate — Serve stale, refresh async

Key Points

  • Understanding Cache Stampede is essential for production systems
  • Always consider scalability and maintainability
  • Test thoroughly before deploying to production
  • Monitor performance and set up alerting

Common Patterns

  1. Validation: Always validate input at the boundary
  2. Error Handling: Use structured error responses
  3. Logging: Log key events for debugging
  4. Testing: Unit, integration, and load tests
  5. Documentation: Keep docs updated with code changes

Cache Best Practices

Strategies

  • Cache-Aside: Application manages cache
  • Write-Through: Sync write to cache and DB
  • Write-Behind: Async write to DB
  • Read-Through: Cache loads from DB

Invalidation

  • Time-based TTL
  • Event-based invalidation
  • Version-based keys
  • Tag-based grouping

Monitoring

  • Hit rate > 80% is good
  • Monitor eviction rates
  • Track cache size
  • Alert on anomalies

Key Points

  • Understanding Cache Stampede is essential for production systems
  • Always consider scalability and maintainability
  • Test thoroughly before deploying to production
  • Monitor performance and set up alerting

Common Patterns

  1. Validation: Always validate input at the boundary
  2. Error Handling: Use structured error responses
  3. Logging: Log key events for debugging
  4. Testing: Unit, integration, and load tests
  5. Documentation: Keep docs updated with code changes

Practice Problems

0/3solved
Implement Cache Stampede

Design and implement a solution for Cache Stampede in a backend system. Consider scalability, error handling, and production readiness.

Solution
// Cache Stampede implementation
// Key aspects: validation, error handling, logging, testing

public class CacheStampede {
    // Production-ready implementation
}
Cache Stampede Edge Cases

Identify and handle edge cases for Cache Stampede. What happens under high load, with invalid input, or during failures?

Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotency
Cache Stampede Testing Strategy

Write a testing strategy for Cache Stampede. Include unit tests, integration tests, and performance tests.

Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injection

Quiz

1. Cache stampede prevention: mutex does?

Question 1 options

2. Stale-while-revalidate serves?

Question 2 options

3. What is the primary purpose of Cache Stampede?

Question 3 options

4. What is a common mistake when implementing Cache Stampede?

Question 4 options

Flashcards

Question

Cache stampede?

Answer

Many requests for expired key at same time

Question

Mutex solution?

Answer

One request regenerates, others wait

Question

What is Cache Stampede?

Answer

Cache Stampede is a key concept in backend development.

Question

When to use Cache Stampede?

Answer

Use Cache Stampede when building production systems that require reliability, scalability, and maintainability.

Question

Cache Stampede best practices

Answer

Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.

Revision Notes

Key Takeaways

  • 1.Stampede: many requests on key expiry
  • 2.Solutions: mutex, probabilistic, background refresh
  • 3.Stale-while-revalidate: serve stale, refresh async

Interview Tips

  • Prevent cache stampede
  • Know solutions

Cheat Sheet

Cache Stampede

  • Problem: many requests on expiry
  • Fix: mutex (one regenerates)
  • Fix: probabilistic expiration
  • Fix: stale-while-revalidate