We just shipped support for the ugliest part of HTTP: Vary
summary
The Vary HTTP header is essential for serving different content versions from one URL but often causes cache inefficiency due to excessive variation in request headers. Cloudflare has introduced Vary support in Cache Rules to allow developers to control how these headers are handled. By separating the origin's declaration of variance from the cache's handling logic, users can maintain high cache hit ratios while serving correct content. This system enables the normalization of redundant header values to prevent cache fragmentation.
key points
- The Vary header prevents caches from serving the wrong content version, such as sending JSON to a browser expecting HTML, but can lead to 'cold caches' when minor header differences create thousands of redundant entries.
- Cloudflare provides three specific actions for Vary headers: 'normalize' to group similar requests, 'passthrough' to treat exact values as distinct, and 'bypass' to avoid caching high-cardinality or personalized data.
- Normalization works by standardizing common negotiation headers like Accept-Language, stripping unnecessary parameters and sorting values to ensure that equivalent requests share the same cached response.
who should read it
Web developers and infrastructure engineers who manage content delivery networks and want to optimize cache performance for negotiated content.
discussion positive
consensus
Commenters generally agree that Cloudflare's lack of Vary header support was a long-standing and frustrating limitation for developers. There is a strong preference for explicit versioning or URL suffixes over content negotiation via the Accept header to avoid caching bugs and complexity. The sentiment is one of relief that the feature is finally arriving, mixed with skepticism toward Cloudflare's general marketing and historical implementation choices.
top insight
The risk of 'front-running' occurs when a response without a Vary header is cached and served to subsequent users who should have received a different version based on their request headers. To prevent this, developers may need to implement snippet rules to ensure every single response explicitly includes a Vary header, regardless of the origin's default behavior.
Generated by an LLM from the article and thread. Check the sources before relying on it.