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

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.

UsersUsersLoad balancerLoad balancerServer AServer AServer BServer BServer CServer C
  • Users — healthy
  • Load balancer — healthy
  • Server A — healthy
  • Server B — healthy
  • Server C — healthy
  1. One front door. Every request reaches the load balancer first, and it picks one of three servers to answer.
  2. Round robin takes turns. Round robin takes turns: server A, then B, then C, then A again.
  3. B slows down. Server B slows down, so its work piles up: 4 requests in progress, against 1 on A and C.
  4. Turns ignore the pile. Round robin still gives server B its turn, so the slow server gets even more work.
  5. Least connections looks. Least connections sends the next request to the server with the fewest in progress: A.
  6. Health checks remove it. If B stops answering health checks, the load balancer stops sending it anything.
  7. 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

  1. Every request meets the load balancer first; users never pick a server themselves.
  2. Round robin takes turns without looking, so a slow server keeps getting its share.
  3. Least connections sends each request to the server with the least work in progress.
  4. Health checks let the load balancer stop sending requests to a server that stopped answering.

Go deeper in the Load balancer lab →

In plain words

Say a site runs on three servers, A, B and C, and 300 requests arrive each second. A 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. gives each server a turn in a fixed order. 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 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

Live simulationOpen the full lab →

Lab · Traffic

Load balancer

A load balancer sits in front of several identical servers and decides which one handles each request. Watch how the algorithm, a slow server and a crash change where requests go — and who gets errors.

Try it

Lab time 3.0 s
Clients, 6 IP addressesClients6 IP addresses6 req/sLoad balancer, Round robinLoad balancerRound robinchecks every 2 sServer A, in poolServer Ain pool2 busy · 8 sentServer B, in poolServer Bin pool2 busy · 8 sentServer C, in poolServer Cin pool3 busy · 8 sent
About each part:
  • Clients — 6 IP addresses — healthy — 6 req/s
  • Load balancer — Round robin — healthy — checks every 2 s
  • Server A — in pool — healthy — 2 busy · 8 sent
  • Server B — in pool — healthy — 2 busy · 8 sent
  • Server C — in pool — healthy — 3 busy · 8 sent
  • Request
  • Response
  • Error / rejected
  • Health check
Requests
25
Succeeded
17
Failed
0
In flight
8
latency
1.3 s
latency
1.4 s

Numbers add up:25 requests = 17 succeeded + 0 failed + 8 in flightThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Algorithm

Next server in order, one at a time.

Traffic
6 req/s
Failures
Server A
Server B
Server C

Tip: you can also click a server in the diagram to crash or recover it.

Release v2

Now: every server runs v1. v2 requests so far: 0, failed: 0, rollbacks: 0.

v2 has a bug: it fails about 1 request in 5.

Where requests went

  • Server A33%
  • Server B33%
  • Server C33%

The tick mark shows an equal 33% share.

What just happened

  1. 0.0sInfo: Load balancer started with 3 servers using Round robin.
What this model simplifies
  • Time is slowed down so you can follow each request: a network hop takes ~0.2 s here instead of well under a millisecond.
  • Servers get slower as they get busier (+12% per concurrent request) and reject work beyond 10 concurrent requests. Real servers degrade in less tidy ways.
  • The load balancer itself never fails. In production you run at least two, behind DNS or a floating IP.
  • Health checks are simple "are you alive?" probes. Real checks often hit an endpoint that also verifies dependencies.

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.

  1. Clients connect to one address

    The site's 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.

  2. Health checks

    Clients, X, Y and ZClientsX, Y and ZLoad balancer, one addressLoad balancerone addresssends to A, B, CServer AServer AServer B, FAILED, NOWServer BcrashedFAILEDServer CServer 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 6

    Step 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
    Clock
    0s
    Servers in the list
    3
    A, B and C
    Failed checks for B
    0 of 3
    still in the list

    Step 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.

  3. An algorithm picks a server

    Pick a case to follow below

    With 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 ZClientsX, Y and ZLoad balancer, one addressLoad balancerone addressServer A, NOWServer A1 in progressNOWServer BServer B0 in progressServer CServer C0 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 6

    Step 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 A
    1in progress
    1 sent so far
    Server B
    0in progress
    0 sent so far
    Server C
    0in progress
    0 sent so far
    Most at once on one server
    1
    on A

    Step 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.

    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 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.

  4. 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.

  5. Layer 4 or layer 7

    A layer 4 load balancer works on connections. It forwards the bytes of a connection without reading them. A layer 7 load balancer reads each 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 , the encryption behind . Large sites often use both: layer 4 at the edge, then layer 7 in front of the services.

Ways to pick a server
RuleHow it picksWorks well whenWeak spot
Round robinEach server in turnRequests take about the same timeA slow request does not change whose turn is next
Weighted round robinMore turns for bigger serversServers differ in sizeWeights are set by hand and can go out of date
Least connectionsThe server with the fewest requests in progressSome requests are much slower than othersNeeds a live count for every server
IP hashA hash of the client's addressA server keeps state for each clientOne 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 ), or switch all traffic at once ().

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

How does a load balancer usually find out that a server has stopped working?

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 .
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

Why pick a server by a hash of the user id?

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?

Most requests take 5 ms, but some take 2 seconds. Which way of picking a server keeps the load most even?

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

A load balancer checks each server every 5 seconds and removes one after 3 failed checks in a row. Server B crashes right after passing a check. About how long does B keep getting traffic?

Apply it

You want requests for /images to go to one group of servers and /api to another. Which load balancer can do this?

Judge a design

To test more, a team makes /healthz on every server run a query on the shared database. What is the risk?

Next: service discovery. A load balancer needs a list of servers, so learn where that list comes from and how it stays fresh.

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.