Setting up Load Balancing
Load balancing in KraftCloud is very easy: as soon as you attach more than one instance to a service group, KraftCloud will automatically start balancing traffic between the multiple instances. The load balancing is done based on the number of connections (in TCP mode) or requests (in HTTP mode); more on that below. This diagram illustrates the overall mechanism:
Because of load balancing, instances in a service group must provide the same service. You can remove instances from a service group at any time, and, when you do, KraftCloud will immediately take the instance out of the load balancing service group.
To set load balancing up, first use kraft cloud deploy
with the -p
flag so that a service group
is created as part of the instance creation. For example, let’s use NGINX as the app:
This single command will (a) create a service group via the -p
flag and (b) start an NGINX instance:
With this in place, it’s now time to start a second instance and attach it to the service group that was
just created (named, in this case, wandering-shape-n6mhimgn
):
The command’s output should be similar to this one:
Notice that both the url
and service group
fields in the 2 instances are the same, as they should be. To check that it worked, run the following command:
You should see output such as:
Note the two instances (their UUIDs) under the instances
field. You’re now load balancing across 2 NGINX instances!
Load Balancing Algorithm
The load balancing algorithm is a variant of least_conn
. For every instance, we track the number of current in-flight
TCP connections (if in tcp
mode) or requests (if in http
mode).
To select an instance, we go over all instances in the service group and find the set of instances that have the least amount of in-flight requests/connections, and pick randomly from that set.
To illustrate, imagine we had the following scenario:
In this case, the algorithm would first choose instances i-1
and i-3
, since they both have the least number of
connections at the moment (only 1 each). After that, the algorithm would choose randomly between these 2 instances
and assign the new connection to it. For example, if it chose i-1
, the next new connection would go to i-3
since it’d be now the only instance with only 1 connection (assuming none of the connections that the other instances
are handling are closed).
Learn More
- The
kraft cloud
CLI reference, and in particular the services sub-command. - KraftCloud’s REST API reference, and in particular the section on service groups.