Module 4 / When one server isn't enough / Load balancing

Lesson9 minModule 4 · lesson 2 of 9

Load balancing

Sharing requests fairly across servers, and how the choice of algorithm changes the result.

One server can only handle so many requests. When traffic grows, you run several copies of the server and put a load balancer in front. It receives every request and hands each to one of the servers. To the client it looks like one address; behind it the work is shared.

ClientsLoad balancerServer 1Server 2Server 3
A load balancer spreads requests over identical servers.

Common ways to pick a server

Step 1 of 3

Round robin

Take turns. Simple and fair in request count when requests cost about the same. It is blind to how busy each server is.

Fair in count is not the same as fair in work. In a delivery app, loading a restaurant list is quick, but generating an order-history export may take ten times longer. If requests alternate heavy and light and you have two servers, round robin sends every heavy request to server 1 and every light one to server 2: equal counts, very unequal load. Try it below.

Round robin versus least connections

Every other request is heavy (cost 10) and the rest are light (cost 1), across 2 servers. Run it with round robin, then switch to least connections. Compare the peak number of requests in flight.

Algorithm
  1. 10 cost, client u0
  2. 1 cost, client u1
  3. 10 cost, client u2
  4. 1 cost, client u3
  5. 10 cost, client u4
  6. 1 cost, client u5
  7. 10 cost, client u6
  8. 1 cost, client u7
  9. 10 cost, client u8
  10. 1 cost, client u9
  11. 10 cost, client u10
  12. 1 cost, client u11
Server 1
0 req · 0 busy
Server 2
0 req · 0 busy
0 of 12 requests routed

With round robin one server piles up five long requests at once (peak 5), while with least connections the peak is 3 because the balancer notices the busy server and sends work elsewhere. Request counts are 6 and 6 either way, which is why counting requests alone can mislead you.

Question 1

Three servers each handle 10k requests/s. How much traffic can the group take if traffic splits evenly?

Question 2

Requests vary a lot in cost. Which algorithm usually spreads the actual work best?

What to remember

  1. A load balancer presents one address and shares requests over identical servers.

  2. Round robin balances counts; least connections balances current work; hashing keeps a client on one server.

  3. Equal request counts can still mean unequal load when requests differ in cost.

  4. Make the balancer itself redundant.