Use Varnish Cache only on-disk

cachedisk-cachevarnish

The situation is:

I' m building a PHP application and need http caching.

Varnish is great and lots of people tell me that Varnish stores the cached data in RAM. I want it to cache on hard disk.

Is there any way to store the Varnish cache-data on hard disk?

Best Answer

There are 2 main ways Varnish caches your data:

  1. to memory (with the malloc storage config)
  2. to disk (with the file storage config)

You are asking for #2. In this method Varnish will always write the cache to disk and rely on the OS virtual memory subsystem to keep the most used disk pages in RAM.

If you are using Red Hat/CentOS, edit your /etc/sysconfig/varnish

NFILES=131072
MEMLOCK=82000
VARNISH_VCL_CONF=/etc/varnish/default.vcl
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082
VARNISH_MIN_THREADS=200
VARNISH_MAX_THREADS=2000
VARNISH_THREAD_TIMEOUT=120
VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin
VARNISH_STORAGE_SIZE=50%
#VARNISH_STORAGE_SIZE=1G
VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
VARNISH_TTL=120

# DAEMON_OPTS is used by the init script.  If you add or remove options, make
# sure you update this section, too.
#             -h classic,500009 \
DAEMON_OPTS="-f ${VARNISH_VCL_CONF} \
             -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
             -t ${VARNISH_TTL} \
             -w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} \
             -u varnish -g varnish \
             -s ${VARNISH_STORAGE} \
             -p thread_pool_min=200 \
             -p thread_pool_max=2000 \
             -p thread_pools=8 \
             -p listen_depth=4096 \
             -p session_linger=50/100/150 \
             -p lru_interval=60"

If not, you want something like this:

 varnishd -s file,/var/lib/varnish/varnish_storage.bin,50%

50% will use half the available disk. You can also use 10G etc. When using the file storage it is recommended to mount /var/lib/varnish to a non-journaled file system (eg ext2) and to use noatime and nodiratime when mounting the FS.

Related Topic