Card 03/ 10

ExampleDifficulty: Beginner2 min

Load Balancing Example: Six Requests, Three Algorithms

Round robin, least connections and IP hash send the same six requests to different servers.

Running the same six requests through three load balancing algorithms shows what each one looks at, and when that matters. Round robin looks at nothing but its own turn; least connections looks at how busy each server is; IP hash looks at who sent the request.

The input. Three servers, A, B and C. One request arrives each second. The first, r1, is a slow report that holds its server for nine seconds; the others each take two. Least connections breaks a tie by picking the first server alphabetically. IP hash takes a checksum (CRC32) of the client's address and divides by three.

Where each algorithm sends each request, from a simulation run with python:3.12-slim (Python 3.12.13)
RequestClientRound robinOpen on A, B, CLeast connectionsIP hash
r1 (9 s)10.0.0.5A0, 0, 0AC
r210.0.0.8B1, 0, 0BC
r310.0.0.5C1, 1, 0CC
r410.0.0.13A1, 0, 1BA
r510.0.0.8B1, 1, 0CC
r610.0.0.5C1, 0, 1BC

What to notice. Round robin sends r4 to A, which is still busy with the nine-second report, while B sits idle. Least connections sees A's open request and sends r4 to B instead. IP hash sends every request from 10.0.0.5 to C, whatever C is doing, and in this run puts five of six requests on one server.

Which workload each suits. Round robin is fine when requests take about the same time, which is common for short API calls. Least connections suits requests of very different lengths, such as reports beside page views. IP hash suits a client that must keep reaching the same server, for example one holding a cache warm for that user, at the cost of uneven load.

So choose the algorithm by what varies in your traffic: equal requests need no more than round robin, and uneven ones need a balancer that counts.