Lesson 6 of 9 · 28m
HTTP Caching as a Designed System, Not a Header Accident
Design immutable static caching, HTML revalidation, shared cache behavior, invalidation, compression, and cache keys that survive real deployment workflows.
Caching is where performance teams accidentally become correctness engineers. A cache that is too timid wastes bandwidth and server time. A cache that is too aggressive serves stale navigation, personalized data to the wrong user, or last week's JavaScript against today's HTML. You cannot treat Cache-Control as a copied header. You have to design object lifetimes and invalidation semantics.
Separate immutable assets from mutable documents
Static assets should be content-hashed and effectively immortal:
cache-control: public, max-age=31536000, immutableThe filename changes when the contents change:
<link rel="stylesheet" href="/assets/app.9d34f21c.css">
<script defer src="/assets/runtime.2ab71e90.js"></script>HTML is different. It is the manifest that points at those immutable assets. For dynamic or frequently deployed sites, HTML usually needs revalidation or short freshness:
cache-control: no-cache
etag: "v2026-11-30-4-en"no-cache does not mean 'do not store.' It means the cache must revalidate before reuse. no-store is the stronger directive for sensitive responses.
Know who is allowed to store what
private permits browser caching but not shared intermediary caching. public permits shared caches, subject to other rules. Personalized data is not automatically safe because the URL contains a user ID:
cache-control: private, no-cache
vary: cookie, accept-encodingVary is a cache-key contract. If responses differ by cookie, language, encoding, or device negotiation and the header is absent or wrong, shared caches can collapse distinct variants. Conversely, careless Vary: User-Agent fragments the cache into uselessness.
Compression is part of cache design
Brotli for compressible static text, gzip as fallback, and nothing extra for already-compressed binary media:
HTML/CSS/JS/SVG/JSON: brotli/gzip; woff2 and image formats: already compressedAt the edge, correct negotiation requires Accept-Encoding in the cache key. Most CDNs normalize this internally, but custom proxies can break it spectacularly. Test encoded and identity responses instead of assuming.
Invalidation is the hard part because deployments are systems
A common failure: HTML cached too long references a deleted hashed asset. Users receive the old HTML, request /assets/app.oldhash.js, get a 404, and the application never boots. Deployment order matters:
1. upload new assets; keep old assets available
2. deploy HTML pointing to new assets; purge or expire HTML
3. remove old assets only after old HTML is goneIf your CDN caches HTML for 10 minutes, deleting old assets immediately is a self-inflicted outage.
Do not cache failure casually
A 500, 502, or deployment error should not pick up a year-long lifetime because a blanket rule matched every response. Configure cache behavior by status as well as path. Negative caching can be useful for 404s on static assets, but minutes are usually enough. Do not turn one bad deploy into a day-long incident artifact.
Field notes
max-age=0andno-cacheare not exactly the same in operational behavior; know which intermediary layers normalize them differently.- Query-string cache busting works only if the CDN includes query strings in the cache key as expected. Filename hashing is cleaner.
- Service workers add a second cache layer. Browser cache correctness does not imply service worker correctness.
- Test cache behavior after deploys, rollbacks, and partial CDN purges. The normal page load often hides lifecycle failures.
Was this lesson helpful?