Varnish Cache employs an innovative memory management system that combines both RAM and disk storage. The daemon uses malloc() to allocate memory for its workspace, with the primary storage being the -s malloc
backend. A typical configuration might look like:
varnishd -a :80 \
-f /etc/varnish/default.vcl \
-s malloc,1G \
-s file,/var/lib/varnish/storage.bin,2G
The actual RAM usage depends on several factors:
- Active working set size
- Storage backend configuration
- HTTP header complexity
- VCL complexity
For a production system handling ~10K requests per minute, you might see:
# Typical memory footprint breakdown
Shared memory: 500MB
VCL workspace: 50-100MB
Log buffers: 20MB
Transient storage: 200MB
These VCL tweaks can significantly reduce RAM consumption:
sub vcl_recv {
# Strip unnecessary headers
unset req.http.Accept-Encoding;
unset req.http.Cookie;
# Limit header sizes
if (req.http.Accept-Language ~ ".*") {
set req.http.Accept-Language = regsub(req.http.Accept-Language, "^([^,;]+).*$", "\1");
}
}
Essential varnishstat metrics for memory analysis:
varnishstat -1 -f \
MAIN.n_lru_nuked \
MAIN.s0.g_bytes \
MEMPOOL.live.bytes \
SMA.s0.c_bytes
For high-traffic systems, consider these kernel parameters:
# /etc/sysctl.conf
vm.overcommit_memory = 1
vm.max_map_count = 262144
kernel.shmmax = 68719476736
A memory-efficient production configuration for a 4GB RAM server:
varnishd -j unix,user=varnish \
-a :80 \
-T localhost:6082 \
-f /etc/varnish/optimized.vcl \
-p workspace_client=64k \
-p http_max_hdr=64 \
-p http_resp_hdr_len=8k \
-s malloc,512m \
-s file,/var/lib/varnish/varnish_storage.bin,4G
Varnish Cache primarily uses two memory areas:
struct vsm {
void *base;
size_t size;
};
struct workspace {
char *ptr;
size_t bytes;
};
The main memory consumers are:
- Varnish Shared Memory (VSM) - For configuration and statistics
- Workspace memory - Per-request working memory
- Storage backend - Where cached objects reside
Typical memory configurations:
# Small blog (10k pages)
malloc,256M
# Medium e-commerce (100k products)
malloc,2G
# Large media site (1M+ assets)
file,/var/lib/varnish/cache,50G
Example VCL for memory-efficient caching:
vcl 4.1;
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_backend_response {
# Cache objects up to 10MB for 1 hour
if (beresp.http.content-length ~ "^[0-9]{1,7}$" &&
std.integer(beresp.http.content-length, 0) < 10000000) {
set beresp.ttl = 1h;
} else {
return (abandon);
}
}
Essential varnishstat commands:
varnishstat -1 -f MAIN.n_lru_nuked
varnishstat -1 -f SMA.s0.c_bytes
varnishstat -1 -f MEMPOOL.*.live
Common issues and solutions:
- Object fragmentation - Increase workspace_client or workspace_backend
- Storage full - Monitor SMA.Transient.c_bytes
- Too many large objects - Implement size-based caching policies