Skip to content
Dev Dump

Caching Fundamentals

  • Cache data that is read frequently, expensive to recompute, or slow to fetch from origin storage.
  • Key goals: reduce round-trips, minimize database load, and improve perceived responsiveness.
  • Cache capacity is limited and costly—focus on high-value datasets.

Overview Overview

  1. Client issues a read.
  2. Cache lookup executes:
    • Cache hit → return data immediately.
    • Cache miss → fetch from source of truth, populate cache, and return.
  3. Eviction policies decide what stays in cache when capacity is reached.

Request Flow 🔁

  • Web browsers: store static assets (HTML, CSS, JS, images) on disk.
  • CDNs: cache static and streaming content near users.
  • DNS resolvers: cache domain-to-IP mappings for quick lookups.
PolicyWhen it helpsTrade-offs
LRU (Least Recently Used)Temporal localitySimple, widely supported
LFU (Least Frequently Used)Stable access patternsMore metadata to track
FIFO (First-In First-Out)Simple queue semanticsIgnores recency/frequency
MRU (Most Recently Used)Bursty workloadsRarely ideal
TTL (Time-to-Live)Keep data freshRequires correct expiry windows
Random ReplacementLow overheadNon-deterministic outcomes
  • Write-through, write-behind, and cache-aside patterns (see Caching Strategies) keep cache contents aligned with the source of truth.
  • TTLs prevent stale data from lingering indefinitely.
  • Manual invalidation hooks are essential after bulk updates or schema changes.
  • Lower read latency and improved user experience.
  • Higher throughput using existing infrastructure.
  • Reduced load on primary databases or services.
  • Enables limited offline or degraded-mode operation for cached resources.
  • Staleness or inconsistency if invalidation is mishandled.
  • Cold-start latency while caches warm up.
  • Limited improvements for write-heavy workloads.
  • Additional operational complexity (evictions, replication, cache coherence).