8

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.

user67641
  • 1,292
  • 2
  • 14
  • 18

2 Answers2

9

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:

Day
  • 131
  • 7
Sameer
  • 4,118
  • 2
  • 17
  • 11
  • 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 01 '11 at 20:29
  • 1
    Is there a way to plug Varnish into other memory based backends? – CMCDragonkai May 20 '14 at 00:50
  • 1
    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. – BenMorel Mar 17 '16 at 12:56
7

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

kontextify
  • 189
  • 1
  • 10
quiver
  • 298
  • 3
  • 6
  • What does the `if (req.url ~ "html")` condition do? I think the OP wants to use both storage backends in the most efficient way for all requests. – kontextify Jun 12 '19 at 12:55