Varnish more than Round Robin load balancing

Varnish Configuration Language shows how to setup health checks

Backend Declaration

backend www {
  .host = "www.example.com";
  .port = "http";
}

The backend object can later be used to select a backend at request time:

if (req.http.host ~ "(?i)^(www.)?example.com$") {
  set req.backend = www;
}

Syntax checking VCL files

To Debug a VCL file use the following where default.vcl is the full path the your .vcl file.

 sudo varnishd -C -f default.vcl

The timeout parameters can be overridden in the backend declaration. The timeout parameters are .connect_timeout for the time to wait for a backend connection, .first_byte_timeout for the time to wait for the first byte from the backend and .between_bytes_timeout for time to wait between each received byte.

These can be set in the declaration like this:

backend www {
  .host = "www.example.com";
  .port = "http";
  .connect_timeout = 1s;
  .first_byte_timeout = 5s;
  .between_bytes_timeout = 2s;
}

Directors

To test this file go to http://testvar1.nuskin.net:6081

# This is a basic VCL configuration file for varnish.  /etc/varnish/default.vcl

# Simple multi backend definition. Works for Varnish version 3

probe healthcheck {
   .url = "/";
   .interval = 30s;
   .timeout = 0.5 s;
   .window = 5;
   .threshold = 2;
   .initial = 2;
   .expected_response = 200;
}
backend default {
    .host = "test.nuskin.com";
    .port = "8080";
}

backend pub1{
    .host = "testpub1.nuskin.net";
    .port = "8080";
    .probe = healthcheck;
}

backend pub2{
    .host = "testpub2.nuskin.net";
    .port = "8080";
    .probe = healthcheck;
}

director adobe_cluster round-robin {
  { .backend = pub1; }
  { .backend = pub2; }
}

sub vcl_recv {
  set req.backend = adobe_cluster;
}

 

Now we need tell Varnish where to send the difference URL. Lets look at vcl_recv.:

sub vcl_recv {
    if (req.url ~ "^/content/") {
        set req.backend_hint = adobe.backend();
    } else {
        set req.backend_hint = default;
    }
}

Backend probes

Probes take the following parameters:

.url

Specify a URL to request from the backend. Defaults to "/".

.request

Specify a full HTTP request using multiple strings. .request will have \r\n automatically inserted after every string. If specified, .request will take precedence over .url.

.window

How many of the latest polls we examine to determine backend health. Defaults to 8.

.threshold

How many of the polls in .window must have succeeded for us to consider the backend healthy. Defaults to 3.

.initial

How many of the probes are considered good when Varnish starts. Defaults to the same amount as the threshold.

.expected_response

The expected backend HTTP response code. Defaults to 200.

.interval

Defines how often the probe should check the backend. Default is every 5 seconds.

.timeout

How fast each probe times out. Default is 2 seconds.

 

Varnish can also serve stale content if all the backends are down. See Misbehaving servers for more information on how to enable this.