Skip to content
Sign inGet all access
Chapter 6·16 minFree

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: close
OutputHTTP/1.1 200 OK
GET / HTTP/1.1
Host: localhost
If-None-Match: "67ffa8c6-267"
Connection: close
OutputHTTP/1.1 304 Not Modified

848 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: close
OutputHTTP/1.1 200 OKServer: nginx/1.27.5Date: <now>Content-Type: text/htmlContent-Length: 615Last-Modified: Wed, 16 Apr 2025 12:55:34 GMTConnection: closeETag: "67ffa8c6-267"

If-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: close
OutputHTTP/1.1 304 Not Modified

A value that does not match gets the whole file again:

GET / HTTP/1.1
Host: localhost
If-None-Match: "stale"
Connection: close
OutputHTTP/1.1 200 OK

That 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: close
OutputHTTP/1.1 412 Precondition Failed

What a 304 costs

175 bytes against 848, and no body at all:

GET / HTTP/1.1
Host: localhost
If-None-Match: "67ffa8c6-267"
Connection: close
OutputHTTP/1.1 304 Not ModifiedServer: nginx/1.27.5Date: <now>Last-Modified: Wed, 16 Apr 2025 12:55:34 GMTConnection: closeETag: "67ffa8c6-267"

The 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: close
OutputHTTP/1.1 304 Not Modified

An 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: close
OutputHTTP/1.1 200 OK

Cache-Control

max-age is how long a response may be reused without asking:

terminal
{ 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 cache
OutputCache-Control: max-age=3600, public

For 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:

terminal
{ 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)'
OutputAge: 120Cache-Control: max-age=3600

no-cache is not no-store

The names are backwards from what they suggest:

terminal
{ 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'
OutputCache-Control: no-cacheETag: "v1"

no-cache stores and revalidates before reuse, which is why it ships an ETag. Only no-store keeps bytes off the disk:

terminal
{ 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 cache
OutputCache-Control: no-store

Vary

A cached response is keyed by URL. Vary adds request headers to the key:

terminal
{ 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 vary
OutputVary: Accept-Encoding

Vary: * means never reuse this for anyone:

terminal
{ 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 vary
OutputVary: *

Who 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:

terminal
{ 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 cache
OutputCache-Control: max-age=31536000, immutable

immutable stops the browser revalidating even on a reload.