Difference between adjusting set req backend in vcl_recv and vcl_backend_fetch

Dridi Boukelmoune dridi at varni.sh
Wed Oct 6 05:46:40 UTC 2021


On Mon, Sep 6, 2021 at 2:22 PM Hamidreza Hosseini
<hrhosseini at hotmail.com> wrote:
>
> Hi,
> This is part of my varnish configuration file for sharding:
>
> ```
> cat /etc/varnish/default.vcl
>
> vcl 4.1;
>
> import directors;
>
>
> probe myprobe {
>     .request =
>       "HEAD /healthcheck.php HTTP/1.1"
>       "Connection: close"
>       "User-Agent: Varnish Health Probe";
>     .timeout = 1s;
>     .interval = 5s;
>     .window = 5;
>     .threshold = 3;
> }
>
>
> backend b-01 { .host = "b-01"; .port = "80"; .probe = myprobe; }
> backend b-02 { .host = "b-02"; .port = "80"; .probe = myprobe; }
> backend b-03 { .host = "b-03"; .port = "80"; .probe = myprobe; }
>
>
> sub vcl_init {
>
>
>   new hls_cluster = directors.shard();
>     hls_cluster.add_backend(b-01);
>     hls_cluster.add_backend(b-02);
>     hls_cluster.add_backend(b-03);
>
>
>   new p = directors.shard_param();
>
>     hls_cluster.set_rampup(30s);
> #    hls_cluster.set_warmup(0.5);
>
>     hls_cluster.reconfigure();
>     hls_cluster.associate(p.use());
>
> }
>
>
>
> acl purge {
>     "localhost";
> }
>
>
> sub vcl_recv {
>
>    set req.backend_hint = hls_cluster.backend();
>
> }
>
>
> sub vcl_backend_fetch {
>
>   p.set(by=KEY, key=hls_cluster.key(bereq.url));
>   set bereq.backend = hls_cluster.backend(resolve=LAZY, healthy=CHOSEN);
>
> }
>
> ```
> 1. there are two set backend in this config, one is on vcl_recv:
> "set req.backend_hint = hls_cluster.backend();"
> and one in vcl_backend_fetch:
> "set bereq.backend = hls_cluster.backend(resolve=LAZY, healthy=CHOSEN);"
> should I remove set in vcl_recv cause I think if I adjust it , all requset will go through this backend list and configs like healthy=CHOSEN in vcl_backend_fetch wouldnt be applied! Am I true?

bereq.backend is initialized from req.backend_hint when a backend
fetch is triggered. Setting bereq.backend will simply override
anything one in vcl_recv.

> 2.Actually what is difference between vcl_backend_fetch and vcl_recv?

In vcl_recv you are processing a client request that was just received.

In vcl_backend_fetch you are preparing a bereq (backend request)
derived from req (client request) in before fetching the resource from
the backend.

https://varnish-cache.org/docs/6.0/users-guide/vcl-built-in-subs.html

> 3.should I remove "set req.backend_hint" from vcl_recv?

If the answer to "do I use the backend selection to make decisions in
client subroutines?" is no, then you can remove it from vcl_recv.

The shard director has task-specific settings, so some things you may
configure in vcl_recv would not apply to vcl_backend_fetch, so if the
answer to the question above was yes, you would probably need both.

Best,
Dridi


More information about the varnish-misc mailing list