Cookie-Based Caching

cookie based caching

Cookies are used to store small pieces of data from a website onto a visitor’s browser. These pieces of data help improve a visitor’s user experience by remembering what they had in their shopping cart, keeping them logged in, and much more. A feature that certain webmasters find useful is to cache assets based on a cookie value.

With the Cache Key Cookie feature you can do just that. This guide will explain how cookie-based caching works and what you need to know.

How Does Cookie-Based Caching Work?

Cookie-based caching works in conjunction with the cookies you have defined at your origin server. For example, if a custom cookie is defined on your origin server you can check this by opening up Chrome dev tools, navigating to the Network tab, selecting an asset which is associated with said cookie, and scrolling down to “Request Headers”.

cookie request header

Depending what you are trying to achieve, you can cache assets based on a particular cookie. For example, if you set cookies based on which language the visitor defines when they visit your site, then the resources required to display the site in a particular language can be cached. This is done by adding the name of your cookie variable in the Cache Key Cookie feature in a pull zone’s advanced features.

cache key cookie feature

In the example above, we’re defining the cookie variable “language” for a website that offers its content in multiple languages to visitors. Setting this variable will tell the CDN to cache the resources required to load the web page based on the value of the cookie.

Summary

With the use of this feature, a separate cache entry will be stored based on the cookie value. However, the downside to this is that it will lower your cache hit percentage (CHP) since the CDN will be required to check the origin if the cookie value differs, therefore resulting in a MISS every time this occurs.

Related Articles