What is an ETag (Entity Tag)?
An ETag is an HTTP response header that provides a validator – a token representing the state of a specific representation (usually a specific body bytes).
ETag is a hash identifier the server attaches to a resource so the client can check if the resource has changed without downloading it again.
Think of it as a fingerprint for the response entity.
- Header name: ETag
- Example values:
- ETag: “33a64df551425fcc55e4d42a148795d9f25f89d4”
Purpose
- Help caches and clients determine whether a resource has changed.
- Enable conditional requests (
If-None-Match,If-Match) to avoid sending full payloads and to implement concurrency controls.
Why ETag Exists
Imagine your browser already downloaded a 5 MB image yesterday. You revisit the page today.
Should the browser download the whole file again?
No.
“Has the file changed since I last downloaded it?”
If not changed -> server responds 304 Not Modified (no download).
If not -> server sends new file -> new ETag.
How It Works
1 Server sends a resource with an ETag
HTTP/1.1 200 OK
ETag: "abc123xyz"
Content-Type: image/png
The ETag here “abc123xyz” uniquely identifies this version of the file.
2 Client stores the ETag
Browser caches:
- The file
- The ETag “abc123xyz”
3 Later, client makes a conditional request
Client asks the server:
If-None-Match: "abc123xyz"
Meaning:
“Send the file only if it does not match this version."
4 Server compares the ETag
Case A: File unchanged
Server returns:
HTTP/1.1 304 Not Modified
Case B: File changed
Server returns:
HTTP/1.1 200 OK
ETag: "newetag456"
Browser downloads new version and updates cache.
Benefits of ETag
| Feature | Explanation |
|---|---|
| 💨 Faster | No need to re-download unchanged files. |
| 📉 Saves bandwidth | 304 responses are tiny. |
| 🗂 Better cache control | More precise than Last-Modified. |
| 🔄 Handles fast changes | Detects changes even if they happen within 1 second. |
Strong vs Weak ETags
Strong ETag
Exact match of bytes.
ETag: "abc123"
Means:
Bit-for-bit identical
Weak ETag
Only semantically identical.
ETag: W/"abc123"
Content is effectively the same, but not necessarily byte-for-byte.
Leave a comment
Your email address will not be published. Required fields are marked *
