Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm interested in maximizing cache hits and efficiency for a slow-changing site. The virtual host doesn't have a huge amount of RAM, but I'd like to use what's available for varnish, but to fall back to disk cache if there isn't enough memory.

Is it possible to do this with a single instance of varnish? The docs describe "file" and "malloc" storage as distinct options.

share|improve this question
up vote 8 down vote accepted

Use the malloc method. It will try to put everything in RAM and the kernel will swap it out if needed. This way you are using memory and disk together.

At the same time file performs much better than malloc when you start hitting disk. For more info, see:

share|improve this answer
Thanks, this works. I think I'll just use malloc method with available RAM, and rely on the server to do disc caching (e.g. apache mod_disk_cache). – user67641 Feb 1 '11 at 20:29
Is there a way to plug Varnish into other memory based backends? – CMCDragonkai May 20 '14 at 0:50
Note that you could actually do the opposite: use only the file backend, and rely on Linux's disk caching that uses all available memory by default. Yes, you're always writing to disk (which may be an issue if you're not using SSDs), but when reading multiple times the same files, you'll read a lot from memory only. – Benjamin Mar 17 at 12:56

You need to name storage respectively as follows and in vcl you specify which backend storage you want to use with = storage_name. .

Varnish 3.* process options

DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s foo=malloc,512m \
             -s bar=file,/var/lib/varnish/varnish_storage.bin,512m"

vcl v3

sub vcl_fetch {
    if (req.url ~ "html") {
       set = "foo";
       set beresp.http.x-storage = "foo";
    } else {
       set = "bar";
       set beresp.http.x-storage = "bar";
    return (deliver);

For Varnish v4, you can follow the official blog post's instruction

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.