Last updated

How a little varnish changed my life

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:

1backend default { .host = "127.0.0.1"; .port = "8080"; }
2sub 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:

 1$ ab -c 1000 -n 60000 http://ariejan.net/2010/03/22/shields-up-rrrack-alert/
 2
 3Server Software:        Apache/2.2.15
 4Server Hostname:        ariejan.net
 5Server Port:            80
 6
 7Document Path:          /2010/03/22/shields-up-rrrack-alert/
 8Document Length:        5117 bytes
 9
10Concurrency Level:      1000
11Time taken for tests:   6.290 seconds
12Complete requests:      60000
13Failed requests:        0
14Write errors:           0
15Total transferred:      331434376 bytes
16HTML transferred:       307460062 bytes
17Requests per second:    9539.34 [#/sec] (mean)
18Time per request:       104.829 [ms] (mean)
19Time per request:       0.105 [ms] (mean, across all concurrent requests)
20Transfer rate:          51459.38 [Kbytes/sec] received
21
22Connection Times (ms)
23                min  mean[+/-sd] median   max
24Connect:        1   56 364.9     12    6209
25Processing:     2   22  76.7     16    3073
26Waiting:        2   19  76.6     13    3070
27Total:          3   78 374.6     29    6223

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

Tags: