From Cloudflare Blog · · 2m
Cloudflare Ships Vary Support for HTTP Caching
Cloudflare now handles HTTP Vary headers to fix cache mismatches without breaking performance.
In brief
Cloudflare now handles HTTP Vary headers to fix cache mismatches without breaking performance. Cloudflare added Vary support in Cache Rules to normalize request variations and prevent cache fragmentation. Originally reported by Cloudflare Blog.
Vary's reputation
The response header Vary has been called the ugliest part of HTTP that we haven't yet improved.
“the ugliest part of HTTP that we haven't yet improved”
Why Vary matters
One URL can have more than one correct response, but caches risk serving the wrong bytes without Vary.
Vary's problem

Vary tells caches which request fields may affect a response but doesn't specify which differences actually matter.
How Vary works
Vary identifies request headers that affect responses, like Accept or Accept-Language, to serve different formats from the same URL.
Cache mismatch example
A browser requesting HTML and an API client requesting JSON both use the same URL but need different responses without Vary.
Vary's real challenge

When requests differ in small ways, caches create thousands of variants even when responses are identical.
High-cardinality variation
User-Agent values and cookies can create 1,000+ combinations across three fields, fragmenting caches into useless variants.
Cloudflare's solution
Vary in Cache Rules lets you normalize, passthrough, or bypass request headers to control cache variants.
Normalization action
Normalize reduces equivalent requests to the same cache key by sorting language tags and stripping parameters.
Passthrough action
Passthrough preserves exact header values, including casing and whitespace, for precise response matching.
Bypass action
Bypass ignores headers with high-cardinality values like cookies or user-agent to prevent cache fragmentation.
How it works
Cloudflare uses configured actions to match requests to cached variants without contacting the origin for equivalent responses.
Real-world example
Accept-Language: en-US, fr;q=0.8 and fr;q=0.8, en-GB both normalize to English and French for shared caching.
Configuration
Set Vary in Cache Rules to normalize Accept and Accept-Language to specific languages and media types.
Why not custom keys
Custom cache keys add dimensions to every response, but Vary handles consistent request fields across responses.
Vary in action
Cloudflare now supports Vary in Cache Rules on all plans to fix cache fragmentation without breaking performance.
The main idea
Cloudflare added Vary support in Cache Rules to normalize request variations and prevent cache fragmentation.

