Caching Strategy Designer
Purpose
Design and implement caching at every layer to dramatically reduce latency and database load.
Cache Layers
- Browser Cache: Static assets, API responses with Cache-Control headers
- CDN Cache: Cloudflare/CloudFront for global edge caching
- Application Cache: Redis/Memcached for computed data
- Database Cache: Query result cache, connection pooling
Cache-Control Headers
# Immutable static assets (hash in filename)
Cache-Control: public, max-age=31536000, immutable
# API: cache for 60s, stale-while-revalidate for 5 min
Cache-Control: public, s-maxage=60, stale-while-revalidate=300
# Private user data
Cache-Control: private, no-cache
# Never cache
Cache-Control: no-store
Application Cache Pattern
# Cache-aside with stampede prevention
def get_cached(key: str, ttl: int, fetch_fn: callable):
if value := cache.get(key):
return value
# Atomic lock prevents stampede
with cache.lock(f"lock:{key}", timeout=10):
# Double-check after acquiring lock
if value := cache.get(key):
return value
value = fetch_fn()
cache.set(key, value, ttl)
return value
Invalidation Strategies
- TTL-based: Expire after fixed time (simple, may be stale)
- Event-driven: Invalidate on data change (accurate, complex)
- Tag-based: Group related cache items, flush by tag
- Cache-busting: Change URL/key when content changes
Outputs
- Caching strategy document
- Cache-Control header configuration
- Application cache implementation
- Invalidation event handlers
- Cache monitoring metrics