October 19, 2020
6 min read time

5 Don'ts When Caching with Varnish

Varnish_caching_don'ts

Over time, the most popular content we’ve created relates to hands-on Varnish use and relevant dos and don’ts. Naturally, we continue to create more content about this because our raison d’être is to help Varnish users get the maximum performance and efficiency they can from their Varnish setups.

Let’s get down to business.

blog banner 6 (1)

Varnish don’ts: Don’t do these things. Really, just don’t

1. Don’t set your time to live (TTL) to zero

It seems counterintuitive. If you don’t want to cache your content, doesn’t it seem like it makes sense to give the content a null TTL? 

Do not set your TTL to zero: This is really important. Why? 

Our own Guillaume Quintard has explained this in detail in a recent blog post, and it’s the very first topic covered in the webinar, so we won’t spend too much time on it here. 

Just keep in mind that if you set a 0 TTL, you’re instructing Varnish to fetch content from the backend and immediately discard it, which could very easily hamper performance significantly. If one of our primary  goals is to serve content rapidly and to avoid overwhelming backends with stampedes of traffic, a null TTL will exacerbate the problem. 

One method we use to protect the origin and serve content faster is a feature called request coalescing, which bundles potentially thousands of concurrent requests for the same piece of content, collapsing them into a single request, which is then cached. This cannot happen if the TTL is set to zero. Instead all the requests will be handled serially, and we never want this. 

Essentially, instead, we want to cache the decision not to cache, creating a hit-for-miss

Both the blog post listed above and the webinar explain in much greater detail the dangers of the null TTL and what you should do to avoid it.

2. Don’t skip the built-in VCL

Varnish Configuration Language offers tremendous flexibility and the possibility to customize your logic. With that said, however, it is never a good idea to ignore the built-in, ready-made VCL that Varnish comes with. It’s not only a kind of safety net, it’s also created by experts whose only job is to work with Varnish and who understand the ins and outs of Varnish behavior. Built-in VCL sets baseline default behaviors that most setups don’t need to diverge from. 

The temptation, of course, can be to do it all yourself because Varnish lets you, because you have the skills to do it, or because you aren’t aware of what the built-in VCL does. Know from the get-go: built-in VCL is perfectly adequate for conventional behavior, and custom VCL should be reserved for custom logic situations

The webinar dives into the detail behind a number of specific built-in VCL examples and what can go wrong if you change their structure. For example, vcl_hash and vcl_hit, among others; watch to find out more.

The bottom line: Extend - don’t amend - the built-in VCL. It’s there for a reason; use it.

3. Don’t vary on the user-agent

User-agent is an HTTP request header used by browsers to retrieve content from the internet to identify itself, and this will have potentially thousands of variations, depending on operating system, browser and browser version, user region, the device being used, etc. A user-agent string then could contain a lot of different information based on what each browser does. 

An incorrect assumption: If you store something in cache, you assume that every user will see the same thing. This is not always the case, and this is where variations come into play. By issuing a Vary response header, which instructs the HTTP cache what parts of a response should and should not be cached, you can cache the home page for the local host, but at the same time state that you need a variation, for example for the language the user will see based on the language header.

You definitely want to sanitize your user-agent string to reduce variations (and keep your cache hit rates high). Out of the box, the DeviceAtlas VMOD, which is available in Varnish Enterprise, will do this for you.

4. Don’t use Varnish’s built-in file storage

A number of years ago Varnish started testing what could be done with a file system. And we found big limitations: in the long run, a file system kills performance, and isn’t persistent, so you lose everything if you have to restart your cache. 

File storage, in a nutshell, then, offers limited performance, is prone to fragmentation and does not persist across restarts. 

We’re all about performance, right? So we launched the Massive Storage Engine (MSE), which is a feature available in Varnish Enterprise.

With MSE you get the benefits of both disk and RAM memory and you have a persistent cache. MSE uses not only disk caching but also comes with a RAM layer whose speed you can leverage. You can store all the content you’re able to store on disk so it can persist on cache restarts, server power outages, unexpected server failures/reboots, maintenance, etc. 

5. Don’t reinvent the wheel – use VMODs

In much the same way as you shouldn’t ignore or mess with built-in VCL because some of the basics are already in place for you, you also should take a look at what Varnish modules are available to you before you go reinventing the wheel. 

When we say “reinvent the wheel” here, we’re saying… check to see what’s already been created in terms of packaged VMODs. Varnish modules, VMODs, are written in C and expose an API that is usable in VCL. Some use cases require a lot of VCL to perform, and it’s not a great idea to try to do some DIY VCL by using inline-C. It’s next to impossible to test and is very difficult to maintain. It makes a lot more sense to look at the available packaged VMODs created by Varnish Software to expand Varnish capabilities and functionality, and address specific use cases and industry needs. 

Which VMODs get created is often influenced by a customer-specific use case but end up meeting a widespread need for which the VMOD can be recycled and used either within the Varnish Enterprise product or, quite often, in the open source community. There’s a large and growing library of official, packaged VMODs, which get some attention in the webinar. 

Definitely do not try to do things from scratch if there’s already a VMOD there to meet your needs.

To go into a lot more detail about each of these don’ts — and learn about five key Varnish dos — watch the webinar in full or get in touch with your questions. Another blog post will follow soon covering the five dos.

WATCH THE WEBINAR