Manual section: | 3 |
---|
import directors [from “path”] ;
vmod_directors enables backend load balancing in Varnish.
The module implements a set of basic load balancing techniques, and also serves as an example on how one could extend the load balancing capabilities of Varnish.
To enable load balancing you must import this vmod (directors) in your VCL::
import directors;
Then you define your backends. Once you have the backends declared you can add them to a director. This happens in executed VCL code. If you want to emulate the previous behavior of Varnish 3.0 you can just initialize the directors in vcl_init, like this::
sub vcl_init {
new vdir = directors.round_robin();
vdir.add_backend(backend1);
vdir.add_backend(backend2);
}
As you can see there is nothing keeping you from manipulating the directors elsewhere in VCL. So, you could have VCL code that would add more backends to a director when a certain URL is called.
Note that directors can use other directors as backends.
Create a round robin director.
This director will pick backends in a round robin fashion.
Create a fallback director.
A fallback director will try each of the added backends in turn, and return the first one that is healthy.
Add a backend to the director.
Note that the order in which this is done matters for the fallback director.
Create a random backend director.
The random director distributes load over the backends using a weighted random probability distribution.
Add a backend to the director with a given weight.
Each backend backend will receive approximately 100 * (weight / (sum(all_added_weights))) per cent of the traffic sent to this director.
Create a hashing backend director.
The director chooses the backend server by computing a hash/digest of the string given to .backend().
Commonly used with client.identity or a session cookie to get sticky sessions.
Add a backend to the director with a certain weight.
Weight is used as in the random director. Recommended value is 1.0 unless you have special needs.
Pick a backend from the backend director.
Use the string or list of strings provided to pick the backend.