awesome-everything RU
↑ Back to the climb

Caching

Cache-Control: header reading

Crux Read real Cache-Control headers and a CDN response, predict how browser and CDN behave, and pick the correct directive for the scenario.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

Cache bugs are read off the wire — in the response headers and the CDN’s cache-status line. Read each header set, predict what every tier in the chain does with it, then pick the fix a senior engineer would ship.

Goal

Practise the loop you run in every caching incident: read the directives, reason about browser vs shared-cache behaviour separately, and choose the directive that matches the data’s sharing and freshness needs.

Snippet 1 — the “secure” endpoint

GET /api/account/balance HTTP/1.1

200 OK
Cache-Control: no-cache
Content-Type: application/json
Quiz

This per-user balance endpoint ships no-cache and no ETag. What is the real behaviour, and what should it ship instead?

Snippet 2 — HTML behind a CDN

200 OK
Cache-Control: public, max-age=31536000
Content-Type: text/html; charset=utf-8
Quiz

This header is on the site's index.html, served through a CDN. What goes wrong, and what is the correct header for HTML that references content-hashed assets?

Snippet 3 — no-cache vs no-store, side by side

# Response A
Cache-Control: no-cache, private
ETag: "v9"

# Response B
Cache-Control: no-store
Quiz

A search-results page that is cheap to revalidate and not sensitive uses Response A; a password-reset page uses Response B. Is each choice right?

Snippet 4 — the CDN response line

200 OK
Cache-Control: max-age=60, s-maxage=300, stale-while-revalidate=3600
Age: 420
CF-Cache-Status: HIT
Quiz

The CDN returns this for a JSON API. Age=420 exceeds s-maxage=300, yet it is a HIT. Is this correct, and what is the client getting?

Recap

Every caching bug is read from headers. no-cache without an ETag on per-user data is a stored, inefficiently-revalidated leak — ship private, no-store instead. A year-long max-age on HTML freezes deploys, because the rule is to hash assets and keep HTML revalidating. no-cache plus an ETag is the right choice for cheap, non-sensitive content that reuses via 304; no-store is for data that must never be stored at all. And a CDN HIT with Age past s-maxage is correct under stale-while-revalidate — stale-but-instant by design, with a background refresh. Read the directives per tier, match them to the data’s sharing and sensitivity, then verify on the wire.

Continue the climb ↑Cache-Control: design and verify a caching policy
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources2
expand
  1. 01
  2. 02

Trademarks belong to their respective owners. Editorial reference only.