Menu
Cloudflare Blog·September 22, 2026

Optimizing Cache Efficiency with HTTP Vary Header Management

This article discusses the challenges and solutions for managing the HTTP Vary header to optimize cache efficiency in distributed systems, particularly within a CDN context like Cloudflare. It explains how Vary prevents incorrect content delivery but can also lead to cache fragmentation if not handled properly, outlining Cloudflare's approach with Cache Rules to normalize or bypass variations.

Read original on Cloudflare Blog

The HTTP `Vary` header is a crucial, yet often problematic, mechanism in web caching. It informs intermediary caches, such as CDNs and reverse proxies, which request headers might influence the origin server's response. This is essential for ensuring that clients receive the correct content when a single URL can serve multiple representations (e.g., different languages, image formats, or compression schemes).

The Core Problem: Correctness vs. Efficiency

While `Vary` is vital for correctness, it introduces a significant challenge: cache fragmentation. If a cache treats every minor difference in a `Vary`-declared header as a distinct variant, it can lead to a "cold cache" problem. Thousands of barely different cache entries may be created, consuming capacity, increasing cache misses, and reducing overall efficiency, even if the actual response bodies are identical.

⚠️

Cache Fragmentation Example

Consider two `Accept-Language` headers: `en-US, fr;q=0.8` and `fr;q=0.8, en-GB`. If the origin only supports `en` and `fr`, both requests might receive the same English content. However, a naive cache would store them as two separate variants due to header differences, wasting space and reducing reuse.

Cloudflare's Solution: Granular Vary Control with Cache Rules

Cloudflare addresses this by separating the origin's declaration of variation (`Vary` header) from the cache's handling of those variations through Cache Rules. This allows administrators to define how each `Vary`-specified header should be processed.

  • Normalize: Recommended default. Cloudflare processes the header value (e.g., lowercasing, sorting, reducing language tags) to map multiple raw values to a single, canonical cache key. This merges logically equivalent requests into one cache entry.
  • Passthrough: Treats the exact header value as part of the cache key. Used when even subtle differences (casing, whitespace, ordering) genuinely result in different responses from the origin. Can lead to fragmentation if not carefully managed.
  • Bypass: Prevents caching altogether for responses that vary on this specific header. Useful for headers with extremely high cardinality or unpredictable values (e.g., unique user identifiers).

The article emphasizes that the origin must consistently return the appropriate `Vary` header for all cacheable responses, including errors. Cloudflare also forwards normalized headers to the origin to ensure consistency between cache matching and origin content selection, preventing scenarios where the cache and origin diverge in their interpretation of a request.

HTTP cachingCDNVary headercache invalidationdistributed cachingweb performanceCloudflareHTTP headers

Comments

Loading comments...