Caching
ETag, Cache-Control, revalidation and the real 304.
Caching is in the protocol, not bolted on. Two headers do almost all of it.
Why it is in the protocol
Every response is reusable or not, and the server says which. One request, answered twice:
GET / HTTP/1.1
Host: localhost
Connection: closeGET / HTTP/1.1
Host: localhost
If-None-Match: "67ffa8c6-267"
Connection: close848 bytes against 175. The rest is how the client knew what to send.
ETag
A fingerprint the server chooses, opaque and compared only for equality. nginx derives one from the file’s timestamp and size:
GET / HTTP/1.1
Host: localhost
Connection: closeIf-None-Match
Send the ETag back and the server answers whether it still matches:
GET / HTTP/1.1
Host: localhost
If-None-Match: "67ffa8c6-267"
Connection: closeA value that does not match gets the whole file again:
GET / HTTP/1.1
Host: localhost
If-None-Match: "stale"
Connection: closeThat is the whole mechanism. A W/ prefix marks a weak ETag — equal enough to reuse, not
byte-identical — and still matches.
The same idea guards writes. If-Match fails loudly instead:
GET / HTTP/1.1
Host: localhost
If-Match: "stale"
Connection: closeWhat a 304 costs
175 bytes against 848, and no body at all:
GET / HTTP/1.1
Host: localhost
If-None-Match: "67ffa8c6-267"
Connection: closeThe round trip still happens; the body does not. max-age removes the round trip too.
Last-Modified
A timestamp instead of a fingerprint:
GET / HTTP/1.1
Host: localhost
If-Modified-Since: Wed, 16 Apr 2025 12:55:34 GMT
Connection: closeAn earlier date means the copy is older than the file, so it gets a new one:
GET / HTTP/1.1
Host: localhost
If-Modified-Since: Wed, 16 Apr 2024 12:55:34 GMT
Connection: closeCache-Control
max-age is how long a response may be reused without asking:
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Cache-Control: max-age=3600, public\r\n'
printf 'Content-Length: 2\r\n\r\nhi'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -i cacheFor an hour a cache serves this without reaching the server at all — unlike a 304.
| Directive | Means |
|---|---|
max-age=N |
reusable for N seconds |
no-cache |
reusable, but revalidate first |
no-store |
never write it down |
public |
any cache may keep it |
private |
only the browser may |
Age tells you how long a cache has held it — the response is 120 seconds into its hour:
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Age: 120\r\n'
printf 'Cache-Control: max-age=3600\r\n'
printf 'Content-Length: 2\r\n\r\nhi'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -iE '^(age|cache)'no-cache is not no-store
The names are backwards from what they suggest:
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Cache-Control: no-cache\r\n'
printf 'ETag: "v1"\r\n'
printf 'Content-Length: 2\r\n\r\nhi'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -iE 'cache|etag'no-cache stores and revalidates before reuse, which is why it ships an ETag. Only
no-store keeps bytes off the disk:
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Cache-Control: no-store\r\n'
printf 'Content-Length: 2\r\n\r\nhi'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -i cacheVary
A cached response is keyed by URL. Vary adds request headers to the key:
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Vary: Accept-Encoding\r\n'
printf 'Content-Length: 2\r\n\r\nhi'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -i varyVary: * means never reuse this for anyone:
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Vary: *\r\n'
printf 'Content-Length: 2\r\n\r\nhi'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -i varyWho is caching
public versus private decides which of four may keep a copy:
| Cache | Keeps private |
|---|---|
| Browser | yes |
| CDN | no |
| Corporate proxy | no |
| Your own server-side cache | it decides |
private is an instruction, not encryption and not a guarantee.
Cache busting
no-cache costs a round trip every time. A fingerprinted name costs none:
/app.css Cache-Control: no-cache
/app.9f2c1b.css Cache-Control: max-age=31536000, immutable
The URL changes when the content does, so the old answer is never wrong:
{ printf 'HTTP/1.1 200 OK\r\n'
printf 'Cache-Control: max-age=31536000, immutable\r\n'
printf 'Content-Length: 2\r\n\r\nhi'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -i cacheimmutable stops the browser revalidating even on a reload.