Taking turns: round robin
After this: Say which server gets each request under round robin, write the rule in code, and compare it with least connections.
On this page · 4 sections · 3 checks
Three cashiers in a shop, and one person at the door who sends each customer to a till. The fairest plan anyone can think of: first till, second till, third till, then back to the first. The load balancer from lesson 1 can do the same with servers.
Taking turns
The load balancer keeps the servers in a fixed order, A, B, C, and remembers which one it used last. Each new request goes to the next server in that order, and after the last server it starts again at the first. This rule is called .
Follow seven requests over A, B and C:
- Requests 1, 2 and 3 go to A, B and C.
- C was the last in the order, so request 4 starts again at A.
- Requests 5 and 6 go to B and C, and request 7 goes to A once more.
The route is A B C A B C A. Server A gets 3 requests, B gets 2 and C gets 2. Seven is not a multiple of three, so the first server gets the extra one. Over many requests, each server gets about a third.
- Round robin: 7 requestsRun it and step through. Before each step, read the note on the load balancer: it names the server that gets the next request. Count how many each server gets.
Press Run it, then step through and watch the requests move.
The rule in code
Number the requests from 0. Request i goes to the server at position i % 3, the remainder when i is divided by 3:
request i | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
i % 3 | 0 | 1 | 2 | 0 | 1 | 2 | 0 |
| server | A | B | C | A | B | C | A |
The remainder counts 0, 1, 2 and then starts again at 0, which is the turn-taking rule. In code, the 3 is the length of the list of servers, so the same line works for any number of them.
Python runs in the browser when you press Run on the block below. Java does not run here: when Java is selected, the block shows the same program in Java for reading, and you can paste it into a Java tool on your computer to run it.
The list holds the servers in their order. The loop runs once per request, with i from 0 to 6. turn is the remainder, a position in the list, and the print shows the server at that position. Change the 7 to 9 and run it again: A, B and C now get three each.
Looking at how busy a server is
Round robin never looks at the servers. That is fine when every request takes about the same time. But some requests are slow, such as a search over the whole shop, while others are quick, such as the home page. A server can end up with several slow requests while the next one in the order sits idle.
A second rule fixes this. The load balancer counts the requests each server is still working on, and sends the new one to the server with the fewest. This rule is called .
Say A is working on 5 requests, B on 2 and C on 3. Under least connections the next request goes to B. Under round robin it goes to whoever is next in the order, however busy that server is.
Check yourself
Three servers, A, B and C, take turns in that order. The last request went to B. Which servers get the next five requests, in order?
Four servers now, and six requests. What does this print? (One letter per line; read the lines left to right.)
servers = ["A", "B", "C", "D"]for i in range(6): turn = i % len(servers) print(servers[turn])public class Main { public static void main(String[] args) { String[] servers = {"A", "B", "C", "D"}; for (int i = 0; i < 6; i++) { int turn = i % servers.length; System.out.println(servers[turn]); } }}Server A is working on 4 requests, B on 6 and C on 1. The last request went to A. Which server gets the next request under round robin, and which under least connections?
- Follow round robin: each request goes to the next server in a fixed order, starting again at the first after the last.
- Say how seven requests share three servers: A B C A B C A, so A gets 3 and B and C get 2 each.
- Write the rule in code: request i goes to the server at position i % (number of servers).
- Say what least connections does: it sends each request to the server working on the fewest requests, which suits a mix of slow and quick requests.