System design lab · Episode 1

How a load balancer shares the work

Big websites run many servers. A load balancer stands in front of them and decides which server answers each request. Here it is a small machine: each ball is a request, the sliding shuttle is the load balancer, and the three presses are servers. Follow the lessons under the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
The request being routed nowPresses A, B, C, back to front: three servers. C is four times slowerLanes: each server's queue, with 5 places

Controls

Rule
Servers
Time

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 benchWhat it means in a real system
Hopper (top left)The visitors. It sends a new ball every 1.1 seconds.
BallOne request.
Sliding shuttleThe load balancer. It carries each ball to a lane. The ball it carries right now has a stronger outline.
LaneA server's queue: requests waiting for their turn. The ruler beside it marks the 5 places.
Presses A, B, CThree 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 leverThe 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.
KnobThe 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.