Boost WordPress performance with Varnish Cache

Posted by on Mar 2, 2013 in Environment, Linux | 3 Comments

Lets face it, WordPress is slow. With every request it has to go though thousands lines of code and multiple SQL queries to render a page. Very popular configuration for a WordPress site is Apache, mod_rewrite, mod_php, PHP and MySQL. It’s very good setup but can’t be consider the fastest (at least without any additional tweaking).
The good news is WordPress doesn’t have to be a speed demon. In most cases it’s just a CMS to produce static pages. If the content is static it doesn’t make any sense to waste CPU cycles on re-rendering the same HTML over and over again.

Currently I’m running this blog on Amazon EC2 Micro instance. It’s the smallest (and slowest) setup you can get from Amazon. The micro instance has 613 MB of RAM and a limited access to CPU. When I tried to benchmark it with the Apache Benchamark I got about 1 req/sec… and crashed database. Ten concurrent connections were enough to DoS my blog! Fifteen minutes later which was required to setup Varnish Cache the same hardware could handle 665 req/sec. During the test CPU got to 65% which means I reached broadband limits.

So why to make a website fast? There are at least 3 reasons:
- It will survive sudden traffic spikes (so called Slashdot effect)
- better Google ranking
- according to Google’s research there is a correlation between website’s response time and consumed content. In other words the faster website is the higher chance for another click.

Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 – 1000x, depending on your architecture.

Varnish stands in front of a web server which means it will have to listen on port 80. By default this port it already taken by Apache. The first thing is to change configuration of your web server.

Change port 80 to 8080.

Edit virtual host settings.

Alter port from 80 to 8080.

Now install varnish. Depends on your distribution you might get different version of the software. This article is created for varnish 3.x.

Edit varnish settings and set port and cache size.

Memory on my server is limited so I use only 64 MB. Default value is 254 but quoter of that is enough for a small blog.

Edit varnish configuration.

Change the VCL script to

You will have to change the IP 54.246.44.13 to your server’s address.
Everything is in place now so the last thing to do is restartng services.

To make sure caching is working run tail against all apache logs and refresh your website few times.

First request will hit Apache server and you should see new entries in the log. Varnish will put all resources for that URL in cache. Every following request shouldn’t populate any new logs.

The VCL configuration makes Varnish cache every GET request for 96 hours. There is an exception for wp-login and wp-admin. Those are dynamic pages and it’s better not to cache them. Your visitors have no reason to go there anyway.

Varnish will cache everything for 96h. If you create a new post or edit an existing one your changes won’t be visible. Varnish doesn’t know something has changed and will continue serving stale content. You can restart the service to wipe all data.

It’s not very user friendly approach. It also clears more then it should. Don’t worry, it’s WordPress. There is plugin for everything. Install “Varnish HTTP Purge” extension. No configuration is required. Default.vcl file already handlers PURGE requests. Now you can visit your blog, open network tab in Firebug and enjoy response time below 200ms.

Varnish is an amazing peace of software with very powerful features. It can act as a load balancer, it can pull different parts of your website from different palaces (ESI) and if it can’t do something there might be an extension for that. There is nothing to wait for. Download it. Use it.

3 Comments

  1. Guilherme Euler
    24/12/2013

    Hello friend,

    When I try to start varnish with this VCL I get the following error:

    Message from VCC-compiler:
    Expected variable, string or semicolon
    (input Line 10 Pos 38)
    req.http.X-Forwarded-For + “, ” + client.ip;
    ————————————-#——————
    Running VCC-compiler failed, exit 1
    VCL compilation failed

    What’s happening?

    Reply
  2. Ian Chard
    23/06/2014

    Great stuff — thanks so much for this, it saved me having to figure out the cookie faff to make WordPress admin happy.

    I added one more test to bypass varnish for the Jetpack site monitor, so that it actually tests the back end:

    if (req.http.User-Agent ~ “^jetmon/”) {
    /* Don’t cache so that jetpack can monitor backend health */
    return (pass);
    }

    Reply
  3. Prasenjit
    11/07/2014

    Hi Lucasz,

    Thanks for the configs. I am new to Varnish and am trying to configure it on a test site. I can see that the Varnish headers are being added, but when I do “varnishstat”, I get the below output. Hit ratio and hitrate avg is always 0. Can you point me to the right direction what the below output means?

    Thanks.

    Reply

Leave a Reply