edoceo

How To: Varnish Cache Performance Tuning

Varnish cache is not a magic bullet! You have to monitor this system and ensure the numbers are hitting metrics that you determien to be "proper" There is no such thing as set-it-and-forget-it when it comes to web-site operations.

Checking Status

Using the built in tool varnishstat a nice terminal display is created. Here's a sample from one that I just started a few minutes ago.

0+01:03:00                                                                                                                                                   staging
Hitrate ratio:        4        4        4
Hitrate avg:     0.0000   0.0000   0.0000

         605        12.52         0.16 Client connections accepted
         605        12.52         0.16 Client requests received
         605        12.52         0.16 Cache misses
          39         0.20         0.01 Backend conn. success
         566        12.32         0.15 Backend conn. reuses
          33         0.13         0.01 Backend conn. was closed
         605        12.52         0.16 Backend conn. recycles
         583        12.39         0.15 Fetch with Length
          22         0.13         0.01 Fetch chunked
          15          .            .   N struct sess_mem
         352          .            .   N struct object
         352          .            .   N struct objectcore
         352          .            .   N struct objecthead
         703          .            .   N struct smf
           0          .            .   N small free smf
           2          .            .   N large free smf
           6          .            .   N struct vbe_conn
          20          .            .   N worker threads
          20         0.00         0.01 N worker threads created
           1          .            .   N backends
         253          .            .   N expired objects
         601        12.45         0.16 Objects sent with write
         605        12.52         0.16 Total Sessions
         605        12.52         0.16 Total Requests
         605        12.52         0.16 Total fetch
      188956      3883.58        49.99 Total header bytes
     4286643     38230.67      1134.03 Total body bytes
         605        12.52         0.16 Session Closed
       46143       880.82        12.21 SHM records
        6196        75.79         1.64 SHM writes
           4         0.00         0.00 SHM flushes due to overflow
          10         0.27         0.00 SHM MTX contention
        1206        24.97         0.32 allocator requests
         701          .            .   outstanding allocations
     5111808          .            .   bytes allocated
  1068630016          .            .   bytes free
         605        12.52         0.16 Backend requests made
           1         0.00         0.00 N vcl total
           1         0.00         0.00 N vcl available
           2         0.00         0.00 N new purges added
        3780         1.00         1.00 Client uptime

My cache is consuming 450MiB, with 1G of total space; so there is nice room to run here. Watch these number for your site. Performance is about establishing a baseline, setting a goal and making one change and measure again.

Worker Threads

If you find that Varnish hasn't enough worker threads adjust those with the -s switch.

    -w 8,32,3600

Storage

I like to split my Varnish cache across multiple small(ish) files. Also, preallocate your files!

~ # dd if=/dev/zero of=/var/cache/varnish.a bs=1m count=512
~ # dd if=/dev/zero of=/var/cache/varnish.b bs=1m count=512
~ # dd if=/dev/zero of=/var/cache/varnish.c bs=1m count=512
~ # varnishd
    -s malloc,512m \
    -s file,/var/cache/varnish.a,512m \
    -s file,/var/cache/varnish.b,512m \
    -s file,/var/cache/varnish.c,512m \

Hash Types

If you really feel like tweaking your environment try adjusting the hash.

    -t classic,32768

Loading Comments from Disqus...