Varnish logo - Nucleus cloud hosting

Varnish is not only something you use to protect wood, it’s also a tool to make your website superfast and a piece of software in our advanced managed hosting solutions. So what is Varnish you might be wondering?

What is Varnish?

The Varnish we refer to calls itself a ‘Web Application Accelerator’, and when properly configured, it is just that. But it does more: it’s a load balancer, a reverse proxy and a website cache in one.

You can install Varnish on your webserver or on a separate server. It replaces the port your webserver would be active on right now (standard port :80) and will receive all HTTP requests itself. Once a request is received it will return the page from cache or forward the request to the actual webserver that is running on a different port (or different server). That answer from the webserver can then be stored in the cache so it can be used for the next visitor / request.

What can it do for me?

When you create or have a webpage you know can become popular with many visitors it is definitely interesting to involve Varnish with your developing. Take for example a standard LAMP (Linux, Apache, MySQL and PHP) environment where many websites and applications are made. Every time someone requests a page, that request arrives on the Apache Webserver, which processes your entire PHP code and uses the necessary SQL queries to request all data from the MySQL database server. The next visitor to your website goes through the same cycle: webserver -> Process PHP -> retrieve MySQL data -> provide page to the user. Even though the end result in both cases is often 100% identical, everything still gets recalculated and requested. And that asks for time of your visitor and time of your server’s CPU.

That has to be more efficient, they thought at Varnish software, in the form of page caching.

What if the end result of the page can already be stored so a visitor who requests an identical page doesn’t have to go through the whole process? The result is that your visitor gets faster response times and can see the desired page on his/her screen faster, without waiting. Varnish explains it nicely in the video below.

By retaining the pages in the memory of the server, everybody benefits: Your server doesn’t have to waste as much time retrieving and processing all data and the visitor get the results (s)he wants to see faster. This 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 in turn will benefit your SEO.

How does it work?

We have to be honest, Varnish is not simple software. Because it acts as man-in-the-middle (or proxy) between the visitor of your website and the webserver it’s actually the most crucial part of your entire software stack. If Varnish doesn’t behave as it should, your visitor’s request can get lost and not get to your webserver. Or the webserver’s answer can’t get back to your visitor.

Varnish will differentiate between the HTTP request made by the visitor and the HTTP response made by the webserver, allowing you as a systems administrator to intervene anywhere within that request. You can modify the incoming request (the ‘Receive’ routine or ‘vcl_recv’) and add and modify headers as well as the answer from the webserver (the ‘Fetch’ routine or ‘vcl_fetch’). Most standard PHP applications for example make it appear as if every visitor is unique by adding a unique PHP session cookie, while in practice most visitors are anonymous.

Varnish is set up by using a scripting language called ‘VLC’ or ‘Varnish Configuration Language’. The syntax of those configurations looks a lot like C-code and in the background it is compiled to make Varnish super-fast. What does this code look like? Well, on our Github repository there’s a nice example that you can use. By using a scripting language you get the possibility to do *everything* with a HTTP request: you can modify it from start to finish. Therein lies both the strength and complexity of Varnish. A thorough knowledge of the basic HTTP protocol is definitively a must before you can get started with Varnish.

In the spirit of Open Source, we’ve made our standard Varnish configurations publically accessible: These are production ready and have all best practices of Varnish ready.

If you want to get started, that’s a very good basis. Have any remarks or improvements? Let us know.

Edge Side Includes vs. caching the whole page

The most commonly used form of Varnish is the one that puts whole pages into the cache. But the more complex your site becomes, it usually means a more complex caching strategy. A typical example is a web page where a logged-in user sees a greeting message like ‘Hello, {username}’. That personal greeting makes that page unique for you as a visitor, but the news items that are shown are possibly still identical to those shown to an anonymous visitor.

Edge Side Includes or ESI makes it possible to cache some parts of a page and keep other parts dynamic. That way you get the best of both worlds: the content that is identical to both anonymous as logged-in users doesn’t need to be recalculated every time and at the same time you get the strength of personalized pages.

Want to know more about ESI, full-page caching, key-value stores and more? Read our pages about caching and scalability.

Wow, that’s complex?

We won’t deny it, Varnish is complex. We have years of experience and are more than familiar with it to offer you all the support you need. Are you interested in what Varnish can do for your site or server? Let us know, we’ll talk about the different caching options and help you make your website(s) super-fast and stay that way.

Want to know more about fast websites? Read our blogpost about “Memcached: who, what and where?” for more tips.

Related posts
PHP Benelux 2018

PHP Benelux 2019: 7 things that we will remember

For two days PHP Benelux 2019 was the place to be for anyone who works with PHP. A quick overview of the most important takeaways.

Read more

visies meet the hackers

Meet the hackers: three perspectives on security

Better safe than sorry was the message behind ‘Meet the hackers’, our workshop on ethical hacking. In the cosy setting of the Mariaburg barn, […]

Read more

gdpr voor developers

GDPR: practical tips for developers

There’s already a massive amount of information around concerning the GDPR, especially for CEOs, CIOs, and marketing managers, but what about GDPR for developers? […]

Read more