The ETag (entity tag) HTTP response header is an opaque identifier for a specific version of a resource. Clients store it with the cached response and send it back later in If-None-Match, so the server can answer 304 Not Modified with no body when nothing changed. The result is less bandwidth and faster pages, and for write requests the same mechanism, via If-Match, prevents lost updates.
ETags are defined in RFC 9110 (HTTP Semantics), which replaced the older RFC 7232 text.
The server computes a validator for the current representation, such as a hash of the body or a version number, and sends it in the response:
The next time the client requests the same URL, it makes a conditional request:
If the representation is unchanged, the server replies with headers only:
The browser then reuses its cached body. If the resource changed, the server returns 200 OK with the new body and a new ETag. The client never interprets the value; it only compares it for equality.
| Type | Syntax | Meaning |
|---|---|---|
| Strong | "abc123" | The two representations are byte-for-byte identical |
| Weak | W/"abc123" | The two representations are semantically equivalent, though bytes may differ |
Weak validators are useful when a response is compressed or reformatted differently per request. Byte-range requests (Range with If-Range) require strong validators. For If-None-Match, a weak comparison is used, so weak ETags work for ordinary caching.
ETag does not say how long a response is fresh. Freshness comes from Cache-Control: max-age (or Expires). ETag only supplies a cheap way to revalidate once the cached copy is stale or when no-cache forces a check on every use.
| Header | Role |
|---|---|
Cache-Control | Defines freshness and who may cache |
ETag / If-None-Match | Revalidates by version identifier |
Last-Modified / If-Modified-Since | Revalidates by timestamp, one-second precision |
When both ETag and Last-Modified are present, If-None-Match takes precedence. ETags are more precise than timestamps, which cannot detect two changes within the same second.
Spring MVC can set the header directly on ResponseEntity, and it answers conditional requests for you:
When the request carries a matching If-None-Match, Spring MVC converts the response to 304 automatically for GET and HEAD requests where you set the ETag. For a no-code option, register ShallowEtagHeaderFilter, which hashes the rendered body and handles If-None-Match:
The shallow filter still runs the controller and builds the full response, so it saves bandwidth but not server work. Using a database version column as the ETag avoids that cost because you can compare before loading the body.
ETags also protect updates. The client sends the ETag it last saw in If-Match; if the resource has since changed, the server refuses the write with 412 Precondition Failed:
This prevents two users from silently overwriting each other's changes. Requiring the header at all can be signalled with 428 Precondition Required.
Vary header must match, because a compressed and an uncompressed variant need separate validators.It tells the client its cached copy is still valid. The response has no body, so the client reuses what it stored.
ETag is more precise and works for dynamic content, while Last-Modified is simpler and cheaper to produce. Sending both is common.
No. Cache-Control controls freshness; ETag validates a stale copy. They work together.
They can be abused that way, since a server can assign a unique value per client. Browsers partition caches today, which limits this, but you should never use ETags as an identifier.
An ETag is a version identifier sent by the server and echoed back in If-None-Match to get 304 Not Modified instead of a full response, and in If-Match to guard updates. Pair it with Cache-Control, use strong validators when exact bytes matter, and derive values from content or a version number so every server in a cluster produces the same one.