Vary header

On this page 7

The Vary header is an HTTP response header that lists the request headers whose values influenced the response, so that any cache storing it reuses that response only for later requests with matching values. In content delivery, it is the mechanism by which an origin server tells browsers, proxies and CDN edge caches that one URL can have several valid representations, for example compressed and uncompressed, or English and German.

Defined in the HTTP semantics and caching specifications (RFC 9110 and RFC 9111), Vary sits at the center of a constant tension: it protects users from receiving the wrong variant, but every header it names splits the cache and can sharply lower the cache hit ratio.

How the Vary header controls cache matching

A cache normally identifies a stored response by its cache key, at minimum the method and URL. When the stored response carries Vary: Accept-Encoding, the cache also records the request’s Accept-Encoding value and serves the stored copy only if a new request presents an equivalent value for that header.

If the values differ, the result is a cache miss and the request goes to the origin or to a parent cache. The special value Vary: * signals that the response depends on factors outside the request headers, which in practice makes it effectively uncacheable for shared caches.

The following table shows how different Vary values affect matching.

Vary valueSelected byEffect on caching
Accept-EncodingCompression support such as gzip or brFew variants, usually safe
Accept-LanguagePreferred language listMany variants unless normalized
OriginRequesting site in CORSOne variant per calling origin
User-AgentBrowser and device stringThousands of variants, very low reuse
CookieSession or preference cookiesOften one variant per user
Asterisk valueUnknown or external factorsResponse not reused by shared caches
Examples of Vary values and their effect

Content negotiation and the Vary header

Vary is the caching counterpart of proactive content negotiation, in which the server chooses a representation based on request headers. Whenever a server changes its output according to a request header, it must list that header in Vary; otherwise a shared cache may hand one user’s variant to another.

The classic failure is a server that returns gzip-compressed content to a client that sent Accept-Encoding: gzip without adding Vary. A proxy then serves the compressed bytes to an old client that cannot decode them, producing garbled pages. The same pattern with CORS headers causes intermittent cross-origin errors when Access-Control-Allow-Origin is cached for the wrong origin.

The table below lists common negotiation scenarios and the header each one requires.

Server behaviorRequired Vary valueRisk if omitted
Compresses responses on demandAccept-EncodingUndecodable bytes sent to clients
Serves WebP or AVIF by browser supportAcceptUnsupported image format delivered
Echoes the caller in CORS headersOriginBlocked cross-origin requests
Localizes by browser languageAccept-LanguageVisitors see the wrong language
Uses client hints for image widthSec-CH-Width or similarOversized or blurry images
Negotiation scenarios and the required Vary value

How CDNs handle the Vary header

CDN edge caches treat Vary with more caution than browsers, because a single edge object serves very large audiences. Many CDNs honor Accept-Encoding natively and normalize its value into a small set such as gzip, br or identity, while other Vary values either fragment the cache or cause the object to be treated as uncacheable.

Because behavior differs between providers, operators often move variation out of Vary and into an explicit, normalized cache key configured at the edge. For example, an edge rule can map a long Accept-Language string to one of five supported languages and add only that token to the key, which keeps correctness and hit ratio at the same time.

ApproachCorrectnessCache efficiencyTypical use
Raw Vary headerHigh if completeLow for high-cardinality headersSimple origins without edge logic
Normalized cache keyHighHighLanguage, device class, image format
Separate URLs per variantHighHighestLocalized paths such as /de/ and /en/
No caching of variantsHighNonePersonalized or authenticated pages
Approaches to serving variants through a CDN

Best practices for using Vary

Good practice is to list every header that actually changes the response, and nothing else. Each extra header multiplies the number of stored variants, so the guiding question is the cardinality of the header values that will really arrive at the cache.

  • Always send Vary: Accept-Encoding when compression is negotiated, even if the CDN compresses at the edge.
  • Avoid Vary on User-Agent; detect a few device classes at the edge and key on the class instead.
  • Avoid Vary on Cookie for public pages; strip irrelevant cookies before the cache lookup.
  • Combine Vary with suitable Cache-Control directives, such as private for user-specific responses.
  • Test with repeated requests that change one header at a time and inspect cache status headers from the CDN.

Common mistakes with the Vary header

Mistakes with Vary fall into two opposite failure modes: too little Vary breaks correctness, too much Vary destroys performance. Both are hard to see in development, where a single browser rarely exposes cache sharing.

SymptomLikely causeFix
Intermittent CORS failuresMissing Vary: OriginAdd Origin to Vary or return a fixed allowed origin
Wrong language served at randomMissing Vary: Accept-LanguageAdd Vary or use language-specific URLs
Hit ratio far below expectationsVary on User-Agent or CookieNormalize into a small cache key
Purges seem to miss some usersStale variants stored separatelyPurge by URL across all variants or by cache tag
Symptoms, causes and fixes

The simple explanation

Imagine a bakery that keeps ready-made sandwiches behind the counter to serve people faster. If some customers want gluten-free bread, the staff must check what each customer asked for before handing over a stored sandwich.

The Vary header is the label on the stored item saying which question to ask first: which compression the browser understands, which language it prefers or which site it came from. Without that label, a cache might give someone the wrong version. With too many labels, almost nothing can be reused, so the art is to ask only the questions that matter.

How it is used in practice

An online magazine served its articles in four languages from the same URLs, choosing the language from Accept-Language, and sent Vary: Accept-Language correctly. Its CDN, however, saw several thousand distinct header strings per day, so most requests missed the edge cache and the origin handled about 70 percent of traffic.

The engineering team added an edge rule that reduced each incoming header to one of five tokens, the four languages plus a default, and placed that token in the cache key while removing the raw header from Vary at the edge. Responses remained correct for every visitor.

Within a week the cache hit ratio rose to roughly 92 percent, origin load fell accordingly and median page delivery time dropped noticeably for distant readers, without any change to article content.