Stage 2 · Building blocks · Easy · 19 min
Load balancing and health checks
A load balancer spreads requests across servers and skips the ones that fail. Compare round robin, least connections and IP hash, and layer 4 with layer 7.
What each interview level is expected to know
Learn first: Vertical and horizontal scaling, Latency, throughput and availability
In AI systems: Serving a model: batches, memory and queues
After this lesson you can:
- Compare round robin, least connections and IP hash on uneven requests.
- Explain how health checks take a failing server out, and how slowly they can react.
- Say when to balance at layer 4 and when at layer 7.
Start here
See it in 60 seconds
How a load balancer picks a server
A load balancer sends each request to one of several servers, and its rule decides which server gets the next one.
Scene 1 of 6 · One front door
Every request reaches the load balancer first, and it picks one of three servers to answer.
- Users — healthy
- Load balancer — healthy
- Server A — healthy
- Server B — healthy
- Server C — healthy
- One front door. Every request reaches the load balancer first, and it picks one of three servers to answer.
- Round robin takes turns. Round robin takes turns: server A, then B, then C, then A again.
- B slows down. Server B slows down, so its work piles up: 4 requests in progress, against 1 on A and C.
- Turns ignore the pile. Round robin still gives server B its turn, so the slow server gets even more work.
- Least connections looks. Least connections sends the next request to the server with the fewest in progress: A.
- Health checks remove it. If B stops answering health checks, the load balancer stops sending it anything.
- Now you try. Server B is slow with 4 requests in progress, and it is B's turn. Pick a rule. For example: Round robin sends the next request to server B. It takes turns without looking at how busy each server is, so slow B still gets its share.
Scene 1 of 6: One front door. Every request reaches the load balancer first, and it picks one of three servers to answer.
Key points
- Every request meets the load balancer first; users never pick a server themselves.
- Round robin takes turns without looking, so a slow server keeps getting its share.
- Least connections sends each request to the server with the least work in progress.
- Health checks let the load balancer stop sending requests to a server that stopped answering.
In plain words
Say a site runs on three servers, A, B and C, and 300 requests arrive each second. A (A server that receives every request first and passes each one to one of several servers behind it.) is the part in front that receives every request and passes it to one of the three. Spread evenly, each server gets about 100 a second. If server B stops, the load balancer notices and shares the traffic between A and C.
The load balancer needs a rule for picking a server. (A way to spread work evenly: give each server a turn in a fixed order, one request at a time.) gives each server a turn in a fixed order. (A way to route requests to whichever server currently has the fewest requests in progress.) picks the server with the fewest requests in progress, which helps when some requests take much longer than others. It also sends each server a (A test that a load balancer or a service registry sends a server on a schedule, to find out if it is still working.) every few seconds, and stops using one that fails a few in a row.
The problem it solves
Clients know one address, but many servers do the work, and they start, stop and fail. Something has to share the requests out, so that no server is overloaded while others sit idle. It also has to stop sending requests to a server that has died.
Why it matters
Once you run more than one server, something must decide which server gets each request. The same part can notice a dead server and stop sending it traffic. So a load balancer gives you more capacity and a way to survive a failed machine.
A worked example
A single server that answers one request every 20 ms can handle about 50 requests per second, since 1,000 ÷ 20 = 50. Four identical servers behind a load balancer raise total capacity to about 4 × 50 = 200 requests per second. No single server gets any faster.
Think of it like…
Think of a barber shop with several barbers. A manager at the door sends each new customer to the barber who will be free soonest.
Where this analogy stops working
One door and several workers is the useful part. But a barber cuts one head of hair at a time. A server works on hundreds of requests at once, so a load balancer counts requests in progress. It also tests each server, and stops sending work to one that fails.
Try it
See it working
Also try: Consistent hashing
Mechanics
How it works
A load balancer has two jobs. It picks a server for each request, and it stops sending requests to a server that has died. Both jobs are shown below on one small site with three servers: A, B and C.
Clients connect to one address
The site's (The Domain Name System: the way the internet turns a name, such as example.com, into the address of a server.) name points to the load balancer, not to any one server. That shared address is often called a virtual IP (VIP). Clients do not need to know the servers' own addresses.
Because clients never see the servers' own addresses, you can add, remove or replace a server without telling any client. Only the load balancer's list changes.
Health checks
Clients, X, Y and Z Clients X, Y and Z Load balancer, one address Load balancer one address sends to A, B, C Server A Server A Server B, FAILED, NOW Server B crashed FAILED Server C Server C - Clients — X, Y and Z — healthy
- Load balancer — one address — healthy — sends to A, B, C
- Server A — healthy
- Server B — FAILED — NOW — crashed
- Server C — healthy
- Request with no answer
- Health check
- Request
Step 1 of 6Step 1 of 6 · Server B
Failed: Server B crashes
- What happens
- At second 0, server B stops answering. The load balancer does not know yet.
- Why
- Nothing tells the load balancer that a server has died. It has to find out for itself.
- What changes
- B is still in the list of servers that get traffic.
- Next
- A request goes to B and gets no answer
Clock0sServers in the list3A, B and CFailed checks for B0 of 3still in the listStep 1 of 6: Server B. Server B crashes. At second 0, server B stops answering. The load balancer does not know yet.
Every 5 seconds, the load balancer calls a small test URL on each server, such as /healthz. In the picture, server B crashes. Step through to see how long B keeps getting traffic.
Nothing tells the load balancer that a server has died. It finds out only when checks fail. This one waits for 3 in a row, so one slow answer does not remove a healthy server.
The cost of that care is time. With a check every 5 seconds and 3 in a row, B can stay in the list for about 15 seconds after it dies. Until then it still gets its turn, and those requests fail.
Shorter gaps or fewer failures find a dead server sooner. But they also remove servers that were only slow for a moment. Many load balancers can also retry a failed request on another server, so users see fewer errors while the checks catch up.
An algorithm picks a server
Pick a case to follow belowWith this case: Round robin put both slow reports on A, so A had 2 requests in progress at once. Turns are even, but the work is not.
Clients, X, Y and Z Clients X, Y and Z Load balancer, one address Load balancer one address Server A, NOW Server A 1 in progress NOW Server B Server B 0 in progress Server C Server C 0 in progress - Clients — X, Y and Z — healthy
- Load balancer — one address — healthy
- Server A — healthy — NOW — 1 in progress
- Server B — healthy — 0 in progress
- Server C — healthy — 0 in progress
- Request
Step 1 of 6Step 1 of 6 · Server A
Done: Request 1, from client X, goes to A
- What happens
- At second 0, a slow report that takes 5 seconds arrives. It is A's turn.
- Why
- Round robin gives each server a turn in order, A, B, C, then A again. It does not look at how busy a server is.
- What changes
- A now has 1 request in progress.
- Next
- Request 2, from client Y, goes to B
Server A1in progress1 sent so farServer B0in progress0 sent so farServer C0in progress0 sent so farMost at once on one server1on AStep 1 of 6: Server A. Request 1, from client X, goes to A. At second 0, a slow report that takes 5 seconds arrives. It is A's turn.
What these pictures simplify. These pictures use three servers, one request a second and set times: 5 seconds for a report, half a second for a page. Real traffic is far larger and more mixed. The IP hash table is a stated example, not a real hash. The Load balancer lab above runs many requests at once.
The rule that picks a server matters most when requests take different times. In the picture, six requests arrive one a second, and two of them are slow reports that take 5 seconds. Switch between the three rules.
Round robin gives each server a turn in order. The turns are fair, but the work is not. Here both slow reports land on A, so A has 2 requests in progress while C has none. Weighted round robin is the same idea, with more turns for a bigger server.
Least connections looks at how many requests each server is working on, and picks the one with the fewest. Each slow report goes to a free server, so no server has more than 1 in progress. This helps whenever some requests are much slower than others.
(A way to route requests: hash the client's IP address to pick a server, so the same client keeps landing on the same one.) picks the server from the client's address. While the list of servers stays the same, a client always lands on the same server. That is useful when a server keeps something for that client, such as a login (A record the server keeps for a logged-in user. The browser holds only its id, and sends it with every request.) in memory. The third case shows the risk: client X sends most of the traffic, so server A does most of the work and C gets none.
The response returns
Usually the response goes back through the load balancer. For heavy downloads such as video, the server can reply to the client directly. That is called direct server return (DSR). It stops the load balancer from limiting outgoing traffic.
Layer 4 or layer 7
A layer 4 load balancer works on connections. It forwards the bytes of a (The rules most internet traffic uses to deliver data in order, sending again any piece that gets lost.) connection without reading them. A layer 7 load balancer reads each (The rules an app or browser uses to ask a server for something and get an answer, such as GET /profile/42 and then 200 OK.) request, so it can send /images to one group of servers and /api to another. The numbers come from a standard model of network layers: TCP is at layer 4 and HTTP is at layer 7.
A layer 4 load balancer does less work per request, so one machine can pass more traffic. A layer 7 load balancer can route by path, add headers and end (Transport Layer Security: the encryption behind the S in HTTPS. It hides the data on its way and lets the browser check it reached the real server.), the encryption behind (HTTP with every message scrambled by a secret key, after the server proves who it is with a certificate a trusted authority signed.). Large sites often use both: layer 4 at the edge, then layer 7 in front of the services.
| Rule | How it picks | Works well when | Weak spot |
|---|---|---|---|
| Round robin | Each server in turn | Requests take about the same time | A slow request does not change whose turn is next |
| Weighted round robin | More turns for bigger servers | Servers differ in size | Weights are set by hand and can go out of date |
| Least connections | The server with the fewest requests in progress | Some requests are much slower than others | Needs a live count for every server |
| IP hash | A hash of the client's address | A server keeps state for each client | One heavy client overloads one server |
Use it when
- Any service that runs on more than one server: something must share the requests between them.
- Handling HTTPS in one place: the load balancer decrypts each request, so the servers behind it do not have to. This is called TLS termination, after TLS, the encryption that HTTPS uses.
- Releasing a new version with less risk: send it a small share of traffic first (a (A release that gives the new version to a small share of the traffic first, such as 10%. If it fails there, few users are hurt and it is pulled back.)), or switch all traffic at once ( (A release that starts a second full set of servers on the new version, then switches all traffic to it in one step. The old set stays ready for a fast switch back.)).
Skip it when
- One server that handles the load, where a short outage is acceptable: a load balancer adds cost and one more part that can fail.
- A slow database: more app servers behind a load balancer do not help when every request waits on the same database.
Check yourself
Judgement
Trade-offs and how it fails
What you give up
- Performance
- A layer 4 load balancer does little work for each connection, so it handles very high traffic. A layer 7 one reads every request, which costs more CPU but lets it route by path, header or (A small value that a site asks the browser to keep, and to send back with every request to that site.).
- Complexity
- The load balancer is itself a single point of failure: if it stops, no request reaches any server. So it runs as a pair, with a standby that takes over the shared address. Managed cloud load balancers do this for you.
Failure modes
- A health check that tests too much. If /healthz also queries the database, one slow moment in the database fails the check on every server at once. The load balancer then has no server to send to.
- Uneven load from sticky sessions: a few very busy clients stay pinned to one server, which overloads while the others have room.
- Slow detection: requests keep going to a dead server until enough checks fail. With a check every 2 seconds and 2 failures needed, some requests fail for up to about 4 seconds.
In the real world
- Netflix built and open-sourced Zuul, the gateway that routes requests into its services.
- Google's Maglev paper (2016) describes software load balancers that pick a server for each connection with consistent hashing. Source: Google: the Maglev paper (2016) (checked 14 October 2026)
Trade-offs to be ready to discuss: L4 vs l7 load balancing
Check yourself
Interview
What interviewers look for
- Say which layer you need and why: "Layer 7, because I route /api and /images to different servers." Pick layer 4 for traffic that is not HTTP.
- Say what happens when a server dies: how the health check finds it, and how long that takes.
- Point out that the load balancer needs a standby too, or it becomes the single point of failure.
Say it out loud: What is direct server return (DSR) in a layer 4 load balancer?
Expected depth by interview level
Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.
- JuniorCore
- Put a load balancer in front of several servers.
- MidCore
- Choose an algorithm, set health checks, and pick layer 4 or layer 7.
- SeniorExpected
- What happens when a server is slow or dead, and what sticky sessions cost.
- StaffUseful
- Global traffic management across regions.
Practice
Apply it
Three harder questions
The first two use the idea on a new case. The last asks you to judge a design.
Apply it
Apply it
Judge a design
Go deeper
Sources and further reading
First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Engineering guideLoad Balancing in the DatacenterGoogle SRE bookWhy round robin falls short, and how Google weights servers by how busy they are.
- Official docsLoad balancing overviewEnvoy proxy documentationReal algorithms (least request, ring hash, Maglev) and how health checks remove bad hosts.
- Official docsUsing nginx as HTTP load balancernginx documentationA short working setup for round robin, least connections and IP hash.