2022 Update: Tired of Magento? So are we! There are so many better options now.

At Distl, we no longer recommend Magento to our clients. In our opinion there are many better, simpler options. Shopify is the worldwide favourite, and our recommendation as of 2022. However if you currently use WordPress and would like to stay with a CMS you know, why not look into an eCommerce plugin like WooCommerce.

 

What is Varnish?

Varnish is a reverse caching proxy. Essentially, what it does is does is reduce server load and improve website speed by providing a cached version of pages from your site. It’s usually best utilised for sites that experience heavy traffic or that are inherently cumbersome. If none of this is making sense, sorry it’s about to get a lot worse. There’s a major “nerd alert” on this article, so be warned 🙂

Benefits of caching

Caching is a very common practice in computing that involves storing files to be used again. Storing these files in memory or on a user’s disk drive can improve speed by making them readily available, rather than needing to be requested or downloaded again. When we browse the web things like images, stylesheets and javascript get stored in our browser’s cache so we don’t have to keep downloading them. This makes the web page load faster, reduces bandwidth usage and also server load.

Varnish can take caching to another level on your website though. When a user visits your site, a series of requests are sent to the server in order to make the page load. The server must then find the files associated with those requests, execute any server-side code related to the requests and return to the complete web page to the user. If we install Varnish on our web hosting machine, it will sit in between the user’s requests and our web server and listen out for duplicate requests that have been received before.

On the first visit to a page, our web server will do everything it needs to (as per usual) but Varnish will make a copy of what is returned to the user. So the next time anybody visits that same page, Varnish will recognise that these requests have been made before and it can quickly respond with a cached version of the result. This simple process can have a huge impact and can create speed increases of 300 to 1000 times (which is ridiculous!). It also means that if our website gets hammered with traffic, it is much better equipped to handle it. This is because the amount of processing required to return a cached result rather than completing the requests from scratch requires a lot less processing power.

Limitations of caching

So this all sounds amazing, but there are some limitations and downsides to Varnish. The most profound limitation is that page caching does not work with any pages that are user specific. Varnish will provide the same cached version of a page to every subsequent user that visits. This would be terrible in a situation where a user logs into a website, because information in their account area could be cached and delivered to other users. Varnish quite simply avoids this by bypassing caching if a cookie has been detected. It is a fair assumption that user specific content is being provided on a site that sets a cookie, but it is also very conservative.

The other major limitation is that cached pages very quickly become out of date. If our site has a blog for example, Varnish will not natively detect when a new post has been added and will continue to display whatever was cached. We can work around this though by setting an appropriate TTL (time to live) which will automatically flush the cache and update to the latest version of the page. We can also employ 3rd party extension for our chosen CMS that purge chosen items when they are deemed to be out of date. The WordPress plugin Varnish HTTP Purge will purge any posts or pages in the Varnish cache when it detects that it has been modified within the CMS. The Magento extension Turpentine fills a similar role.

Installing Varnish

Setting up Varnish on your server is quite a simple process. Access to your web server with sudo privileges is required. The process is slightly different depending on which OS your server is running. Here’s an article for CentOS setup:

Setting up Varnish with Magento

As I mentioned earlier, sites with user specific data require a bit more work to get them working with Varnish. Thankfully, there is an awesome Magento extension called Turpentine that works with Magento to deliver users with as much cached material as we can. It does this by modifying Varnish’s configuration to suit Magento and it also modifies Magento’s behaviours to increase caching rates. Be aware that Turpentine is not compatible with Varnish 4 so be sure to install 3.x in order to get it working.

Download and install Turpentine from Magento Connect and use its thorough configuration guide.

Once you’re up and running, Varnish will take the load off of your web server immediately. The huge benefits that it boasts though will only be noticed if your site receives high traffic. Magento stores really benefit from Varnish because they can use a lot of web server resources when generating pages. With Varnish correctly installed and setup to work with Magento, you will definitely see some speed improvements. Good luck!