Varnish is a web accelerator, a technical tool that can make your website quite a bit faster. That is of course what everyone wants, but Varnish is a complex tool. That means there is quite a bit of knowledge and effort required from both your developers and system administrator.
Because Varnish is so complex, we will try to explain in a couple of videos what Varnish is, how it works, what you can do with it, where the pitfalls are, and how you get started. In this first edition of “Chalk Talk” we explain what Varnish is.
The transcript of our Chalk Talk about Varnish
Welcome to Chalk Talk, a series of videos where we try to explain complicated technologies with just one chalkboard. In this video we are looking at Varnish.
What is Varnish?
Varnish is a web accelerator that you can introduce to your software stack to significantly improve the speed of your website. But first let’s look at what makes a website slow in the first place.
What slows down websites?
Traditionally, if you have a user behind his laptop visiting your site, he is going to connect to a webserver – an Apache or an Nginx – that in turn will trigger a PHP process that is going to hit your code. Your code will talk to a database like MySQL or PostgresSQL. It might talk to a cache like Redis or Memcached, or it might talk to external API’s that you don’t really have under control, like Facebook or Twitter API or things like it.
At the end of the day when all data is gathered, your PHP will generate a response to be handed over to your Apache webserver. The response is basically the HTML code that your client is going to see. And it delivers that to your client. But he is not very happy!
And the next person that comes to your webserver will trigger the exact same response, hitting Apache, followed by a PHP process, talking to the database or the API… You get the picture, it may take a while.
And at the end of the day, the response being given to that user is probably the same as the previous one. So why don’t we try to be smarter about that?
Is Varnish a smart solution?
If you start getting more and more visitors, traditionally you would use a bigger server. Or add another one. And another one. That doesn’t seem that efficient! So, how can we solve this with more finesse and more intelligence? We could introduce an additional layer on top of your Apache webserver, called Varnish.
Varnish is a simple tool that is going to keep previously sent responses in memory, to be served faster to the next visitor. So all this complicated logic will only be triggered once. The HTML response will be sent to Varnish. Varnish will send it to the visitor and the next visitor will just talk to Varnish, asking “give me your homepage”, and it can deliver that in milliseconds, without talking to PHP, MySQL, your API, you name it.
So this makes Varnish a web accelerator. Technically it is a proxy, sitting between the client and your webserver, but it accelerates your visitors.
Varnish as load balancer
Varnish can also act as load balancer. If the amount of visitors exceeds the capacity of a single server, you can add multiple web servers to serve all your visitors. Vanish can act as the middleman determining where that request should be sent. If your first webserver is unavailable, being in the maintenance or offline, Varnish will take that one out of commission and send requests to other available servers.
It also allows you to do rolling upgrades, where you gradually deploy your code to the next server without interrupting a visitor.
By introducing Varnish, you can create a smarter solution for scalability. Instead of throwing in more hardware, you introduce intelligence that eliminates CPU, memory and disk access. It makes everything a lot faster. If you introduce Varnish to your stack, you will have less PHP, less MySQL, less database. Basically, less of everything, and happier clients. The end result is a site that is a lot faster. Think milliseconds instead of seconds.
Better SEO results thanks to Varnish
On top of that, you have the benefit that sites like Google and Bing take the speed of your website as one of the indicators to determine the ranking of your site.
So the faster your site, the higher you can rank. You can’t do that with just PHP. You need to be smart and introduce a caching layer on top of that.
Next time on Chalk Talk?
In the next videos we’ll look at Varnish in depth. We will get into how you can use Varnish as a developer or sysadmin, develop a content strategy for your website, and how to make integrating Varnish into your project a success.
See you next time for a new episode of Chalk Talk!