In this lesson · topic 2 of 7
Topic 2
Four ways to pick a server
4 min read
The decision
A balancer makes one decision per request: which server gets it. The strategies differ in how much they look at before deciding. Step through one decision below. The five servers have the queues shown, and the same request is placed by each strategy.
One request, four strategies
Step 1 / 5- queues
- 4 1 3 0 6
A request arrives
Five servers have these queues. The balancer must pick one for the new request, and the request will wait behind whatever is already there.
Round robin
It is the next server in the fixed order. It does not look at the queues at all. This time that order points at the longest queue.
Random
A random server. It also ignores the queues. On another draw it could have picked the empty one.
Power of two choices
Pick two servers at random, compare their queues, take the shorter. Two lookups steer the request away from the worst queues.
Least connections
Look at every server and take the shortest queue. Best placement, but it needs a fresh count from all of them for every request.
- A request arrives. Five servers have these queues. The balancer must pick one for the new request, and the request will wait behind whatever is already there. queues: 4 1 3 0 6.
- Round robin. It is the next server in the fixed order. It does not look at the queues at all. This time that order points at the longest queue. servers inspected: 0; queue ahead of the request: 6.
- Random. A random server. It also ignores the queues. On another draw it could have picked the empty one. servers inspected: 0; queue ahead of the request: 4.
- Power of two choices. Pick two servers at random, compare their queues, take the shorter. Two lookups steer the request away from the worst queues. servers inspected: 2; queue ahead of the request: 1.
- Least connections. Look at every server and take the shortest queue. Best placement, but it needs a fresh count from all of them for every request. servers inspected: 5; queue ahead of the request: 0.
What each strategy needs
Round robin keeps a counter and walks through the servers in order. It needs no information about the servers, and it spreads request counts perfectly evenly. But it counts requests, not work. If requests differ in cost, or servers differ in speed, even counts are not even load.
Random is the same idea with a coin flip instead of a counter. It needs nothing, and it has no memory, so it cannot avoid a server it just overloaded.
Least connections asks every server how many requests it has right now and picks the smallest. It reacts to real load, including slow servers and expensive requests. The price is a fresh count from every server before every decision. With many servers or many balancers, that is a lot of coordination, and the counts go stale the moment they are read.
Power of two choices picks two servers at random and takes the shorter queue. It asks two servers, not all of them. The idea comes from research on balls and bins, where a second random choice turns out to be worth far more than the cost of one extra lookup. The sources listed at the end of this lesson go through the proof.
What the lab adds
In the lab every strategy faces the same arrivals. Each request arrives at the same instant with the same cost no matter which strategy runs, and only the placement differs. That is what makes the comparison fair, and it is why you can read the difference in p99 as the effect of the strategy alone.