Skip to content

Balancing algorithms

Every balancer has to answer one question for each request: which server? The common answers differ in what the balancer needs to know.

  • Round robin sends each request to the next server in a fixed order. It keeps no count and looks at no server. It is the default of nginx, where it can be weighted, and of AWS’s Application Load Balancer.
  • Random sends each request to a server chosen at random. It also looks at nothing.
  • Least connections sends each request to the server that holds the fewest. nginx counts active connections and HAProxy counts connections, while AWS counts outstanding requests and calls it least outstanding requests. It needs a count for every server, kept up to date, each time it chooses.
  • Power of two choices draws two servers at random and sends the request to the one that holds fewer. Envoy’s least request balancer does this by default, nginx offers it as random two, and HAProxy’s random algorithm implements it.

Mental model. Each algorithm is a guess about which server’s line is the shortest. Round robin and random guess without looking. Least connections looks at every line. Two choices looks at two lines chosen at random.

Try it: the same requests, four algorithms

Section titled “Try it: the same requests, four algorithms”

The lab below is four servers, each of which works on one request at a time and takes 20 ms on average, so each can serve 50 requests a second and the four can serve 200. Requests arrive at random, 160 a second, which is 80% utilisation. Every algorithm in the table receives the identical requests, so a difference between two rows comes from the algorithm and not from luck.

Requests reach a load balancer, which places each one on one of several servers. Each server has its own waiting line. Choose how the balancer decides, slow one server down, switch on sticky sessions, and make a server fail or be replaced, then compare what every algorithm does with the identical requests.

160 req/s
Servers
4
ClientsHealthy<1%Load balancerHealthy<1%ServersSaturated80%
ClientsHealthy<1%Load balancerHealthy<1%ServersSaturated80%

Each dot stands for many requests. The numbers show the real rate.

Servers saturated: 160 req/s against request capacity of 200 req/s.

Load balancer: the same requests under every algorithm

The balancer
1×
0%
Health checks
5 s
3
During the run
Utilisation
80%Saturated107% without the last server
99th percentile
311.8 msone shared queue: 142.4 ms
Failed requests
00% of the requests

Round robin, 160 requests per second on 4 servers, 80% utilised: half of the requests took under 48.3 ms, 95% under 201.1 ms and 99% under 311.8 ms. The busiest server received 25% of the requests, where an even split would give 25%. No request failed. One shared queue, which no balancer can run, would give a 99th percentile of 142.4 ms.

Round robin: what each server did
ServerShare of requestsBusyWaiting in line99th percentile
Server 125%81%2.1348.4 ms
Server 225%83%2.3315.8 ms
Server 325%82%1.7291.1 ms
Server 425%81%1.8246.3 ms
The same requests, under every algorithm
AlgorithmMeanMedian95th percentile99th percentileFailedBusiest server
Round robin · shown above68.3 ms48.3 ms201.1 ms311.8 ms025%
Random103.8 ms73.7 ms301.9 ms433.8 ms025%
Least connections40.6 ms30.1 ms113.8 ms172.0 ms025%
Power of two choices48.9 ms37.4 ms135.0 ms193.8 ms025%
Consistent hash305.4 ms74.5 ms987.0 ms997.4 ms2,977 (15.2%)39%
One shared queue (benchmark)36.2 ms28.2 ms99.0 ms142.4 ms0—

Times are for the requests that were answered within a second. The last row is a benchmark and not an algorithm: it needs the servers to take work when they are free.

0 ms
Metrics
ComponentStateUtilisationOffered (req/s)Served (req/s)Rejected (req/s)p50p95p99
ClientsHealthy<1%16016000 ms0 ms0 ms
Load balancerHealthy<1%16016000.03 ms0.15 ms0.23 ms
ServersSaturated80%160160027 ms95 ms138 ms
Traffic
160 req/s
Successful
160 req/s
Error rate
0%
Mean latency
35 ms

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • Each node is one queue, served first-come first-served by several identical slots (instances × concurrency).
  • Requests arrive at random at a steady average rate. Each needs an exponentially distributed time on a server, 20 ms on average. Every algorithm in the table gets the identical requests, so the differences between its rows come from the algorithm and not from the luck of the draw.
  • Each server works on one request at a time, and has a waiting line of its own that holds up to 100 requests. A request that has not been answered after 1 second has failed, and a server does not work on one that has already given up.
  • The balancer knows only how many requests each server holds. It answers instantly, has room for any traffic, and never fails itself.
  • Each figure comes from one run of 120 simulated seconds, after 10 seconds of warm-up, with a fixed random seed. The run can be repeated exactly, but it is one run: a difference of a few per cent between two rows is within the noise.

