Hero Image

Tune Varnish Cache on RHEL 9

Tune Varnish Cache on RHEL 9

Memory and storage

Systemd service override (/etc/systemd/system/varnish.service.d/override.conf):

[Service]
ExecStart=
ExecStart=/usr/sbin/varnishd \
    -a :80 \
    -T localhost:6082 \
    -f /etc/varnish/default.vcl \
    -s malloc,2G \
    -p thread_pools=2 \
    -p thread_pool_min=200 \
    -p thread_pool_max=4000 \
    -p thread_pool_timeout=300 \
    -p http_max_hdr=64 \
    -p workspace_backend=128k \
    -p workspace_client=64k

Key runtime parameters

Parameter Default Recommendation
thread_pools 2 = CPU cores
thread_pool_min 100 200-500
thread_pool_max 5000 4000-10000
default_ttl 120 depends on app
http_max_hdr 64 increase if needed

Monitor with varnishstat

varnishstat -1 | grep -E 'MAIN\.(cache_hit|cache_miss|threads|n_object|uptime)'

Hit rate calculation

varnishstat -1 -f MAIN.cache_hit -f MAIN.cache_miss | \
    awk '{sum[$1]=$2} END {printf "Hit rate: %.1f%%\n", sum["MAIN.cache_hit"] / (sum["MAIN.cache_hit"] + sum["MAIN.cache_miss"]) * 100}'

Log analysis with varnishlog

# Watch all requests in real time
varnishlog -i ReqURL -i RespStatus

# Only cache misses
varnishlog -q "VCL_call eq MISS" -i ReqURL