varnish

If you have a website, it is crucial it loads up fast so users get a good experience. There is a small piece of software known ‘Varnish Cache’ which can help your side load faster by caching its content. Varnish Cache is an open source web application accelerator also known as HTTP accelerator or caching HTTP reverse proxy. Varnish Cache can dramatically improve the site performance and depending upon your system architecture can speedup your website performance by 80% or more.

How does Varnish Cache works?

As the name hints, Varnish Cache caches the requested web content and it is served through this cache when it is requested again. This prevents unnecessary request to the web-server and hence improves the performance. Varnish Cache can work efficiently with dynamic web content like specific user related too. Further, it can also be configured in load-balancing scenarios and even acts as a fault tolerance in the event if your webserver goes down. That is a fun part which we will look upon in future articles. For now, let’s learn how to install Varnish Cache.

Varnish Cache Installation:

For RHEL/CentOS systems, its is recommended to use YUM to take care of the package installation & dependencies resolution. First we install the dependencies. Ideally, Varnish Cache must be installed on a separate server than your website’s server.

#yum install gcc binutils cpp kernel-headers glibc-headers glibc-devel -y
#yum install varnish -y

Before you start the varnish cache service, make sure your website is up. Then enable the varnish service & make it to run on each startup.

#service varnish start
#chkconfig –level 35 varnish on

Varnish Configuration Files:

Now that Varnish Cache is enabled, its time to dig into its configuration files.

/etc/init.d/varnish – This is the main startup script. Used to start, stop or reload the varnish cache service.

/etc/sysconfig/varnish(on RHEL/CentOS) – This file contains information on varnish VCL configuration file, varnish port, varnish storage file etc.

Change the following values as per your preference;

VARNISH_LISTEN_PORT= Port on which varnish will listen
VARNISH_STORAGE_SIZE= Maximum amount of memory(RAM) used by varnish
VARNISH_MIN_THREADS= Minimum number of varnish instances to start
VARNISH_MAX_THREADS= Maximum number of varnish instances to start
VARNISH_STORAGE_FILE= Location of varnish cache file

/etc/varnish/default.vcl – The varnish basic configuration file. It holds information of the back-end servers from which varnish caches & also their port. Additional settings can be made into this file when it comes to web-load balancing scenarios and other special requirements.

Testing Varnish:

Simply load your website in your browser and then issue;

#varnishlog

This will confirm whether Varnish is working as it is expected to be.

Varnish Cache Monitoring Commands:

A few varnish commands which are helpful in monitoring;

#varnishlog – Provides detailed information on requests.
#varnishstat – Provides all the info you need to spot cache misses and errors.
#varnishadm – CLI varnish administration used to reload vcl configuration and purge url’s.
#varnishhist – Provides a histogram view of cache hits & misses.
#varnishtop – It reads from the shared memory logs and presents the most commonly occurring log entries.

There is more to Varnish Cache. It is truly an amazing piece of application if you have a high traffic web-server.

Advertisements