Skip to content

Lesson 2 of 9 · 23m

ETags, Validators, and Conditional Requests

Use validators to trade bandwidth for correctness and understand when weak etags, Last-Modified, and 304 responses help or hurt.

A long TTL says 'trust this copy for a while.' Validation says 'ask whether this copy is still good.' Teams reach for validation when they want freshness without giving up caching, then discover they have merely moved latency around. Validators are powerful, but they are not magic.

The conditional GET dance

The origin emits a validator:

HTTP/2 200 OK
cache-control: private, no-cache
etag: "user-42-profile-v918"

On the next request, the cache or client sends it back:

GET /api/profile HTTP/2
if-none-match: "user-42-profile-v918"

If unchanged, the origin replies 304 Not Modified with no body. The client still pays a network round trip, but avoids transferring and reparsing the full representation. That is the deal: freshness and bandwidth savings, not zero latency.

Strong versus weak validators

A strong etag changes when the bytes change. A weak etag, prefixed with W/, means semantic equivalence rather than byte equality:

ETag: "abc123"      strong validator
ETag: W/"abc123"    weak validator

Weak etags are useful for representations where harmless encoding differences occur, but they do not work for range requests. Compression also complicates byte identity. If your proxy recompresses or mutates responses, a casually generated strong etag can become wrong.

Last-Modified is coarse

Last-Modified uses timestamps, normally with one-second granularity. That is fine for files and hopeless for fast-changing application state. Two writes in the same second can produce the same validator. Prefer content hashes or version counters where correctness matters.

last-modified: Tue, 18 Nov 2026 18:31:04 GMT
if-modified-since: Tue, 18 Nov 2026 18:31:04 GMT

Generate validators from meaning

Do not hash a response after serializing random key order. Generate validators from stable version inputs: object version, tenant-visible revision, or content digest. For example:

etag = f'W/"profile-{profile.id}-{profile.version}"'

Weak or strong depends on exact byte semantics, but the important part is that the validator changes when the meaningful resource changes. If it changes on every render because timestamps or random ordering leak into it, every conditional request becomes a useless 200.

304 still costs work

A conditional request reaches the origin. If the server queries twelve tables to compute the etag, validation has saved bandwidth but not database load. For expensive representations, store validator metadata cheaply or cache the composed representation itself. HTTP caching and application caching solve different parts of the cost curve.

Field notes

  • A 304 response must include the same cache metadata semantics as the original response; inconsistent headers create confusing behavior.
  • If-None-Match: * and precondition headers have meanings beyond cache revalidation. Do not improvise them.
  • ETags containing unbounded user input are both a correctness and injection headache. Keep them opaque and bounded.
  • Clock skew makes timestamp validators less trustworthy than revision counters.
  • Measure origin CPU saved, not only bytes saved.

Was this lesson helpful?

Sign in to rate