▶Cache invalidation strategies — write-through vs write-back vs cache-aside. Which for my use case?
Write-through: always hit cache first, miss = fetch + write back. Safe, consistent, slower writes. Use for reads >> writes. Write-back (write-behind): write to cache immediately, flush to DB async. Fast writes, risks data loss if cache dies. Use for non-critical data (analytics, counters). Cache-aside: app manages cache explicitly. Most flexible, most code. Use when flexibility > simplicity.
▶Redis vs Memcached vs Valkey vs KeyDB — when do I pick each?
Memcached: ultra-simple, distributed by default, no persistence — great for HTTP session store. Redis: rich data types (sorted sets, streams, pub/sub), persistence options, single-threaded — best for general app cache + real-time. Valkey: Redis drop-in successor (open-source fork), actively developed. KeyDB: threaded Redis (better perf on multi-core), commercial. For 2026: default to Redis/Valkey; Memcached only if you need zero operational overhead.
▶How do I prevent cache stampede (thundering herd)?
Cache stampede = many threads request stale key simultaneously, all recompute. Solutions: (1) lock-based: get lock, one thread recomputes, others wait for result; (2) probabilistic early expiry: expire key probabilistically before TTL (e.g., 10% at 90% of TTL); (3) background refresh: cron job refreshes hot keys before expiry. Combine locking + probabilistic expiry for robustness. Libraries: lua scripts in Redis, or app-level with semaphores.
▶TTL choice — how do I pick the right expiration time?
Data volatility: static content (1h-1d), user data (5-15m), prices/inventory (1-5m), real-time analytics (1m). Cold data: shorter TTL (waste if unused). Hot data: longer TTL. Monitor hit rates: if < 80%, TTL likely too short or working set too large. A/B test: measure latency + DB load at 5m vs 30m vs 1h. Start conservative (5m), increase if hit rate > 85%.
▶Browser vs CDN vs app cache — what should I cache where?
Browser: static assets (CSS, JS, images), immutable content. Cache-Control: max-age=1y for versioned, max-age=0 for HTML. CDN: same as browser but global + 24h TTL for user-specific data (unsafe). App (Redis): database query results, expensive computations, session state. Database query cache: only if rows are immutable (expensive joins). Stack them: browser + CDN + app cache = 1000x latency wins.
▶How do I test cache hit rates and measure impact?
Hit rate = (cache hits) / (total requests). Tools: Redis MONITOR + COUNT, CloudWatch metrics, APM (Datadog, New Relic). Target > 80%. Measure: (1) latency: p99 with vs without cache (should be 10-100x faster); (2) DB load: query count with vs without cache; (3) revenue impact: slower pages = fewer conversions. A/B test on prod: cache on vs off.
▶Distributed caching consistency — strong vs eventual? How to debug stale data?
Strong consistency: write to primary, replicate to all caches, block until ack (slow, safe). Eventual consistency: write to primary, async replicate, return immediately (fast, stale reads possible). For most cases: eventual + TTL. Debugging: (1) log cache version + DB version on response; (2) add cache-buster header (e.g., X-Cache-Key); (3) CloudWatch dashboard with hit rate + staleness lag; (4) A/B test: cache on/off user group. Stale > slow > wrong.