Engineering Note

Architecture patterns for cache boundaries, stampede mitigation, TTL jitter, and deterministic invalidation.

Cache-Aside vs. Write-Through

Caching is not a substitute for proper database indexing. A high-performance caching layer complements a well-structured database by absorbing repeated reads on stable datasets.

In the Cache-Aside pattern, the application checks Redis first. On a cache hit, the result is returned immediately. On a cache miss, the application queries PostgreSQL, writes the result to Redis with an explicit TTL, and returns the response.

Mitigating Cache Stampedes & Hotkey Expiration

When a heavily requested cached key expires, thousands of concurrent requests can simultaneously miss the cache and hit the database at once, causing a cache stampede or thundering herd event.

Mitigate this by adding jitter (random variation) to TTL values to avoid synchronized expiration, and use distributed locks or probabilistic early expiration (XFetch algorithm) to recompute hot keys in the background before they expire.

Related Engineering Notes

/production-ready-apis

Designing Production-Ready APIs

A comprehensive guide to designing resilient REST APIs: deterministic contracts, idempotency keys, RFC 7807 problem details, and defense-in-depth rate limiting.

/avoiding-n-plus-one

Avoiding N+1 Database Queries

Techniques for eliminating silent database query amplification in ORMs through query planning, batching, and join projection.

← Back to engineering notes