Varnish – the web accelerator for content-heavy sites
If a website could be seen as a chemical reaction of code, style sheet and content, then web accelerator tools like Varnish, Memcached and Node.JS could be treated as catalyzers that make the ingredients react faster and deliver better results.
In a series of articles we’ll focus on the main capabilities of those tools and will include an example of how you could use them to leverage on the huge catalyzing effect they could have on your sites’ performance. Let’s start with Varnish.
Varnish Cache, also known as a caching HTTP reverse proxy, is a web accelerator that can considerably speed up a website by reducing the load on the server.
If servers now work faster, why is Varnish needed?
In theory, a server should return a response immediately without needing to do any real work in the background. In practice, however, the server may need to do a great deal of work before returning a response to the client. To execute a simple request, the server may need to start a new process, to load script files from the disk, to perform database queries, as well as many other actions that could exhaust the system resources.
And in the case of a poorly scripted site, the server response burden will get even bigger. This is just how web servers are made to work. No better solution has been invented so far.
Here is where web accelerators like Varnish come into play – they streamline the response operation. Their main mission is to cache the not-so-often-updated pages of content-heavy websites and to make them load faster. Depending on the site’s architecture, it can be sped up 300 to 1000 times its current speed.
How exactly does Varnish work?
Let’s see in more detail how Varnish works to streamline the server response.
When a request is sent to the server for the first time, it is first checked by Varnish and then forwarded to the server. The server then returns a response to Varnish, which in turn gives the response to the client that sent the original request. In the process, Varnish stores the response from the backend in its cache for future use.
What’s more important, the response is stored in memory for faster access, instead of being cached to disk. This helps Varnish to quickly serve the next response directly from its cache and to eliminate the need for any action from the backend server.
As a result, the load will be reduced significantly, the response times will be improved, and the server will be able to handle more requests per second. All those optimizations allow Varnish to process server requests at an incredibly fast speed, which will give a direct boost to the performance of your sites and applications.
How to work with Varnish?
Working with Varnish requires a proper installation and a good control of its settings. We offer Varnish with all our web hosting plans, so our system will take care of the installation and memory allocation procedures.
Here is an example of how to create a basic Varnish instance on our servers.
Let’s say you want to use Varnish to cache your WordPress blog.
In the Varnish section of the Web Hosting Control Panel, click on the Create an Instance button on the top right.
In the popup window, select the memory allocation that you will need for this instance. By default, Varnish will scan your website traffic using a custom algorithm and, starting with the most visited pages, will cache your website content until the memory quota is reached. So, even if you exceed your memory allocation limit, your most sought-after pages will still be served:
Next, you need to select the dedicated IP address that we’ll use to set up the instance on. You could get one from the Service Upgrades section of the Control Panel. If you do not want to use a dedicated IP address, we will assign a specific port to your host. In this case, however, you will need to make sure that all static links to your blog are changed to reflect the port allocation:
NOTE: If you decide to use a dedicated IP address for the Varnish instance, you will need to point your WordPress site to that IP address from the Hosted Domains section of the Control Panel.
If you are well familiar with Varnish, you could make a manual configuration on your own by selecting the ‘Manual configuration’ option. Otherwise, we’ll do that for you automatically.
In general, cookies enabled for a site interfere with the proper caching of the content. This is why, if you have enabled cookies for your site, you will need to have them neutralized for the purpose of caching. You can do that by marking the ‘Remove cookies’ box, which will configure Varnish to ignore the cookies on your site automatically.
The TTL box refers to the time within which the cached content will be left in the memory. The default value is set to 60 seconds.
Finally, specify the URL(s) of your site that you do not want to be cached by Varnish. In our case, these should be the /wp-admin and /wp-login.php URLs of your WordPress site, which are input in the ‘Exclude URL’ field as follows:
All other packages offer it as an upgrade option. Also, customers can always upgrade the current Varnish memory quota pertaining to their plan from the Service Upgrades section of the Control Panel.