RedGiant
RedGiant

Reputation: 4748

Does Varnish 4 use hash key to look up objects when purging or banning?

When varnish invalidates cache via Purge and Ban, does it use the hash keys defined in vcl_hash to help looking up the objects? If I'm going to invalidate cache by a custom header obj.http.page_id instead of obj.http.url recommended by this article, would it affect the performance since the hash keys don't use http.page_id? I don't see any mention of this in the document, but want to make it clear before using the new method.

   sub vcl_recv {

      if (req.method == "PURGE") {

        if (!client.ip ~ purge) {

            return (synth(405, "Not allowed."));
        }

        ban("obj.http.url ~ ^" + req.url);

        return (purge);
      }
   }

   sub vcl_hash {
        hash_data(req.url);
        if (req.http.host) {
            hash_data(req.http.host);
        } else {
            hash_data(server.ip);
        }
   }

Upvotes: 0

Views: 919

Answers (2)

Ray Jennings
Ray Jennings

Reputation: 345

A BAN is more involved than a PURGE. A PURGE has to match the obj exactly from the value returned from vcl_hash(). A BAN can use regular expressions and is put on a BAN list. Each request coming in has to check the BAN list in determining if the cache contains stale data. Traversing the BAN list for each request can cause a drop in performance if the number of BANs becomes too large. The good news is that BANs are removed by a ban-lurker-thread which walks the cache and compares the timestamp of objects in the cache with the timestamp of the BAN request. If there are no cache objects with a timestamp earlier than the BAN request that match the BAN's regex, the BAN is removed from the list since it is no longer useful.

It might be useful to have separate conditions for PURGE and BAN:

if (req.method == "BAN")
{
    // TODO: Add client validation as needed
    ban("obj.http.x-url ~ " + req.url);
    return(synth(901, "BAN Set for " + req.url));
}

if (req.method == "PURGE")
{
    // TODO: Add client validation as needed
    return(purge);
}

Upvotes: 1

Joshua DeWald
Joshua DeWald

Reputation: 3229

If you're doing a BAN, then it does not need to match the hash, since the BAN is going to apply when the object(s) are looked up.

A PURGE however will do a normal object lookup, and so you could not do it using an alternate key.

Upvotes: 2

Related Questions