Skip to main content

How a little varnish changed my life

·562 words·3 mins· ·
Ariejan de Vroom
Author
Ariejan de Vroom
Jack of all Trades, Professional Software Craftsman

Okay, it’s a bit of an exaggeration to say varnish changed my life, but it sure did change the speed of my site!

I got from a rotten 6 requests per second with WordPress to a whopping 9500! If you’re on Linux and running Apache, installing varnish is a breeze! Especially if you’re hosting a well cacheable site like a blog.

~

I’m running on a Linux Debian virtual server (provided by Kabisa) with 1 VCPU core and 512MB of memory. Nothing fancy.

Before I was running Apache 2 with PHP and WordPress. Doing the occasional test this gave me (uncached) a performance of about 6 request/second (using apache’s ab with 10 concurrent connections). With some caching plugins I was able to crank that up to about 15-20 requests a second.

After upgrading my blog to Toto, I got quite a boost to about 30 requests/second. But, my blog doesn’t contain any dynamic elements any more and Toto + Rack give you all the handles to implement caching (ETags, Cache-Control headers, etc.). FYI: I’m running Apache2 + Passenger to run Toto.

Since I heard about Varnish a few times before I decided to give it a try and apt-get install varnish‘ed it on my Debian box (I’m running squeeze, thank you).

Now I have a few other sites running on my vps which I don’t want to cache just yet. The problem was how do I tell Varnish to only cache ariejan.net, and skip the rest.

Here’s the entire configuration for Varnish to accomplish just that:

backend default { .host = "127.0.0.1"; .port = "8080"; }
sub vcl_recv { if (req.http.host !~ "ariejan.net") { return(pass); } }

Yes, that is just two lines! What this does is forward everything you throw at varnish to the server at port 8080. The vcl_recv makes sure that if the hostname does not include ariejan.net varnish passes the request forward - no caching.

The second thing I had to do was configure Apache to listen on port 8080 instead of 80 in /etc/apache2/ports.conf. Then also make sure to have all your virtual hosts (even those you don’t want cached) configured for port 8080 too in /etc/apache2/sites-available

Restart apache, restart varnish and you’re golden!

When I first ran my ab benchmark with 10 concurrent connections I got to about 150 requests per second. But when I really pushed it to a 1000 concurrent connections (ab couldn’t handle more), I got to a whopping 9500 requests per seconds doing 60k requests! That is epic! Oh, and my VPS didn’t even break a sweat - system load got up to 1.08 for second or so.

For the record:

$ ab -c 1000 -n 60000 http://ariejan.net/2010/03/22/shields-up-rrrack-alert/

Server Software:        Apache/2.2.15
Server Hostname:        ariejan.net
Server Port:            80

Document Path:          /2010/03/22/shields-up-rrrack-alert/
Document Length:        5117 bytes

Concurrency Level:      1000
Time taken for tests:   6.290 seconds
Complete requests:      60000
Failed requests:        0
Write errors:           0
Total transferred:      331434376 bytes
HTML transferred:       307460062 bytes
Requests per second:    9539.34 [#/sec] (mean)
Time per request:       104.829 [ms] (mean)
Time per request:       0.105 [ms] (mean, across all concurrent requests)
Transfer rate:          51459.38 [Kbytes/sec] received

Connection Times (ms)
                min  mean[+/-sd] median   max
Connect:        1   56 364.9     12    6209
Processing:     2   22  76.7     16    3073
Waiting:        2   19  76.6     13    3070
Total:          3   78 374.6     29    6223

Interested? Check out Varnish now or ask us at Kabisa to help you out!