Skip to content

Caching Strategies — Detailed#

flowchart TB
  subgraph Tiers[Cache Tiers]
    B[Browser Cache]
    CDN[CDN / Edge Cache]
    REV[Reverse Proxy<br/>Varnish/NGINX]
    APP[App Local<br/>Caffeine/Guava]
    DC[Distributed Cache<br/>Redis/Memcached]
    DBC[DB Buffer Pool]
  end

  subgraph Patterns[Patterns]
    CA[Cache-Aside<br/>look-aside]
    RT[Read-Through]
    WT[Write-Through]
    WB[Write-Back<br/>write-behind]
    WA[Write-Around]
    REF[Refresh-Ahead]
  end

  subgraph Eviction[Eviction]
    LRU
    LFU
    TLRU[TTL+LRU]
    ARC
    TwoQ[2Q / TinyLFU]
  end

  subgraph Failures[Failure Modes]
    STAMP[Cache Stampede<br/>thundering herd]
    PEN[Cache Penetration<br/>key never exists]
    AVAL[Cache Avalanche<br/>mass expiry]
    INV[Stale data /<br/>invalidation bugs]
  end

  subgraph Mitigations[Mitigations]
    LOCK[Single-flight /<br/>distributed lock]
    BLOOM[Bloom filter for<br/>missing keys]
    JIT[Jittered TTL]
    NEG[Negative caching]
    VER[Versioned keys]
  end

  Client --> B --> CDN --> REV --> APP --> DC --> DBC
  Client --> Patterns
  Patterns --> Eviction
  Failures --- Mitigations
  STAMP -.->|fix| LOCK
  PEN -.->|fix| BLOOM
  PEN -.->|fix| NEG
  AVAL -.->|fix| JIT
  INV -.->|fix| VER

    classDef client fill:#dbeafe,stroke:#1e40af,stroke-width:1px,color:#0f172a;
    classDef edge fill:#cffafe,stroke:#0e7490,stroke-width:1px,color:#0f172a;
    classDef service fill:#fef3c7,stroke:#92400e,stroke-width:1px,color:#0f172a;
    classDef datastore fill:#fee2e2,stroke:#991b1b,stroke-width:1px,color:#0f172a;
    classDef cache fill:#fed7aa,stroke:#9a3412,stroke-width:1px,color:#0f172a;
    classDef queue fill:#ede9fe,stroke:#5b21b6,stroke-width:1px,color:#0f172a;
    classDef compute fill:#d1fae5,stroke:#065f46,stroke-width:1px,color:#0f172a;
    classDef storage fill:#e5e7eb,stroke:#374151,stroke-width:1px,color:#0f172a;
    classDef external fill:#fce7f3,stroke:#9d174d,stroke-width:1px,color:#0f172a;
    classDef obs fill:#f3e8ff,stroke:#6b21a8,stroke-width:1px,color:#0f172a;
    class CDN,REV edge;
    class CA,RT,WT,WB,WA,REF,TLRU,STAMP,PEN,AVAL,INV,LOCK,BLOOM,JIT,NEG,VER service;
    class B,APP,DC,DBC,TwoQ cache;

Pattern semantics#

Pattern Read Write Notes
Cache-aside app reads cache, falls back to DB, fills cache app writes DB, invalidates cache most common
Read-through cache transparently loads from DB on miss cache library does the load
Write-through app writes cache → cache writes DB synchronously consistent, slower writes
Write-back app writes cache → cache async-flushes to DB fast writes, durability risk
Write-around app writes DB, cache filled lazily on read avoids cache pollution by one-time writes
Refresh-ahead proactively refresh hot keys before TTL predictive

Invalidation#

  • TTL (simplest, drifts).
  • Explicit on write (DEL, pub/sub).
  • Versioned key (user:42:v17) — never invalidate, just bump version.
  • Change-Data-Capture (Debezium) → cache invalidator.

Glossary & fundamentals#

Concepts referenced in this design. Each row links to its canonical page; the tag column shows whether it is a high-level (HLD) or low-level (LLD) concept.

Tag Concept What it is Page
HLD Load balancer / GSLB L4/L7 traffic distribution and failover load-balancer
HLD CDN edge caching for static assets cdn
HLD Cache strategies cache-aside, read/write-through, eviction caching-strategies
HLD Pub/Sub & message brokers topics, consumer groups, delivery semantics pub-sub-pattern
HLD Probabilistic data structures Bloom, HLL, Count-Min, MinHash, t-digest probabilistic-data-structures
HLD Idempotency & retries safe re-execution, backoff + jitter idempotency-retries
HLD Change Data Capture WAL/binlog tailing, outbox publishing change-data-capture
LLD Structural patterns Adapter, Decorator, Facade, Proxy, Composite structural-patterns