System Design Fundamentals
11 items
11 items
From browser to database - a complete guide to caching at every layer
Caching stores copies of frequently accessed data in faster storage to reduce latency and load on origin systems. Caches exist at every layer: browser, CDN, reverse proxy, application, and database. The key challenge is cache invalidation—ensuring cached data stays fresh. Different caching patterns (cache-aside, read-through, write-through, write-behind) offer different consistency-performance trade-offs. Understanding cache eviction policies (LRU, LFU, TTL) and problems like thundering herd and cache stampede is essential for building reliable cached systems.
Knowing when cached data is stale is genuinely difficult. Too aggressive invalidation defeats the purpose of caching. Too lazy invalidation serves stale data. There's no universal solution—each use case needs its own strategy.
Application checks cache first, falls back to database, then populates cache. It's explicit, flexible, and lets you cache exactly what you need. Most web applications use this pattern with Redis or Memcached.
Writing to cache and database together ensures cache is always fresh. But every write now has cache latency added. Use when read consistency matters more than write speed.
Caching stores copies of data in faster storage to reduce latency and origin load.
The speed hierarchy (latency): - L1 CPU cache: 1 ns - L2 CPU cache: 4 ns - RAM: 100 ns - Redis (local): 0.5 ms - Redis (remote): 1-5 ms - SSD: 0.1 ms - Database query: 1-100 ms - Network API call: 50-500 ms
Caching exploits this hierarchy—store frequently accessed data in faster tiers.
When to cache: - Data is expensive to compute/fetch - Data is accessed frequently - Data doesn't change too often - Stale data is acceptable (briefly)
Cache metrics:
Example calculation:
Origin latency: 100ms
Cache latency: 2ms
Hit rate: 90%
Avg latency = (0.9 × 2ms) + (0.1 × 100ms) = 11.8ms
88% latency reduction!