Want the details?
What is a load balancer?
A popular website has many visitors at the same moment. Each visitor's browser sends requests (“give me this page”). One computer, a server, can answer only so many requests per second. So big sites run several servers that all do the same job.
Something must decide which server answers each request. That is the load balancer. It sits in front of the servers. Every request reaches it first, and it passes the request to one server. Its goal is to share the work, so that no server is buried while another one sits idle.
How to read the bench
| Part on the bench | What it means in a real system |
|---|---|
| Hopper (top left) | The visitors. It sends a new ball every 1.1 seconds. |
| Ball | One request. |
| Sliding shuttle | The load balancer. It carries each ball to a lane. The ball it carries right now has a stronger outline. |
| Lane | A server's queue: requests waiting for their turn. The ruler beside it marks the 5 places. |
| Presses A, B, C | Three servers. A press works on one ball at a time, and its head comes down while it works. A and B need 2 seconds per ball. C needs 8 seconds: think of an old, slow machine. The end of each press arm has one, two or three dots, for A, B and C. |
| Lamp and lever | The server's health. Lamp on means the press is “in rotation”: it can get new balls. |
| Reject lane (at the back) | Lost requests. A ball goes here when its lane is already full. In real life the visitor gets an error such as “503 Service Unavailable”, or waits until the request times out. |
| Knob | The rule the load balancer uses to choose. |
| Meters (under the picture) | How many balls each press has, waiting plus being pressed, out of 6 places. The big number counts the balls dropped since the rule last changed. |
The two rules
Round robin
Take turns: A, then B, then C, then A again. It does not look at how busy a server is. It is simple, and it is fair when all the servers are the same.
Least connections
Before it sends a ball, it counts the balls each press already has. It sends the ball to the press with the fewest. When two presses are equal, it takes turns between them. It needs a little more bookkeeping, but it adapts to slow servers.
The numbers
- Balls arrive one every 1.1 seconds: 0.91 per second.
- Presses A and B take 2 seconds per ball: 0.5 per second each. Press C takes 8 seconds: 0.125 per second.
- All three together finish 1.125 per second. So the bench is about 81% busy. There is enough capacity, if the work is shared well.
- Each lane has room for 5 waiting balls.
Every number in the readout comes from the same small simulation that moves the balls. What you see and what you read always agree.
In real systems
- Common load balancers: Nginx, HAProxy, Envoy, and the load balancers of cloud providers (AWS Elastic Load Balancing, Google Cloud Load Balancing, Cloudflare). They all offer both rules on this page, and more.
- Health checks. The load balancer asks every server, every few seconds: “are you OK?” A server that does not answer is taken out of rotation, like the lever on a press. When it recovers, it goes back in.
- Other rules. Weighted round robin gives stronger servers more turns. Here, weights A=4, B=4, C=1 would also stop the drops. Least response time picks the server that has been answering fastest. IP hash always sends the same visitor to the same server. That helps when a server keeps the visitor's session in its memory.
- What the bench makes simpler. Real requests are not all the same size. A real load balancer counts open connections, not the real work inside each one. Real traffic comes in waves, not exactly every 1.1 seconds. The ideas stay the same.
Words to know
- Request
- One message from a visitor that asks for something.
- Server
- A computer that answers requests.
- Queue
- Requests that wait for a server.
- Throughput
- How many requests the system finishes per second.
- Capacity
- The most a server, or the whole system, can finish per second.
- In rotation
- A server the load balancer is allowed to send work to.