Lesson 21 / 24

Caching with ETags

Letting clients skip re-downloading data that hasn't changed.

Cache-Control sets the rules

Cache-Control: max-age=60 tells clients and proxies a response is fresh for 60 seconds — no request needed at all in that window. no-store says never cache it (sensitive data).

ETags for revalidation

An ETag is a fingerprint of the current representation. The client resends it as If-None-Match; if unchanged the server replies 304 Not Modified with no body — saving bandwidth while confirming freshness.

The revalidation round-trip

The client always sends the request, but a match keeps the body off the wire.

GET /orders/482 HTTP/1.1
If-None-Match: "a1b2c3"

HTTP/1.1 304 Not Modified
ETag: "a1b2c3"

Quick check: A client sends `If-None-Match` with an ETag that still matches the current resource. What should the server return?

  • 200 OK with the full body
  • 304 Not Modified with no body
  • 404 Not Found
Answer

304 Not Modified with no body — A matching ETag means the client's cached copy is still valid, so the server confirms freshness with 304 and skips resending the body.