Best practice for caching scenario with different backend servers but same content

Hamidreza Hosseini hrhosseini at hotmail.com
Sat Aug 14 10:50:39 UTC 2021


Hi,
Thanks to you and all varnish team for such answers that helped me alot,
I read the default varnish cache configuration again:
https://github.com/varnishcache/varnish-cache/blob/6.0/bin/varnishd/builtin.vcl
and find out vcl_hash as follow:

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

```
So, if I change vcl_hash like following , would it be enough for my purpose?(I mean caching the same object from different backends just once with roundrobin directive !:)

```

sub vcl_hash {
    hash_data(req.url);
    return (lookup);
}

```

By this config I told varnish just cache the content based on the 'req.url' not 'req.http.host' therefore with the same content but different backend varnish would cache once(If I want to use round robin directive instead of shard directive ), Is this true? what bad consequences may it cause in the future by this configuration?

________________________________
From: varnish-misc <varnish-misc-bounces+hrhosseini=hotmail.com at varnish-cache.org> on behalf of Hamidreza Hosseini <hrhosseini at hotmail.com>
Sent: Sunday, August 1, 2021 4:17 AM
To: varnish-misc at varnish-cache.org <varnish-misc at varnish-cache.org>
Subject: Best practice for caching scenario with different backend servers but same content

Hi,
I want to use varnish in my scenario as cache service, I have about 10 http servers that serve Hls fragments as the backend servers and about 5 varnish servers for caching purpose, the problem comes in when I use round-robin director for backend servers in varnish,
if a varnish for specific file requests to one backend server and for the same file but to another backend server it would cache that file again because of different Host headers ! so my solution is using fallback director instead of round-robin as follow:

```
In varnish-1:
    new hls_cluster = directors.fallback();
    hls_cluster.add_backend(b1());
    hls_cluster.add_backend(b2());
    hls_cluster.add_backend(b3());
    hls_cluster.add_backend(b4());
    hls_cluster.add_backend(b5());
    hls_cluster.add_backend(b6());
    hls_cluster.add_backend(b7());
    hls_cluster.add_backend(b8());
    hls_cluster.add_backend(b9());
    hls_cluster.add_backend(b10());



In varnish-2:
    new hls_cluster = directors.fallback();
    hls_cluster.add_backend(b10());
    hls_cluster.add_backend(b1());
    hls_cluster.add_backend(b2());
    hls_cluster.add_backend(b3());
    hls_cluster.add_backend(b4());
    hls_cluster.add_backend(b5());
    hls_cluster.add_backend(b6());
    hls_cluster.add_backend(b7());
    hls_cluster.add_backend(b8());
    hls_cluster.add_backend(b9());


In varnish-3:
    new hls_cluster = directors.fallback();
    hls_cluster.add_backend(b9());
    hls_cluster.add_backend(b1());
    hls_cluster.add_backend(b2());
    hls_cluster.add_backend(b3());
    hls_cluster.add_backend(b4());
    hls_cluster.add_backend(b5());
    hls_cluster.add_backend(b6());
    hls_cluster.add_backend(b7());
    hls_cluster.add_backend(b8());
    hls_cluster.add_backend(b10());

```
But I think this is not the best solution, because there is no load balancing despite, I used different backend for the first argument of fallback directive,
What is varnish recommendation for this scenario?



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20210814/095efe90/attachment.html>


More information about the varnish-misc mailing list