04-backend-knowledge

Caching

04-backend-knowledge/caching

Caching

Overview

Caching stores computed or fetched results so later requests can be faster or cheaper.


Why It Matters

Caching can improve speed dramatically, but stale data or invalidation bugs can also create confusing issues.


Core Concepts

  • Cache data with a known lifetime or invalidation rule.
  • Cache at the right layer.
  • Stale data is a trade-off, not an accident.

Mental Models

Ask what is being optimized: latency, load, cost, or availability.


Best Practices

  • Define invalidation upfront.
  • Measure hit rate and staleness impact.
  • Keep cache keys predictable.

Common Mistakes

  • Caching everything.
  • Forgetting to invalidate.
  • Treating stale data as a harmless default.

Trade-offs

More cache usually means better performance and worse freshness. The right balance depends on the use case.


Decision Framework

Cache useGood for
CDN / edgeStatic assets and public responses
App cacheReused computed data
Client cacheSmooth UI and fewer refetches

Examples

  • Cache a product list briefly, but invalidate it when inventory changes.

Checklists

  • What invalidates this cache?
  • What happens if it is stale?
  • Do we know how to observe hit rate and misses?

Senior Engineer Notes

Senior engineers treat caching as a correctness trade-off as much as a performance win.


Further Reading