Simplifications

  • Above capacity there is no steady state. The model switches to a fluid approximation: the node serves at capacity, its queue grows until it reaches its limit, and further requests are rejected.
  • The diagram, and the table of components under it, come from the graph engine: they treat the servers as one pool with one shared queue and never put a request on one server. That is the benchmark in the table, not what any balancing algorithm achieves.
  • A server that fails stops answering without a word, so each request sent to it waits for its timeout. A server whose program has stopped but whose machine is up can refuse connections at once instead, and a balancer can try another server. The model shows neither the refusal nor the retry.
  • A health check here is a probe that fails only when the server is down. Real probes can also fail on a server that is merely busy, and a balancer can also learn from the requests that fail.
  • Sticky sessions: 200 sessions send the sticky share of the traffic, a few of them much more than the rest (a Zipf distribution with exponent 0.8). Each is pinned to one server at random. If its server is taken out, the session is placed again and loses what that server held. Real systems have more sessions, and other distributions.
  • Consistent hash gives each server 100 places on a ring and hashes the session, so a session always reaches the same server, whatever the sticky share. It places requests by session and not by load.
  • A deploy takes out the last server. The new version starts 30 seconds later and is ready at once. The balancer puts it back at once if it was told about the deploy, and at its next passing check if it was not. Real servers take time to start and warm up.
  • Only the first server can be slow, by the factor you set, on every request. Servers of different sizes, or that slow down only under load, are not modelled.
  • There is no network time, connection set-up, TLS or keep-alive, and no difference between layer 4 and layer 7: a request is placed on a server, and nothing is said about how.
  • The one shared queue is a benchmark, not an algorithm that a balancer can run, because it needs the servers to take work when they are free. It shows how much the choice of server costs.

Parameter values are illustrative. They are not benchmarks of any real product.

Read the table at the defaults. The last row is the benchmark: one waiting line shared by all four servers, which no balancer can run, because it needs the servers to take work when they are free. It is the good end of the comparison from the lesson on queueing.

  1. Random has a Mean of 103.8 ms. Random placement sends each server a share of the requests that arrives at random too, so each server is a queue of its own with random arrivals, and the lesson on queueing found that such a queue at 80% takes 100 ms. The lab is one run, so it lands a few per cent from the theory.
  2. The benchmark has a Mean of 36.2 ms, close to the 34.9 ms that the lesson on queueing found for four servers sharing a queue at 80%.
  3. Round robin is at 68.3 ms, Least connections at 40.6 ms and Power of two choices at 48.9 ms. Least connections closes most of the gap between 103.8 and 36.2, and two choices nearly as much.
  4. The 99th percentile tells the same story, and more sharply: 433.8 ms for random, 311.8 ms for round robin, 193.8 ms for two choices, 172.0 ms for least connections and 142.4 ms for the benchmark.

Round robin does better than random although it knows no more, because it spaces the requests that each server receives evenly, one in four, where random sometimes sends several in a row to one server. That is reasoning from the result, not a separate measurement.

Now choose an algorithm with Algorithm and look at the table of servers under the summary. The column Waiting in line is the average number of requests in the server’s line, not counting the one being served. With random placement the longest line is on average 4.0 requests, on the second server, while the others have 2.7, 3.6 and 3.4. With two choices the lines are 1.2, 1.2, 1.2 and 1.2. With least connections they are 0.8.

Power of two choices looks at two servers, not at all of them, and yet it is not far behind least connections: 48.9 ms against 40.6 ms, and 193.8 ms against 172.0 ms at the 99th percentile. This is Michael Mitzenmacher’s result. His model has requests arriving at random at n servers, each taking an exponentially distributed time, and a request that chooses d servers at random and joins the line of the one with the fewest. His abstract says that having two choices gives exponential improvements in the expected time in the system over having one, whereas having three choices is only a constant factor better than two. The paper also finds the improvement to be dramatic in small systems at reasonable loads, not only in the limit of many servers.

The same idea is older, and simpler to see when nothing finishes. Place n requests on n servers, each request choosing the less loaded of two servers at random, and compare the busiest server with the one you get from choosing at random. The lab’s own random numbers give, on average over 20 runs:

Servers and requests Busiest server, random Busiest server, two choices
100 4.30 2.85
1,000 5.50 3.00
10,000 6.50 3.15

