Vary Cache on Cookie Value

Enterprise or Essential?

3 min read

For the vast majority of websites, a user visits the site, requests a page and gets it back. And no matter who that user is, or where they are, they get the same copy of the page back. Some websites however will change the content depending on who is visiting. For example someone visiting from Australia might get the page in English, someone visiting from Spain will get it in Spanish.

The Vary Header

The HTTP Vary header is the standard way of telling a cache that the content of a page can change depending on a request header. For example, if a website returns the header:

Vary: Accept-Language

It is telling any cache that the content can change depending on the language preference of the user's browser. The cache will then store a separate copy of the page for each language.

This is great, but what if the content changes based on something other than a standard header? For example, a user might be able to select their currency on an ecommerce store. This preference is usually stored in a cookie.

A common, but problematic, way of handling this is to return:

Vary: Cookie

This tells the cache to store a separate version of the page for every unique Cookie header it sees. The problem is that the Cookie header can contain many different cookies (e.g., for analytics, session tracking, etc.), leading to a massive number of cached versions and a very low cache hit rate. This effectively makes caching useless.

Varying on a Specific Cookie Value

A much better solution is to vary the cache based on the value of a specific cookie. For example, an ecommerce store might use a cookie named currency to store the user's preference. By instructing the CDN to only look at the value of the currency cookie, it can store separate cached versions for AUD, USD, EUR, etc., while ignoring all other cookies.

This gives you the best of both worlds: you can serve personalised, dynamic content while still benefiting from a high cache hit rate.

Use Cases

Varying the cache on a cookie value is incredibly powerful for dynamic websites:

  1. Multi-Currency/Multi-Lingual Stores: Serve cached pages with the correct currency and language for each user.
  2. User Groups: Show different content or pricing to different user groups, like wholesale vs. retail customers, without them needing to log in.
  3. A/B Testing: Serve different versions of a page to different users as part of an A/B test and cache both versions.
  4. Personalisation: Cache pages with personalised content, like recently viewed items or location-based offers.

Enterprise or Essential?

Many major CDN providers restrict this feature to their expensive enterprise plans, putting it out of reach for many businesses. At Peakhour, we believe this is an essential feature for any modern, dynamic website. That's why we make it available on all our plans. It's a core part of our 'Enterprise for Everyone' philosophy.

The ability to vary the cache on a specific cookie value is a game-changer for dynamic content delivery. It solves the limitations of the Vary: Cookie header and allows for efficient caching of personalised content. It's not a luxury feature; it's an essential tool for improving performance and user experience on modern websites.

Enterprise-Grade Security and Performance

Peakhour offers enterprise-grade security to shield your applications from DDoS attacks, bots, and online fraud, while our global CDN ensures optimal performance.

Contact Us

Related Content

Enterprise-Level Caching for All

Enterprise-Level Caching for All

Elevate your e-commerce with our newly released Magento 2 plugin. Experience enterprise-level caching features accessible to all Peakhour customers.

Cache Tags/Surrogate Keys

Cache Tags/Surrogate Keys

Surrogate Keys, or cache tags, are a powerful mechanism for targeted flushing of content from a cache, not all CDNs support them though.

© PEAKHOUR.IO PTY LTD 2025   ABN 76 619 930 826    All rights reserved.