The header that decides what your cache stores
Vary is the single most misunderstood caching header. It tells every cache —
the browser, a CDN, a reverse proxy — which request headers must be part of the
cache key. Get it right and content negotiation works through caches; get it
wrong and you either serve the wrong variant or shred your hit rate. This
reference rates the common Vary field names by safety and cache impact.
How it works
When a response includes Vary, a cache stores one entry per distinct
combination of the listed request-header values:
Vary: Accept-Encoding, Accept-Language
A request only matches a stored entry if its values for those headers match. So
varying on Accept-Encoding keeps gzip and Brotli bodies separate (correct),
while varying on Cookie or User-Agent fragments the cache into near-unique
entries (harmful). CDNs often normalise values — collapsing dozens of
Accept-Encoding strings down to gzip/br/identity — to keep the key small.
Tips and pitfalls
- Always
Vary: Accept-Encodingfor compressed responses. - Avoid
Vary: CookieandVary: User-Agenton cacheable assets — they bust caches. Vary: Originis needed for CORS responses cached across origins.Vary: *makes a response uncacheable by shared caches — use sparingly.- Keep the list minimal; every extra field multiplies the number of stored variants.