Web Performance, Caching & CDNs

an ETag

/ EE-tag /

Suppose you photocopied a document and want to know later whether the original has changed — without reading the whole thing again. The simplest trick is a short fingerprint: the office writes a code on each version, and you just compare codes. If the code on the original still matches the code on your copy, nothing changed. An ETag (entity tag) is exactly that fingerprint for a web resource. The server attaches it as a response header — for example ETag: "a17f9" — to identify this particular version of the content.

Caches use it for cheap revalidation. When a cached copy goes stale, the cache doesn't re-download the whole resource; it sends a conditional request with the header If-None-Match: "a17f9", which asks do you still have version a17f9? The server compares the tag against the current content. If they match, it replies 304 Not Modified with an empty body — confirming the stored copy is still good — and only the tiny header travels. If the content has changed, the server sends the full new body together with a new ETag. The tag itself can be a hash of the content or a version counter; the only requirement is that it changes when the content does.

Why it matters: ETags make staleness cheap to resolve. Instead of choosing between always re-fetching (wasteful) and risking stale data (incorrect), a cache can keep a copy long, then confirm it with a single round trip that usually returns a near-empty 304. ETags complement the Last-Modified date (a timestamp-based validator) and are often more precise, since two edits within the same second still produce different tags. One subtlety: a weak ETag (written W/"...") means semantically equivalent rather than byte-for-byte identical, which suits responses that differ only in compression or trivial whitespace.

Your browser holds a cached profile.json tagged "v12". On the next visit it sends If-None-Match: "v12". The data is unchanged, so the server returns 304 Not Modified with no body — saving the whole download but confirming the copy is current.

Compare fingerprints instead of re-downloading: a 304 confirms the copy.

An ETag identifies a version; it is not a checksum for integrity or a security token. It only tells a cache whether its copy matches the current one — it does not prove the content is authentic or untampered (TLS does that).

Also called
entity tag實體標籤ETag 標頭