Caching For System Design: Redis, CDN, Cache Patterns Explained
A deep dive into caching for system design covering how caches work internally, where copies of data can live (in-process, Redis/Memcached, CDN, browser, database), and the tradeoffs of each layer. It walks through Redis internals (hash table resizing, approximated LRU/LFU eviction via sampling, lazy and active expiry), Memcached's slab allocation and rebalancing history, HTTP caching semantics (no-cache vs no-store, ETags, stale-while-revalidate, stale-if-error), CDN tiered caching, and why databases like MySQL removed their query cache. It also covers caching patterns (cache-aside, write-through, write-behind, refresh-ahead) with real vendor definitions that conflict, TTL and jitter strategies, consistent hashing, and failure modes such as cache stampedes, negative caching, hot keys, thundering herds on expiry, cold caches, and cache poisoning incidents (Steam, PayPal). It closes with a cost/benefit framework: miss ratio curves, working set sizing, and pricing comparisons between cache nodes and read replicas to decide whether caching is worth it.