+3 votes
in Varnish Cache by (242k points)
Varnish Cache: higher performance for dynamic web pages

1 Answer

+4 votes
by (1.6m points)
Best answer

This is how Varnish Cache works
The advantages and disadvantages of Varnish Cache
Varnish Cache Installation


Varnish Cache: higher performance for dynamic web pages

As the complexity and number of visits to dynamic pages increases, their performance can suffer. To reduce the load on the server and counteract the loss of speed, it is possible to implement a reverse proxy that is responsible for responding to the server's requests. This is responsible for caching the requested material, which is, in general, static content such as images and results of frequent searches on dynamic websites. Varnish is a very popular caching software . Unlike many of its competitors, this free software was initially developed as a web accelerator . In order to use Varnish Cache, the server must have a Unix operating system and have sufficient root permissions for its installation. 

This is how Varnish Cache works

The server where the content of the web page is located starts Varnish directly as a reverse proxy. When a user visits the web, the request is initially processed by the original server, while the Varnish stores the request and the contents. Thus, when the server receives a similar request, the data will be loaded directly from Varnish Cache. In other words: the software caches all data and allows the operating system to choose which data it will permanently store on the server's hard drive. This prevents data from being cached and on the hard drive at the same time..

Varnish also serves as a load balancer . With the help of the round robin scheduling algorithm, incoming requests from the client will be treated as worker threads (threads) to be handled later by Varnish Cache. A fixed limit determines the number of concurrent threads that can be processed and, if reached, Varnish will put all subsequent requests on hold. When the waiting requests exceed the limit, all incoming connections will be blocked.

The configuration of Varnish as a reverse proxy is controlled primarily through the Varnish Configuration Language (VCL). This request management language allows you to write hooks (interfaces) that facilitate the integration of strange code in the application. When loading a script into VCL, it is translated into the C programming language and compiled into a program library. These VCL routines are subsequently bound to the Varnish cache. If the content manager, the eCommerce system or the web application used interprets the ESI (Edge Side Includes) markup language, Varnish offers the possibility of storing complete pages. The markup language generates so-called ESI tags in HTML files, thus distinguishing dynamic content. With this, every time the client makes a request, Varnish Cache recognizes the tags and loads the appropriate content..

The advantages and disadvantages of Varnish Cache

Optimizing your own web hosting with Varnish Cache can be very useful in the face of an increase in its complexity and the number of web visitors. However, the implementation of the software is not recommended for all types of web projects. To get a better overview, we have summarized the pros and cons of Varnish:

Advantage: Disadvantages:
? Faster loading speed thanks to content storage in memory ? For those systems that do not interpret ESI, Varnish Cache does not offer any optimization possibilities
? Load balancing on the server ? Increased complexity and susceptibility to errors
? Edge Side Includes Markup Language ? There is no support for TLS / SSL, i.e. for HTTPS
? The operating system stores content on the server's hard drive ? Complex installation and configuration processes. Requires experience
? Load balancing with the round robin method ? Only for Unix operating systems
? Flexible configuration options thanks to VCL

The above comparison shows that Varnish Cache is the appropriate complement to existing caching functions (on clients and web servers) when working with a web application that allows interpreting the ESI markup language. On the other hand, the installation and configuration of Varnish Cache, including ESI tags, is not easy. Also, because Varnish does not support TLS / SSL connections, it is necessary to use another proxy server to ensure secure transmission.

Those projects where Varnish Cache is configured correctly, as well as their ESI tags, will speed up their loading speed much more than using conventional caching methods. Consequently, this software will greatly reduce the loading time for your visitors, which in the long run will mean a significantly higher conversion rate. In the same way, you will automatically benefit from a better ranking in search engines and a reduction in the load on your web server, which will no longer be solely responsible for the processing of incoming connections. In particular, Varnish Cache is a program used mostly by operators of online stores and dynamic web pages with a great variety of content..

Varnish Cache Installation


To install Varnish Cache, it is necessary to have administrative permissions of the respective Unix system. Furthermore, it must have been previously installed on the web server that will use it. In the tutorial below you will find the necessary steps to install and configure Varnish . In this example Ubuntu is used as the operating system and Apache as the web server:

1. Varnish is included, by default, in the Ubuntu software package, but not necessarily its latest version. This is why, during its installation, Varnish allows access to its own online directory. By entering the code below you can open the directory and use it as source code:

  sudo apt-get install apt-transport-https sudo curl https://repo.varnish-cache.org/GPG-key.txt | apt-key add - sudo echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.1" >> /etc/apt/sources.list.d/varnish-cache.list  

2. Next, you should read the package lists and install Varnish

  sudo apt-get update sudo apt-get install varnish  

3. Now is the time to configure the Varnish file so that the software knows where to find the content on the web. For this it is necessary to open the following file:

  sudo nano /etc/default/varnish  

Here you will need to change the values ​​for? DAEMON_OPTS? as it's shown in the following:

  DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m"  

4. Save the changes and open the default.vlc file: 

  sudo nano /etc/varnish/default.vlc  

Specify port 8080 as the source for content covered by Varnish:

  backend default { .host = ""; .port = "8080"; }  

5. Finally, specify the Apache port as 8080 (set by default to 80). To do this, you can open the Apache port configuration file:

  sudo nano /etc/apache2/ports.conf  

Change the port number for the? NameVirtualHost? and? Listen?

  NameVirtualHost Listen  

6. Set the default file (etc / apache2 / sites-available / default) in the same way as the VirtualHost entry.

7.   Next, restart Varnish and the server to finish the installation and configuration:

  sudo service apache2 restart sudo service varnish restart  

If you need additional instructions for installing Varnish on other Unix operating systems or are looking for the program code for this software, go to the download section of its official website varnish-cache.org.