Skip to content

Lesson 1 of 9 · 24m

Cache-Control Is a Contract, Not a Header Sprinkle

Understand freshness, cacheability, and the difference between browser and shared caches before setting max-age values.

The worst caching bug I ever inherited was one line long: a well-meaning proxy rule added Cache-Control: public, max-age=86400 to every JSON response. Sessions did not leak immediately because most users were alone on their laptops. Then the corporate proxy hit production traffic and one customer started receiving another customer's dashboard. Caching is not a performance garnish. It is a correctness contract.

The freshness model

An HTTP cache stores a response and may reuse it while it is fresh. Freshness usually comes from Cache-Control: max-age=N; validation comes from ETag or Last-Modified. Fresh does not mean correct forever. It means the origin explicitly authorized reuse for that duration.

HTTP/2 200 OK
content-type: application/json
cache-control: private, max-age=60
etag: "v123"

private means a browser cache may store it. public means shared caches may store it if other rules permit. no-store means do not retain a copy. no-cache does not mean do not cache; it means store, but revalidate before reuse. Yes, the names are terrible. Learn them anyway.

Shared cache or private cache

Before touching headers, answer two questions: who is allowed to reuse this representation, and what inputs select it? A personalized API object is normally private. Anonymous marketing HTML can be forums. Authenticated responses should not become public merely because a CDN can technically cache them.

browser cache       one user's private cache
forward proxy       an organization's shared cache
reverse proxy/CDN   your edge network's shared cache
service worker      application-controlled browser cache

Each layer has different eviction, purge, and debugging behavior. A response can be correct in one layer and dangerous in another.

Method and status matter

Caches primarily reuse safe methods such as GET and HEAD. POST responses are not normally reusable unless explicitly designed that way. Error responses can be cached too. 404 and 500 caching are common outage multipliers when a CDN sees a temporary origin failure and stores it.

location /static/ {
    expires 7d;
    add_header Cache-Control "public, immutable" always;
}

location /api/ {
    proxy_pass http://app;
    add_header Cache-Control "private, no-cache" always;
}

Do not copy this blindly; the point is that static assets and personalized APIs need different contracts.

The day-one rule

If the response depends on who the caller is, default to private, no-cache until you can prove otherwise. If bytes are content-addressed, such as app.4f2a91.js, long-lived immutable caching is safe because the URL changes when the content changes. If the URL does not change, long TTLs are a freshness risk.

Field notes

  • no-cache permits storage but requires validation; no-store is the stronger instruction.
  • max-age is not a guarantee of retention. Caches evict early under memory pressure.
  • Browsers heuristically cache some responses without explicit headers; be explicit for anything important.
  • Shared caches must honor authorization boundaries. Never rely on an Authorization header being absent from a cache key by accident.
  • A surprising amount of caching behavior comes from intermediaries you do not control.

Was this lesson helpful?

Sign in to rate