Cloudflare Releases Vary Header Support in Cache Rules to Resolve Cache Fragmentation
Key point
Cloudflare has released Vary header support in Cache Rules, addressing fragmentation issues through header normalization and cache bypass settings.
Details
Cloudflare has released Vary header support in Cache Rules for all plans (Free, Pro, Business, Enterprise). Previously, it was difficult to finely control cache behavior when the Origin returned a Vary header, but now users can choose between normalizing header values, passing them through exactly, or bypassing the cache to determine how cache keys are generated.
Limitations of Vary Headers and Cache Fragmentation
The Vary header informs the cache of request fields that affect the response, but it cannot distinguish which differences actually matter. While the Origin maps thousands of header values to a few actual representations, the cache distinguishes raw value differences such as whitespace, order, and quality values, generating unnecessary cache entries. This causes Cache Fragmentation, and particularly when high-cardinality headers like User-Agent or Cookie are included, cache hit rates drop sharply and Origin load increases.
New Processing Logic and Actions
The new Cache Rules apply the following Actions to headers specified by the Origin in Vary:
- Normalize (Recommended default): Lowercases values for Accept, Accept-Language, Accept-Encoding, etc., and sorts them by quality value to standardize cache keys. Client request order does not affect the cache key.
- Passthrough: Preserves differences in case, whitespace, order, and duplicate values. This should only be used when the Origin requires exact value differences, as it risks generating unnecessary variants.
- Bypass: Bypasses the cache for headers containing personal information or unlimited values.
Responses containing Vary: * always bypass the cache. On the first request, the Cache Rule normalizes fields before the Origin request to maintain consistency between the cache key and the Origin response. On subsequent requests, it directly looks up variants based on the stored Vary fields to determine cache hits.
Configuration Guide and Precautions
Vary settings can be configured via the Cloudflare Dashboard (Caching > Cache Rules) or the Rulesets API. The criteria for choosing between Custom Cache Key and Vary are as follows:
- Use Custom Cache Key if request attributes always define the resource.
- Use Vary if the Origin declares the same set of request fields across all cacheable responses.
When Vary settings are changed, existing content is not automatically purged, so existing entries remain until they expire or are manually purged after cache keys change due to the new policy. Additionally, PUT requests to the http_request_cache_settings entrypoint replace all rules for that entrypoint, so caution is required. Cloudflare is currently evaluating ideas from the Availability Hints draft, which would allow Origins to directly describe representations in the future.
This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.
Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.