Performance Engineering: Scenario-Based Questions
64. What are effective caching strategies and how do you handle cache invalidation?
Caching improves performance and reduces load by serving data from memory or edge. However, cache invalidation is one of the hardest problems in computer science — getting it wrong can lead to stale data or outages.
🚀 Caching Layers
- Client-side: Browser or mobile app caching (e.g., HTTP cache, localStorage).
- Edge: CDN like Cloudflare, Akamai caches static/dynamic content.
- App/Service Layer: In-memory caches (Redis, Memcached) per service.
- Database Layer: Query result caching, read replicas.
📦 Caching Strategies
- Read-through: App fetches from cache; cache loads if miss.
- Write-through: Updates go to cache and DB simultaneously.
- Write-around: Bypass cache on write, refresh on read miss.
- TTL-based: Time-bound expiry (e.g., 60s freshness window).
🔄 Cache Invalidation Techniques
- Time-based: TTL or sliding expiration.
- Event-based: Invalidate cache on DB update (e.g., pub/sub).
- Manual: Admin flush or cache bust on deploys.
- Versioned keys: Use data versioning in cache key to force refresh.
🧰 Tools
- Redis, Memcached for app-level caching.
- CDNs with edge rules (Cloudflare Workers, Fastly).
- ORM-level cache decorators (e.g., Django cacheops, Spring Cache).
✅ Best Practices
- Use consistent keys and namespacing.
- Monitor hit ratio and eviction patterns.
- Build fallbacks for cache failures or cold starts.
- Document expiration policy per cache.
🚫 Common Pitfalls
- Stale cache served after underlying data changed.
- Hardcoded TTLs or mismatch with data update frequency.
- Cache stampede: simultaneous misses overwhelm DB.
📌 Final Insight
Caching can supercharge performance — but only with strong discipline around consistency and invalidation. Think of cache as a living part of your data pipeline, not a shortcut.