Overview

Caching stores copies of frequently accessed data in faster storage layers to reduce latency and load on origin systems. Effective caching strategy requires understanding access patterns, data freshness requirements, and consistency trade-offs. Caches exist at every layer of a system — browser, CDN, reverse proxy, application, database query cache — and each layer has different invalidation semantics and failure modes. The two hardest problems in computer science are famously cache invalidation and naming things; caching strategy determines which problem you solve and which you accept as ongoing complexity.

Key Concepts

  • Cache-aside (lazy loading) — Application checks cache first, loads from database on miss, then populates cache. Simple but risks thundering herd on cold start.
  • Write-through — Writes go to cache and database simultaneously. Ensures consistency but adds write latency.
  • Write-behind (write-back) — Writes go to cache immediately, then asynchronously to database. Fast writes but risks data loss on cache failure.
  • TTL (Time to Live) — Cached entries expire after a set duration. Balances freshness against cache hit rate.
  • Cache invalidation — Actively removing stale entries. Event-driven invalidation is more precise than TTL but harder to implement.
  • CDN caching — Content Delivery Networks cache static and dynamic content at edge locations near users.
  • Thundering herd — When a popular cache entry expires and many concurrent requests hit the origin simultaneously. Mitigated by lock-based revalidation.

Trade-offs

  • Stale data risk increases with longer TTLs
  • Memory cost scales with cache size and cardinality
  • Distributed caches add network hops and consistency complexity
  • Over-caching masks performance problems in underlying systems
  • Load Balancing interacts with caching — cache locality vs distribution trade-offs
  • REST API Design leverages HTTP caching headers (ETag, Cache-Control)
  • Microservices need coordinated caching strategies across service boundaries