devroom.io/content/posts/2010-03-24-how-a-little-varnish-changed-my-life.md
2019-06-05 14:32:16 +02:00

75 lines
3.7 KiB
Markdown

+++
date = "2010-03-24"
title = "How a little varnish changed my life"
tags = []
slug = "how-a-little-varnish-changed-my-life"
+++
Okay, it's a bit of an exaggeration to say [varnish][1] 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.
[1]: http://varnish-cache.org/
~
I'm running on a Linux Debian virtual server (provided by [Kabisa][2]) 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:
``` text
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:
``` shell
$ 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][1] now or ask us at [Kabisa][2] to help you out!
[2]: http://kabisa.nl