On most websites, a user requests a page and receives the same response as everyone else. Some websites, however, change the content depending on who is visiting. For example, someone visiting from Australia might get the page in English, while someone visiting from Spain gets it in Spanish.
The Vary Header
The HTTP Vary header is the standard way to tell a cache that the content of a page can change depending on a request
header. For example, if a website returns this header:
Vary: Accept-Language
It tells any cache that the content can change depending on the language preference of the user's browser. The cache then stores a separate copy of the page for each language.
That works for standard request headers, but content often changes based on something else. 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.), creating many cached
versions and a very low cache hit rate. This effectively makes caching useless.
Varying on a Specific Cookie Value
A 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 look only at the value of the currency
cookie, it can store separate cached versions for AUD, USD, EUR, etc., while ignoring all other cookies.
This means 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 useful for dynamic websites:
- Multi-Currency/Multi-Lingual Stores: Serve cached pages with the correct currency and language for each user.
- User Groups: Show different content or pricing to different user groups, like wholesale vs. retail customers, without them needing to log in.
- A/B Testing: Serve different versions of a page to different users as part of an A/B test and cache both versions.
- 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 see it as an essential feature for modern dynamic websites. 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 addresses the limitations of the Vary: Cookie header and allows
for efficient caching of personalised content. It is not a luxury feature; it is an essential tool for improving performance
and user experience on modern websites.