While Varnish Cache famously uses memory (RAM) as its primary storage backend for blazing-fast performance, many developers don't realize it can be configured for disk-based storage. This becomes crucial when:
- Your server has limited RAM but ample disk space
- You need persistent cache that survives reboots
- You're caching very large files that exceed available memory
Here's how to modify your Varnish VCL configuration to use disk storage:
varnishd -s malloc,1G -s file,/var/lib/varnish/varnish_storage.bin,10G
This command does two things:
- Allocates 1GB of RAM (-s malloc,1G)
- Creates a 10GB disk-based cache file (-s file,...)
For a production setup, you'll want a more complete configuration. Here's an example:
# /etc/default/varnish DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m \ -s file,/var/lib/varnish/varnish_cache.bin,50G"
While disk storage works, remember these performance implications:
Storage Type | Latency | Throughput | Best Use Case |
---|---|---|---|
RAM | ~100ns | Extremely High | Small, Hot Objects |
SSD | ~100μs | High | Medium/Large Objects |
HDD | ~10ms | Medium | Large, Cold Objects |
Add these parameters to optimize disk performance:
varnishd [...] -s file,/path/to/cache,50G,50% -p thread_pool_add_delay=2 -p thread_pool_add_threshold=2
The 50%
parameter tells Varnish to use 50% of the file immediately, while the thread pool parameters help with disk I/O contention.
Use these varnishstat commands to monitor disk usage:
varnishstat -1 -f MAIN.s_cache varnishstat -1 -f SMA.*
Key metrics to watch:
- SMA.s0.c_req - Cache requests
- SMA.s0.c_fail - Cache failures
- SMA.s0.g_bytes - Storage used
While Varnish Cache defaults to RAM storage for optimal performance, it can indeed be configured for disk-based caching when memory constraints exist or when persisting large caches between server restarts is necessary.
Varnish offers several storage backends through its stevedore interface. The most relevant for disk storage are:
# file: /etc/varnish/default.vcl
vcl 4.1;
backend default {
.host = "127.0.0.1";
.port = "8080";
}
# Disk storage configuration
sub vcl_init {
new disk_cache = directors.shard();
disk_cache.add_backend(
storage.file(
path="/var/lib/varnish/storage.bin",
size=10G,
granularity=1M
)
);
}
To properly configure disk storage:
- Create storage directory with proper permissions:
- Modify the Varnish service configuration:
sudo mkdir -p /var/lib/varnish
sudo chown -R varnish:varnish /var/lib/varnish
# file: /etc/default/varnish
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-s file,/var/lib/varnish/storage.bin,10G"
When using disk storage:
- SSD drives are strongly recommended over HDDs
- Consider adjusting
thread_pool_max
for better disk I/O handling - Monitor disk I/O wait times to identify bottlenecks
For more complex setups with multiple storage backends:
sub vcl_init {
# Small objects in memory
new mem_cache = storage.malloc(
size=512M
);
# Large objects on disk
new disk_cache = storage.file(
path="/var/lib/varnish/storage.bin",
size=50G
);
# Fallback to disk if memory is full
new hybrid_cache = storage.log(
storage.transport(mem_cache),
storage.transport(disk_cache)
);
}
Essential Varnish CLI commands for monitoring:
varnishadm storage.list
varnishadm backend.list
varnishadm stats.show