Skip to content
Dev Dump

Caching

Caching stores hot data in faster storage (memory/edge) so systems can:

  • reduce read latency,
  • protect databases/services from repeated reads,
  • increase throughput with the same backend capacity.

Caching is a performance optimization layer, not the source of truth.

Caching layers overview

  1. Client requests data.
  2. Cache lookup:
    • Hit -> return immediately.
    • Miss -> read from source, populate cache, return.
  3. Eviction policy decides what remains when capacity is full.

Cache hit and miss flow

  • Client/browser cache: static assets and API responses.
  • CDN/edge cache: geo-distributed static delivery.
  • Application cache: object/query/result caching near app tier.
  • Database cache/buffer pool: page/block-level acceleration.
  • DNS cache: domain resolution reuse.

Best candidates:

  • read-heavy data,
  • expensive-to-compute responses,
  • slow remote lookups,
  • data with acceptable staleness window.

Avoid caching:

  • highly volatile or strictly real-time data without strong invalidation controls,
  • low-reuse keys that pollute cache.
PolicyBest forTrade-off
LRUTemporal localityMay evict frequent older items
LFUStable popularity patternsExtra frequency metadata
FIFOSimplicityIgnores access behavior
TTL-basedFreshness controlSensitive to wrong TTL values
RandomVery low overheadUnpredictable quality

Caching correctness depends on invalidation strategy:

  • TTL expiry,
  • explicit delete/update on writes,
  • versioned keys,
  • event-driven invalidation.

How reads and writes coordinate with the cache is covered in Caching Strategies below.

Caching strategies define how data is read into cache, how writes reach cache and the source of truth, and how consistency, latency, and durability trade off.

Caching strategy map

Flow:

  1. app reads cache,
  2. on miss, app reads DB,
  3. app populates cache.

Pros:

  • simple and common,
  • good control in application layer.

Cons:

  • cache miss penalty hits request path,
  • stale data risk unless invalidation is handled well.

Flow:

  1. app asks cache,
  2. cache fetches from DB on miss,
  3. cache returns and stores value.

Pros:

  • cleaner app code,
  • central cache population logic.

Cons:

  • tighter coupling to cache/data adapter,
  • less app-level flexibility.

Read path strategies

Write to cache and DB in same path (usually sync).

Pros:

  • cache + DB stay closely aligned,
  • low data-loss risk.

Cons:

  • higher write latency,
  • DB write cost on every request.

Write to cache first; DB update happens asynchronously later.

Pros:

  • very low write latency,
  • absorbs bursty write traffic.

Cons:

  • risk of data loss on cache failure before flush,
  • more complex durability/replay pipeline.

Write directly to DB, skip cache on write.

Pros:

  • avoids polluting cache with cold write data,
  • simpler cache invalidation for write-heavy workloads.

Cons:

  • next read likely misses cache and hits DB.
  • Stale cache reads if the key is still present: the write updates only the DB, so you must invalidate or let the entry expire (TTL); otherwise clients can read an old cached value until then.

Write path strategies

StrategyRead LatencyWrite LatencyConsistencyData Loss Risk
Cache-AsideFast on hit, miss penaltyN/ADepends on invalidationLow
Read-ThroughFast on hit, managed missN/ASimilar to cache-asideLow
Write-ThroughGood read freshnessHigherStronger cache/DB alignmentLow
Write-BackGood after warm cacheLowestEventualHigher if flush fails
Write-AroundMiss after writesModerateDB-first correctnessLow
  • Cache-aside + write-through for inventory/price/order-adjacent reads.
  • Goal: fast reads with stronger write correctness.
  • Read-through + write-back for high-throughput timelines/interactions.
  • Goal: absorb spikes and trade strict immediacy for throughput.
  • Define cache invalidation ownership clearly.
  • Set TTLs by data volatility, not one global value.
  • Protect DB from thundering herd (request coalescing, jittered TTL).
  • Monitor hit ratio, eviction rate, stale-read incidents, flush lag.
  • Add fallback behavior for cache outages.
  • Prefer cache-aside as baseline for most services.
  • Use write-through for correctness-sensitive writes.
  • Use write-back only with strong durability controls.
  • Use write-around when write volume is high and re-read probability is low.