​Varnish Cache Explained


If you’re a Varnish Beginner who wants to learn more about this ridiculously fast caching tool, then this post’s for you. We will talk about what Varnish is, how it works, and who it’s a good fit for.

This post has been updated on: Sunday, July 23rd, 2017

Varnish has become synonymous with high-performance HTTP acceleration. Varnish Cache is an open source reverse HTTP proxy accelerator that’s great at reducing web server load on your server. Varnish is placed in front of the web server and acts as a proxy that establishes a bridge between the browser and the server.

Poul-Henning Kamp (the creator of Varnish)

The Varnish project was the brainchild of Danish technology architect  Poul-Henning Kamp. A well-known FreeBSD consultant, Kamp was the lead developer of Varnish.

The Varnish project was started by the online branch of the Norwegian tabloid newspaper Verdens Gang. But the software was so successful that it was eventually spun off into a separate company.

But don’t worry, it’s still free and completely open source software available under a two-clause BSD license. But if you want commercial support, it’s available from Varnish Software. 

Currently, the support, management, and development of Varnish are taken care of under the banner of Varnish Software

How does Varnish Work?

Consider the following scenario. You walk into your local McDonald’s and order a BigMac burger. The guy at the counter first checks whether there’s a BigMac ready to serve.

If there’s one in stock, he’ll immediately serve you the ready-made burger. If it’s not in stock, he’d place the request to the chef.

Varnish does the job of the guy at the McDonalds counter.

Varnish sits in front of your web server and the first time a visitor requests for a web page, Varnish gets the request and passes it to the backend server. The backend server processes the request and sends the response back to the browser.

Varnish, meanwhile ‘caches’ the rendered content and stores it in super fast RAM memory.

Any subsequent request for the same web page is served directly from the cached version (in RAM Memory!) without bothering the web server or Database.

Who Uses Varnish?

Around 10% of the busiest sites on the web, including Wikipedia, The Guardian, The New York Times and Facebook use Varnish Caching.

Varnish offers ridiculous performance benefits for content-heavy, high traffic websites. Varnish can be used to cache both static and dynamic content.

There is a tragic misconception that Varnish is only for static content, but with a little bit of .VCL configuration tweaking it can work brilliantly for even dynamic websites and mobile applications. 

Why is Varnish so fast?

Varnish is synonymous with high-performance because of the way it deals with cached content. If you want to be able to serve thousands of concurrent requests per second without overloading your web server, there’s no better HTTP accelerator than Varnish.

Within the varnish configurations, you can set whether you want Varnish to store the cache on the hard disk or RAM memory.

Did you know that RAM is 1,000% faster than even SSD storage?

To get the instant “click and it’s there” feeling, you’ll want to have that cache stored exclusively on your RAM. By hosting your “cached” content on RAM, you can drastically reduce your disk i/o operations.

No database calls are made, and there is absolutely no processing involved. You will see a much lower response time and a huge decrease in server resource usage with a properly configured Varnish setup.

Is Varnish Hard to Configure?

Installing Varnish is extremely easy. Most popular Linux distributions like CentOS, Ubuntu, and Debian provide Varnish package that you can install with a single command.

Varnish comes with a base default configuration that can be used out of the box to get HTTP acceleration running with minimum fuss. To use Varnish in its default configuration, you simply need to install the package and start the daemon to listen to all HTTP requests.

However, if you are looking to customize Varnish, it can be a little tricky.

Varnish provides you with a  Varnish Configuration Language (VCL) file – where you play with the configuration settings to fine tune its performance. The VCL file tells Varnish exactly how to handle the caching process. 

When and what to cache, when not to cache, and how long to keep the cached content in memory. Mastering

Mastering yours .VCL skills can give you super powers allowing you to modify nearly every aspect of an HTTP request and response.

When you should use Varnish

  1. If you’ve got a content-heavy website with frequent traffic spikes
  2. Varnish is great for servers with heavy database load. By caching the most common web requests, your MySQL server can get some rest.
  3. If you want to reduce your disk’s activity prolonging your disk’s life expectancy.
  4. If you want the peace of mind that you can serve thousands of concurrent visitors without crashing your web server and database. 

When you should’t use Varnish?

There are plenty of easier ways to make great performance boosts like NGiNX micro caching that you can try out before resorting to Varnish.

Varnish isn’t for everyone in the same way that a V12 engine is not required for daily drives.

Roy Enfield - Support Engineer at EuroVPS

For small websites with under 1,000 page views per day, you’ll probably be just fine with an optimised LAMP StackLiteSpeed, or NGiNX with PHP-FPM.

Because of the complexity of customising the .VCL file to your specific requirements, sometimes it’s just not worth the effort to use Varnish. Getting Varnish to play well dynamic content caching is tricky. Sure, you can use ESI ‘hole punching,’ to serve user-specific content, but is it really necessary?

Check the Low Lying Fruit First

Before recommending Varnish to our customers, we always check for the low lying fruit first.

  • Are your images properly optimised?
  • Is your CSS and jS minified?
  • Is your MySQL properly tuned?

 Varnish is very powerful, but it is not a panacea.

10 Reasons Your Site is Slow. So what are the factors that cause a website to slow down? Let's figure out some of the most common high impact root causes that might affect the performance of your website.
Read More

Conclusion

Setting up Varnish to serve content properly and following your organisation’s requirements can be tricky. Cache expiry rules, time to live, URL caching, and object caching requirements in Varnish are highly complex. As a result, many consultancy firms have been borne out of the requirement to have “Varnish Implementations” done in their environment.

At EuroVPS, support for Varnish implementation is included with all managed VPS and dedicated servers. Our support engineers have extensive experience building, maintaining, and configuring Varnish to do exactly what you want.