HTTP caching
IntermediateHow the browser and a CDN keep copies of files: Cache-Control and max-age, revalidating with an ETag and 304 Not Modified, and file names that change with every version.Builds on HTTP requests and responses
GET /app.js HTTP/1.1Host: example.com HTTP/1.1 200 OKCache-Control: max-age=60ETag: "v7" GET /app.js HTTP/1.1Host: example.comIf-None-Match: "v7" HTTP/1.1 304 Not ModifiedCache-Control: max-age=60ETag: "v7" GET /app.3f9a1c.js HTTP/1.1Host: example.com HTTP/1.1 200 OKCache-Control: public, max-age=31536000, immutableEmpty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
3 min read
On this page
What is HTTP caching?
The fastest request is the one that never happens. HTTP caching lets the browser, and caches along the way, keep a copy of a response and reuse it for later requests.
- The browser cache keeps copies for one person.
- A keeps copies on servers close to many visitors, so they skip the long trip to the site's own server, the origin.
The server decides what may be cached and for how long, with the Cache-Control header on each
response.
How do freshness and revalidation work?
- Fresh.
max-age=60makes a copy fresh for 60 seconds. While it is fresh, the cache uses it without sending any request. - Stale, then revalidated. After that the copy is stale, but it is not thrown away. The cache
sends a conditional request:
If-None-Matchwith the copy'sETag(a name for that version), orIf-Modified-Sincewith itsLast-Modifieddate. If nothing changed, the server answers304 Not Modifiedwith no body, and the copy is fresh again. - Who may keep it.
publiclets shared caches such as a CDN keep the response.privatekeeps it in the user's own browser only, for pages that belong to one person.s-maxagesets a different lifetime for shared caches. - Versioned file names. Build tools add a hash of the content to the name (
app.3f9a1c.js), so a new version is a new URL. Those files getmax-age=31536000, immutable, and the HTML that links to them is revalidated on every visit.
What do people get wrong about caching?
Mini example
A typical site sends three kinds of Cache-Control. The HTML is always checked, a personal page is
never stored, and a versioned stylesheet is kept for a year:
GET / HTTP/1.1Host: example.com HTTP/1.1 200 OKCache-Control: no-cacheETag: "home-41" GET /account HTTP/1.1Host: example.com HTTP/1.1 200 OKCache-Control: private, no-store GET /styles.8d21e0.css HTTP/1.1Host: example.com HTTP/1.1 200 OKCache-Control: public, max-age=31536000, immutableQuick check
A response came with Cache-Control: max-age=60 and an ETag. You reload the page 90 seconds later. What happens?
Further reading
All steps as text
- Step 1:
A page needs the script
/app.js. Before asking anyone, the browser looks in its HTTP cache, the copies it kept from earlier responses. It’s empty. - Step 2:
So the request goes out. example.com’s DNS points to a CDN, a network of caches close to visitors. This CDN server has no copy either.
Lines 1–2
- Step 3:
The CDN passes the request on to the origin server, the one that really has the file. This trip is the long one, possibly across the world.
- Step 4:
The origin sends the file with two caching headers.
Cache-Control: max-age=60says a copy is fresh for 60 seconds;ETagnames this exact version.Lines 4–6
- Step 5:
The CDN keeps a copy and passes the response on, and the browser keeps one too. The next visitor near this CDN server gets the file without reaching the origin.
- Step 6:
Thirty seconds later the page loads again. The copy is still fresh, so the browser uses it straight away: no request at all, not even to the CDN.
Line 5
- Step 7:
Two minutes later, the copy is older than 60 seconds: it is stale. The browser doesn’t throw it away. It asks whether it is still good.
- Step 8:
This is a conditional request.
If-None-Match: "v7"means: send the file only if your version is no longer "v7". The CDN’s copy is stale too, so it asks the origin.Lines 8–10
- Step 9:
The file hasn’t changed, so the origin answers
304 Not Modifiedwith no body. The copy you have is still right, and it is fresh for another 60 seconds.Lines 12–14
- Step 10:
The CDN passes the 304 on, and the browser reuses its copy. A few hundred bytes crossed the network instead of 48 KB.
- Step 11:
There is a catch: when a new version ships, visitors with a fresh copy keep the old one until it goes stale. So sites also put the version in the file name.
- Step 12:
The build tool names the file after a hash of its content:
app.3f9a1c.js. New code gets a new name, and the HTML points to it.Lines 16–17
- Step 13:
A file at that name never changes, so it can be cached for a year (
max-age=31536000), by any cache (public), with no revalidation at all (immutable).Lines 19–20
- Step 14:
Fresh copies are used with no request, stale ones are checked with a cheap 304, and versioned file names can be cached for a year.