Round robin, least connections, hashing: how a load balancer picks a server.
A load balancing algorithm is the rule a load balancer uses to pick a server for each request.
The load balancer has a list of healthy servers. For every request it must answer one question: “Which server gets this?” The algorithm is the answer.
The right algorithm decides if the load is shared fairly. A wrong one can overload one server while others sit idle.
Go through the servers in a circle: A, B, C, A, B, C... Simple and fair if servers and requests are about equal.
Give each server a weight (its power). A server with weight 3 gets 3 turns for every 1 turn of a weight-1 server.
Send the request to the server with the fewest open connections right now. A busy or slow server has more open connections, so it gets fewer new ones. The weighted version divides open connections by the weight.
Pick the server that answers fastest (and has few connections). It needs the LB to measure the response time of each server.
Take the client IP (or a header, or the URL), run it through a hash function to get a number, and use number % server_count to pick a server. The same client gives the same number, so it always lands on the same server.
% server_count changes and almost every client moves to a different server. For caches, this is a disaster. The fix is Consistent Hashing.Pick a server at random. It is simple and works fine at large scale. A clever version, “power of two choices”, picks two random servers and sends the request to the less busy one. This gives a very good spread with little work.
| Algorithm | Looks at | Best for |
|---|---|---|
| Round robin | Nothing | Equal servers, short equal requests |
| Weighted round robin | Server weight | Different server sizes, canary release |
| Least connections | Open connections | Long or uneven requests |
| Least response time | Speed and connections | Latency sensitive apps |
| IP / URL hash | Client or URL | Sticky users, caches |
| Consistent hash | Client or key, on a ring | Caches and sharded data with changing servers |
| Random / 2 choices | Nothing / 2 loads | Very large, simple fleets |
# round robin
i = 0
def pick(servers):
global i
s = servers[i % len(servers)]
i += 1
return s
# least connections
def pick(servers):
return min(servers, key=lambda s: s.open_connections)| Term | Meaning in easy words |
|---|---|
| Algorithm | The rule that picks a server for a request |
| Weight | A number showing how strong a server is. Higher weight, more traffic |
| Open connection | A request that the server is still working on |
| Static vs dynamic | Static ignores live load. Dynamic looks at it |
| Hash function | Turns text (like an IP) into a number |
| Sticky / affinity | The same client keeps going to the same server |
| Hot spot | One server that gets far more load than the others |
| Slow start | A new server gets traffic slowly, not all at once |
| Canary release | Send a small share of traffic to a new version to test it |
| Power of two choices | Pick two random servers, send to the less busy one |
| Choice | We gain | We lose |
|---|---|---|
| Round robin | Simplicity, no state | No awareness of load |
| Least connections | Better balance | LB must track connections |
| IP hash | Same client, same server | Uneven load, remap on change |
| Weighted | Uses big servers well | You must set and update weights |