What is Varnish cache and why should you use it?

What is Varnish cache and why should you use it?

Varnish is the tool to make your website super fast and a standardized piece of software in most managed webhosts. Want more details? Read on..

What isVarnish?

The Varnish we mean calls itself a 'Web Application Accelerator', and if properly configured it is indeed. But it also does much more: it's a load balancer, a reverse proxy and a website cache in one…

Varnish can be installed on your webserver or a separate server. It replaces the port on which you would now have your webserver active (default port :80) and will receive all HTTP requests itself from now on. Once a request is received, it will either return the page from a cache or forward the request to the real web server running on another port (or other server). That response from the web server can then be stored in the cache so that it can be used at the next visit.

What's in it for me?

If you have or are creating a web page that you know may become popular with many visitors, it is certainly interesting to involve Varnish in your development. For instance, a standard LAMP (Linux, Apache, MySQL and PHP) environment is used to create a lot of websites and applications. Every time someone requests a page it arrives on the Apache web server, which processes your entire PHP code and requests all the data from the MySQL database server with the necessary SQL queries.

The next visitor who visits your website does the whole cycle again: web server -> process PHP -> retrieve MySQL data -> deliver page to the user. Even though the end result is often 100% identical in both cases, everything is recalculated and retrieved. And that takes time from your visitor and time for your server's CPU.

So they thought Varnish Software should be able to do this more efficiently, in the form of page caching. What if the end result of the page can already be tracked so that a visitor requesting the identical page does not have to go through the entire process? The result is that your site visitor gets a response faster and can see the desired page on his or her screen faster, without waiting times.

By keeping the pages in the memory of the server, everyone benefits: your server needs to waste less time retrieving and processing all data and the visitor gets the results it wants faster. This even makes Varnish a perfect part of your environment, even if you don't expect high visitor numbers but just want a fast website. And that certainly benefits your SEO efforts.

How does it work?

We have to be honest, Varnish is not a simple software. Because it actually acts as a man-in-the-middle (or proxy) between the visitor of your website and the web server, it is in fact the most crucial part of your entire software stack.

If Varnish doesn't behave as it should, your visitor's request may be lost and may not reach your web server. Or the web server's response does not get back to your visitor.

Varnish splits the visitor's entire HTTP request and the web server's response into several blocks, where you as a system administrator or developer can pick up. You can modify the incoming request (the 'Receive' routine or 'vcl_recv') and add or modify headers just like the web server response (the 'Fetch' routine or 'vcl_fetch'). For example, most standard PHP applications make it seem as if every visitor is unique by giving a unique PHP session cookie, while in practice most visitors are anonymous.

Varnish is set by using a scripting language called 'VCL' or 'Varnish Configuration Language'. The syntax of these configurations is very similar to C code and in the background it is also compiled to make Varnish super fast. By working with a scripting language you get the possibility to do everything with an HTTP request: you can modify it from beginning to end. This is the strength and complexity of Varnish. A thorough knowledge of the basic HTTP protocol is a must before you can start working with Varnish.

Edge Side Includes vs. whole page caching

The most common form of Varnish is the one where whole pages are cached. But the more complex your site becomes, the more complex usually the caching strategy. A typical example is a web page where you as a logged in user have a personal greeting at the top, like 'Hello, {username}'. That personal greeting actually makes that page unique for you as a visitor, but the news items that are shown may be identical to those of an anonymous visitor.

Edge Side Includes or ESI makes it possible to cache some parts of a page and keep others dynamic. This way you get the best of both worlds: content that is the same for both anonymous and logged in users doesn't have to be calculated every time and at the same time you get the power of personalized pages.

That's complex!

We don't deny it, Varnish is complex. We have several years of experience with it. Are you interested in what Varnish can do for your site or server? Try it out!