CodePark/ questions / HTTP caching & CDNs
START PRACTICINGPRACTICE
Frontend question · Browser & web performance › HTTP caching & CDNs

How do ETag and Last-Modified work, and what is a 304 response?

ETag and Last-Modified are validators. The browser uses them to ask "has this changed?" when a cached response is stale or marked no-cache. A 304 response means "no, reuse your copy".

How it works:

  • The server sends an ETag (an ID for this version of the content, often a hash), a Last-Modified date, or both.
  • When the browser needs to revalidate, it sends a conditional request. It includes If-None-Match: <etag> or If-Modified-Since: <date>.
  • If the content has not changed, the server replies 304 Not Modified with no body. The browser reuses its cached copy and marks it fresh again.
  • If the content has changed, the server sends a normal 200 with the new content.

This still costs a round trip, but it saves the download.

ETag is more precise. Last-Modified has only one-second resolution, and it depends on file timestamps.

  • The two validators are ETag and Last-Modified.
  • The browser sends conditional requests with If-None-Match or If-Modified-Since.
  • A 304 Not Modified response has no body, and the browser reuses its cached copy.
  • Revalidation saves bandwidth, but not the round trip.