Is it possible to run varnish with both memory and disk storage?

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.


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:

  • The Storage Backends section of the Tuning chapter of the Varnish Book
  • This blog post on "Varnish best practices" (from January 2010)

You need to name storage respectively as follows and in vcl you specify which backend storage you want to use with beresp.storage = 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 beresp.storage = "foo";
       set beresp.http.x-storage = "foo";
    } else {
       set beresp.storage = "bar";
       set beresp.http.x-storage = "bar";
    }
    return (deliver);
}

For Varnish v4, you can follow the official blog post's instruction https://info.varnish-software.com/blog/partitioning-your-varnish-cache