Back to Backend Developer
Detail

Caching Strategies

Cache aggressively and correctly to reduce latency and database load.

Caching layers:

1. Application cache (in-process): Simple key-value in memory. Lost on restart.
2. Distributed cache: Redis, Memcached. Shared across instances.
3. CDN cache: Static assets, edge responses.
4. Database query cache: Short-lived result caching.

Strategies:
- Cache-aside (lazy loading): Read from cache, miss → read DB → write cache.
- Write-through: Write to cache and DB together.
- Write-behind (write-back): Write cache first, async flush to DB.
- Read-through: Cache sits in front of DB, handles misses automatically.

Cache invalidation:
- TTL-based expiry
- Event-driven invalidation (pub/sub)
- Cache tags / groups
- Stale-while-revalidate

Common pitfalls:
- Cache stampede (thundering herd) — use mutex/lock or probabilistic early expiry
- Stale data serving longer than expected
- Cache poisoning