Caching can greatly improve the experience of your website visitors and many VPS server users that host websites implement various caching strategies for this. Generally, this term is used for the process of temporarily storing content fetched by previous requests in the web server so that subsequent requests are fulfilled faster, meaning that content on the website loads faster. Web caching, on the other hand, minimizes network traffic by caching HTTP responses for requests by following certain rules. The subsequent requests are fulfilled by the cache closest to the visitor instead of the main web server. It’s beneficial for both content providers and the consumers as it improves responsiveness, increases the performance of the same hardware, decreases network costs, and makes content available during network interruptions whenever your VPS server is having issues. But there’s more to know about web caching, so let’s dive a little deeper.
What Can Be Cached?
Some content is cached more easily than others. For most websites, the easiest content to cache is:
- Media files;
- Non-rotating images;
- Logos and brand names;
- Downloadable content;
- Style sheets;
They are not modified as frequently as other items, and they can be cached for a longer period. You should be extra careful with caching:
- Rotating images;
- HTML pages;
- Content requested with authentication cookies;
Of course, there is content that should never be cached:
- Content that is user-specific and frequently requested;
- Assets related to sensitive data, such as banking information and other security sensitive topics.
If you want, you can specify your policy to cache different types of content appropriately.
If you’re fairly new to caching and haven’t implemented it in your VPS server yet, there are some useful terms to know from the get-go. You’re very likely to come across these more common ones:
- Origin server. This is the original location of the content you’re putting on the internet. It’s responsible for setting the caching policy for all content and serving content that for some reason couldn’t be retrieved from a cache along the request route.
- Cache hit ratio. This shows the ratio of requests that can be retrieved from a cache to the total requests made. The higher the ratio, the more content can be retrieved from the cache.
- Freshness. This shows whether an item in the cache is a viable candidate that can be served to the client. Only content that falls into the specified freshness time frame can be sent to the visitor’s browser.
- Stale content. These are items that are “expired” according to the freshness policy, and they will not be used to respond to client requests. The origin VPS server has to be re-contacted to either update said content or so verify that it’s still accurate.
- Validation. As mentioned previously, stale content’s expiration time can be refreshed by validation. You have to check in with the origin server to see if the items are up to date.
- Invalidation. This process removes content from the cache before its expiration date. This is crucial for items that are changed because keeping outdated content can cause issues for visitors.
There are many more terms, but these should be enough for beginners.
Your set caching policy determines most of the caching behaviour. These policies are set via specific HTTP headers. As with terms, there are many different cache-headers out there, but users should pay close attention to these:
- Expires. This sets the time of expiration for content. When it does expire, all requests for those items will have to go to the origin VPS server. Users are advised to only use it as a fallback.
- Cache-Control. This helps you control and dictate your content’s caching policy. You can do so by typing in instructions using this header.
- Etag. This is used for cache validation. The original server can provide each item with a unique Etag that can later be used to validate content that is about to expire by sending back the Etag to the origin. It verifies whether the content is up-to-date or sends the updated version with a new Etag.
- Last-Modified. This shows the last time an item was modified.
- Content-Length. This header is useful as it specifies the size of the content as certain software refuses to cache it if it’s doesn’t know the size beforehand.
- Vary. With this, you can store different versions of the same content, but by doing so, you dilute the entries in the cache.
As you work more with caching on your VPS server, you will learn more useful headers that will help you set policies that best suit your needs.
Your specific requirements and situation will ultimately determine what kind of caching strategies you will use. But when you’re just starting and everything’s still pretty fresh, it’s best to follow some widely used steps to ease into caching. You can:
Establish specific directories for images, CSS, and shared content. This way, you can easily refer to specific items from any page on your website.
Use the same URL to refer to the same items. You should refer to your content the same way on every page because caches use both the path to the requested content and the host.
Use CSS image sprites where possible. This type of sprites lowers the number of requests needed to display your website.
Fingerprint cache items. It’s quite useful to fingerprint static content because by doing so, when you modify the resource, the new resource name for that specific item can be requested, and the requests will correctly bypass the cache.
While caching might seem tricky and confusing at first, once you get the hang of it, it’s fairly simple. Implementing this process greatly improves the experience not only for your website’s visitors but for your system as well. Both parties benefit from it, and all it takes is just a bit of work. It should be noted, though, that while good caching policies will significantly affect the performance, it’s not the only means to better your server and website, and users should look into other beneficial methods, too