Skip to content

Varnish cache invalidation — purge vs ban vs xkey

How to invalidate Varnish cache safely — when to use HTTP PURGE, when to use ban-list, and how xkey turns it into surgery instead of a sledgehammer.

Invalidation is the operational half of Varnish that the install articles never cover. A request to evict /products/sku-12345 from the cache has three implementations in Varnish — PURGE, ban, and xkey — and they behave very differently under load. This article is the reference card we hand operators: when each one applies, what it costs, and what to wire into the application.

How to verify

Before changing anything, see what is already in the cache and what your invalidation pipeline does today.

varnishadm "ban.list"
varnishstat -1 -f MAIN.bans -f MAIN.bans_lurker_tested -f MAIN.n_purges
varnishlog -g request -q 'ReqMethod eq "PURGE" or ReqMethod eq "BAN"' -i ReqMethod,ReqUrl,VCL_return,RespStatus
curl -X PURGE -H "Host: example.com" http://127.0.0.1/products/sku-12345

If MAIN.bans shows hundreds of entries growing without trimming, the ban-lurker is not keeping up and the cache is doing a linear scan on every lookup. That is a production problem.

What’s happening

A purge removes a specific cache object identified by its hash key. It is O(1), fast, and only invalidates one variant. It cannot do “everything matching /products/*” — for that, you need a ban.

A ban registers a predicate (e.g. obj.http.X-Url ~ "^/products/") against the cache. On every cache lookup, Varnish checks the object against the ban list — newer bans only. A background “ban-lurker” thread walks the cache asynchronously, evaluating bans against stored objects and removing matches; bans that have processed every object are dropped. If the lurker is too slow, the ban list grows and every lookup pays a linear cost.

xkey (a VMOD shipped with Varnish Enterprise and ported to open source as libvmod-xkey) adds secondary keys to cache objects. A response is tagged with one or more keys (xkey: product-sku-12345 category-electronics), and an invalidation request says “evict everything tagged with product-sku-12345.” This gives you the granularity of ban with the cost of purge.

The procedure

  1. Implement HTTP PURGE in VCL. Restrict by ACL so only the application or admin tools can purge.

    acl purge_acl {
        "127.0.0.1";
        "10.0.0.0"/8;
    }
    sub vcl_recv {
        if (req.method == "PURGE") {
            if (!client.ip ~ purge_acl) { return (synth(403, "Forbidden")); }
            return (purge);
        }
    }
    sub vcl_purge {
        return (synth(200, "Purged"));
    }
  2. Implement HTTP BAN for pattern invalidation. Pin the predicate to obj.* properties so the ban-lurker can evaluate it without seeing the original request.

    sub vcl_recv {
        if (req.method == "BAN") {
            if (!client.ip ~ purge_acl) { return (synth(403)); }
            ban("obj.http.X-Host == " + req.http.X-Ban-Host
              + " && obj.http.X-Url ~ " + req.http.X-Ban-Url);
            return (synth(200, "Ban added"));
        }
    }
    sub vcl_backend_response {
        set beresp.http.X-Host = bereq.http.Host;
        set beresp.http.X-Url = bereq.url;
    }
    sub vcl_deliver {
        unset resp.http.X-Host;
        unset resp.http.X-Url;
    }
  3. Adopt xkey for application-driven invalidation. Install the VMOD, tag responses, and use xkey.purge() instead of bans for tag-style invalidation.

    import xkey;
    sub vcl_backend_response {
        if (bereq.url ~ "^/products/") {
            set beresp.http.xkey = "product-" + regsub(bereq.url, "^/products/([^/]+).*", "\1")
                                 + " category-electronics";
        }
    }
    sub vcl_recv {
        if (req.method == "PURGE" && req.http.xkey) {
            if (!client.ip ~ purge_acl) { return (synth(403)); }
            set req.http.X-Purges = xkey.purge(req.http.xkey);
            return (synth(200, "Purged " + req.http.X-Purges));
        }
    }
  4. Wire the application. A model-save hook fires an HTTP request to Varnish with the appropriate tag.

    curl -X PURGE -H "xkey: product-sku-12345" http://varnish.internal:6081/
    curl -X BAN -H "X-Ban-Host: example.com" -H "X-Ban-Url: ^/products/" http://varnish.internal:6081/
  5. Monitor the ban list. A growing ban list with a slow lurker is a silent latency regression. Alert when MAIN.bans exceeds 1000 or when MAIN.bans_lurker_tested stops moving.

    varnishstat -1 -f MAIN.bans -f MAIN.bans_completed -f MAIN.bans_lurker_tested
    varnishadm ban.list | head -20

Common pitfalls

  • Issuing a ban with req.url ~ ... (request-time predicate) instead of obj.http.X-Url ~ ... (object-time) — request-time bans cannot be evaluated by the lurker and stay on the list forever.
  • Wiring every database write to a “ban everything” call (ban("obj.http.X-Host ~ .*")) — you have just turned the cache off until the lurker finishes.
  • Putting xkey tags into Set-Cookie or Vary instead of a dedicated header — those are reserved for protocol behaviour.
  • Allowing PURGE from the public internet because “the ACL feels restrictive” — adversaries will spend an afternoon purging your hot objects.
  • Forgetting that purges and bans do not propagate across a Varnish cluster — you need a fan-out (broadcast script or VMOD) or all of them lag.

In the engagements we run, invalidation is the line item that separates a cache that “works in dev” from a cache that earns its rent in production. Stack Harbor specifies the invalidation pipeline up front, wires it into the application’s domain events, and monitors the ban list as a first-class signal. See our managed operations for how that operating model plays out.