Scope of this article
- Environment
- Header examples require review against actual CDN behavior and privacy requirements.
A cache is a second copy of truth with different freshness and failure rules. Its value is not that Redis or a CDN is fast; it is that the application can state which version of data a reader may receive, for how long, and what happens when the source changes.
Three decisions before choosing a cache#
What is the key? Include every dimension that changes the representation: locale, currency, tenant, permission scope and query parameters.
How stale may the value be? Public article lists can tolerate delay; inventory and entitlements often cannot.
What triggers invalidation? TTL alone, write-through, event-driven purge or versioned keys each have distinct failure cases.
HTTP caching is not database caching#
Layer | Good candidate | Primary hazard |
|---|---|---|
Browser | Hashed JS/CSS assets | Private API responses cached inadvertently |
CDN | Public article HTML / images | Serving stale personalized HTML |
Application / Redis | Catalog aggregation / expensive read models | Stampede and stale permission keys |
Database buffer cache | Frequently accessed pages/indexes | Memory-pressure and IO competition |
# Public, versioned static assets
Cache-Control: public, max-age=31536000, immutable
# Public article listing where brief staleness is acceptable
Cache-Control: public, max-age=60, stale-while-revalidate=300
# User-specific checkout / account response
Cache-Control: private, no-storeThe stale-while-revalidate directive permits reuse of eligible stale responses while the cache revalidates asynchronously, within the declared window [1]. It does not guarantee that all intermediary caches implement the behavior identically.
Avoid a cache stampede#
Use a bounded single-flight mechanism, per-key short lock, stale fallback where safe and jittered TTLs to prevent synchronized expirations. If a lock holder crashes, the system must recover rather than strand all readers.
Invalidation is part of the write path#
For a content update, commit the database change and then invalidate the affected cache keys. The crash window can still leave stale values; an event/outbox or short TTL bounds it. For sensitive permissions and pricing, prefer direct authoritative checks rather than relying on a best-effort purge.
Measure the right metrics#
Hit rate together with origin load; a high hit rate can hide expensive rare misses.
Age of served objects and invalidation propagation latency.
p95 latency on hits, cold misses and cache-unavailable fallback.
Key cardinality and memory usage, especially if request IDs or full URLs enter keys.
Cross-user/tenant leakage tests for every cacheable response type.
Sources
- [1]MDN — Cache-Control
Mozilla · publication date unknown · HTTP · retrieved 2026-11-02
Spotted an error? Use “Suggest a correction” in the discussion, or read our corrections policy.
Discussion
Sign in to join the discussion. Reading is always open.
Loading discussion…