CodePark/ questions / Headers & HTTP caching
START PRACTICINGPRACTICE
Backend question · HTTP & API design › Headers & HTTP caching

What is an ETag, and how does this exchange save bandwidth?

GET /api/products/9 HTTP/1.1
If-None-Match: "a1b2c3"

HTTP/1.1 304 Not Modified
ETag: "a1b2c3"
Cache-Control: max-age=0, must-revalidate

An ETag is an ID for one version of a resource. It saves bandwidth because the server can answer "not changed" without sending the body again.

The ETag is opaque: the client does not read its meaning, it only compares it. It is often a hash of the content or a version number.

How the exchange works:

  • The server sends the ETag with the first full 200 response.
  • The client caches the body together with the tag.
  • Later, the client's copy is stale (too old to use without checking). The client sends a conditional GET with If-None-Match set to the stored tag.
  • If the resource has not changed, the server replies 304 Not Modified with no body. The client reuses its cached copy.
  • If it has changed, the server sends a normal 200 with the new body and a new ETag.

Last-Modified with If-Modified-Since works the same way, but uses timestamps. ETags are more precise. Weak ETags start with W/. They mean the two versions have the same meaning, not that they are identical byte for byte.

  • An ETag identifies one version of the representation.
  • The client revalidates with If-None-Match.
  • A 304 Not Modified response has no body, which saves bandwidth.
  • Last-Modified with If-Modified-Since is the date-based alternative.
  • Weak ETags start with the W/ prefix.