The busiest server under random placement keeps getting busier as the system grows. Under two choices it hardly moves. A second look costs a second count, and that is nearly all that it takes. Envoy’s documentation points out the practical side: the choice takes constant time, however many servers there are, where looking at every server takes time in proportion to their number.

Nine statements. Which algorithm does each one describe?

Challenge

Which algorithm?

Round robin, least connections and power of two choices each decide with different knowledge. Put each statement under the algorithm it describes.

Sorting needs JavaScript.

Categories

Round robin
The next server in turn, whatever the load.
Least connections
The server holding the fewest requests, out of all of them.
Power of two choices
The less loaded of two servers drawn at random.
  1. Line 1 of 9Each request goes to the next server in a fixed order, whatever the servers are doing.
  2. Line 2 of 9One server takes twice as long as the others, yet it keeps receiving the same share of the requests, and it fails requests the others would have served.
  3. Line 3 of 9The default choice when the requests are alike in how much work they need and the servers are alike in power.
  4. Line 4 of 9The balancer looks at how many requests every server holds, and picks the one that holds the fewest.
  5. Line 5 of 9A slow server holds its requests for longer, so it holds more of them, and this algorithm sends it fewer without ever being told that it is slow.
  6. Line 6 of 9The choice AWS suggests when the requests vary a lot in how much work they need, or the servers differ in power.
  7. Line 7 of 9The balancer draws two servers at random and sends the request to the one that holds fewer requests.
  8. Line 8 of 9It looks at two servers each time, however many servers there are in the pool.
  9. Line 9 of 9Mitzenmacher showed that two random choices give an exponential improvement in the expected time over one, and that a third choice adds only a constant factor.

Check your answers at least once before looking at the answers.

Round robin and random give each server the same share, so they assume that the servers are the same. Set Traffic to 100 and Slow server to 2×, so that the first server takes 40 ms instead of 20 ms. The four servers can now serve 175 requests a second, and 100 is 57% of that, which looks comfortable.

The first server can serve 25 requests a second. Round robin gives it a quarter of the 100, which is 25 a second: exactly what it can serve. Its line grows, 13.2 requests long on average, and it is busy 98% of the time. The table shows what follows. Round robin fails 216 requests, 1.8% of them, and its 99th percentile is 962.3 ms, with the Mean at 144.5 ms. Random is the same, with 207 requests failed, 1.7%. The failed requests are the ones that waited more than a second in the slow server’s line.

Least connections and two choices fail none. The slow server holds its requests for longer, so it holds more of them at any moment, and the algorithms that count requests send it less: 18% of the requests in both, where an even split would send 25%. Their 99th percentiles are 147.6 ms and 194.8 ms. Nobody told them that a server was slow. The count of requests held showed it.

There is a remedy that does not need a count. A balancer can give each server a weight, and nginx’s default weight is 1. With weights of 1, 2, 2 and 2, the slow server gets 1 request in 7 and each other server gets 2 in 7. At 100 requests a second that is 14.3 for the slow server, which can serve 25, and 28.6 for each of the others, which can serve 50: every server is 57% busy. It works, as long as someone sets the weights and keeps them right. A server that slows down for a reason that nobody sees, such as a noisy neighbour, makes the weights wrong, while a count does not need to be told. AWS’s documentation matches the two: round robin is commonly used when requests are similar and targets are similar in power, and least outstanding requests when requests vary in complexity or targets vary in processing capability.

One caution about counts. They measure what the balancer is told to count: connections in nginx and HAProxy, requests in AWS. When one connection carries many requests, as with HTTP/2, a count of connections is a poor measure of load, which is another reason for the layer 7 balancer of the previous lesson.

  • Round robin and random look at nothing. Least connections looks at every server’s count. Power of two choices looks at two servers chosen at random.
  • With four 20 ms servers at 80% utilisation, the mean is 103.8 ms for random, 68.3 ms for round robin, 48.9 ms for two choices, 40.6 ms for least connections and 36.2 ms for one shared queue. Random is four separate queues, as the lesson on queueing found.
  • Two choices comes close to least connections because of Mitzenmacher’s result: two random choices give exponential improvements over one, and a third adds only a constant factor.
  • With one server twice as slow at 100 requests a second, round robin and random fail 216 and 207 requests, and least connections and two choices fail none, because a slow server holds more requests and gets fewer.
  • Weights fix a known difference by hand. Counts adapt to a difference nobody has noticed.

Next: health checks, and how long a balancer takes to notice a dead server.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.