IntroductionIntelligent content caching is one of the most effective ways to improve the experience for your site’s visitors. Caching, or temporarily storing content from previous requests, is part of the core content delivery strategy implemented within the HTTP protocol. Components throughout the delivery path can all cache items to speed up subsequent requests, subject to the caching policies declared for the content. Show In this guide, we will discuss some of the basic concepts of web content caching. This will mainly cover how to select caching policies to ensure that caches throughout the internet can correctly process your content. We will talk about the benefits that caching affords, the side effects to be aware of, and the different strategies to employ to provide the best mixture of performance and flexibility. What Is Caching?Caching is the term for storing reusable responses in order to make subsequent requests faster. There are many different types of caching available, each of which has its own characteristics. Application caches and memory caches are both popular for their ability to speed up certain responses. Web caching, the focus of this guide, is a different type of cache. Web caching is a core design feature of the HTTP protocol meant to minimize network traffic while improving the perceived responsiveness of the system as a whole. Caches are found at every level of a content’s journey from the original server to the browser. Web caching works by caching the HTTP responses for requests according to certain rules. Subsequent requests for cached content can then be fulfilled from a cache closer to the user instead of sending the request all the way back to the web server. BenefitsEffective caching aids both content consumers and content providers. Some of the benefits that caching brings to content delivery are:
TerminologyWhen dealing with caching, there are a few terms that you are likely to come across that might be unfamiliar. Some of the more common ones are below:
There are plenty of other caching terms, but the ones above should help you get started. What Can be Cached?Certain content lends itself more readily to caching than others. Some very cache-friendly content for most sites are:
These tend to change infrequently, so they can benefit from being cached for longer periods of time. Some items that you have to be careful in caching are:
Some items that should almost never be cached are:
In addition to the above general rules, it’s possible to specify policies that allow you to cache different types of content appropriately. For instance, if authenticated users all see the same view of your site, it may be possible to cache that view anywhere. If authenticated users see a user-sensitive view of the site that will be valid for some time, you may tell the user’s browser to cache, but tell any intermediary caches not to store the view. Locations Where Web Content Is CachedContent can be cached at many different points throughout the delivery chain:
Each of these locations can and often do cache items according to their own caching policies and the policies set at the content origin. Caching HeadersCaching policy is dependent upon two different factors. The caching entity itself gets to decide whether or not to cache acceptable content. It can decide to cache less than it is allowed to cache, but never more. The majority of caching behavior is determined by the caching policy, which is set by the content owner. These policies are mainly articulated through the use of specific HTTP headers. Through various iterations of the HTTP protocol, a few different cache-focused headers have arisen with varying levels of sophistication. The ones you probably still need to pay attention to are below:
An Aside about the Vary HeaderThe In the case of Items like How Cache-Control Flags Impact CachingAbove, we mentioned how the Some
of the
These can be combined in different ways to achieve various caching behavior. Some mutually exclusive values are:
The Developing a Caching StrategyIn a perfect world, everything could be cached aggressively and your servers would only be contacted to validate content occasionally. This doesn’t often happen in practice though, so you should try to set some sane caching policies that aim to balance between implementing long-term caching and responding to the demands of a changing site. Common IssuesThere are many situations where caching cannot or should not be implemented due to how the content is produced (dynamically generated per user) or the nature of the content (sensitive banking information, for example). Another problem that many administrators face when setting up caching is the situation where older versions of your content are out in the wild, not yet stale, even though new versions have been published. These are both frequently encountered issues that can have serious impacts on cache performance and the accuracy of content you are serving. However, we can mitigate these issues by developing caching policies that anticipate these problems. General RecommendationsWhile your situation will dictate the caching strategy you use, the following recommendations can help guide you towards some reasonable decisions. There are certain steps that you can take to increase your cache hit ratio before worrying about the specific headers you use. Some ideas are:
In terms of selecting the correct headers for different items, the following can serve as a general reference:
The key is to strike a balance that favors aggressive caching where possible while leaving opportunities to invalidate entries in the future when changes are made. Your site will likely have a combination of:
The goal is to move content into the first categories when possible while maintaining an acceptable level of accuracy. ConclusionTaking the time to ensure that your site has proper caching policies in place can have a significant impact on your site. Caching allows you to cut down on the bandwidth costs associated with serving the same content repeatedly. Your server will also be able to handle a greater amount of traffic with the same hardware. Perhaps most importantly, clients will have a faster experience on your site, which may lead them to return more frequently. While effective web caching is not a silver bullet, setting up appropriate caching policies can give you measurable gains with minimal work. |