Skip to content

Load Balancer

A component that spreads incoming requests across many servers so no single server is overwhelmed and a failed server does not take the site down.

beginner25 minAsked oftenScalabilityAvailabilityNetwork traffic

Last reviewed 22 Oct 2026 · by TheLearningLoop Team

Progress is saved on this device. Create a free account to keep it everywhere.

On this page ▾

Key takeaways

  • A load balancer spreads requests across servers so no single one is overwhelmed.
  • Round robin and least connections are the two most common routing strategies.
  • Health checks pull a crashed server out of rotation automatically.

Why you need a load balancer

One server can only handle so many requests. When traffic grows, you add more servers, but clients still need one address to talk to. A load balancer sits in front of the servers, accepts every request, and forwards each one to a healthy server.

It gives you two things interviewers always ask about:

  • Scalability: add servers behind it to handle more traffic (horizontal scaling).
  • Availability: if a server dies, health checks notice and traffic goes to the others.

Try it

Send requests and switch between algorithms. Then take a server down and watch the load balancer route around it. Servers have different capacities, and some requests are slower than others, so the algorithm you pick changes how evenly the load spreads.

Load balancer

Step 1 of 55 · Press Space to play, arrow keys to step
fast slow
#1LoadbalancerServer Aspeed 1× · weight 11 activeServer Bspeed 1× · weight 10 activeServer Cspeed 2× · weight 20 active

Request #1 (slow, 14 units) → server A: next in turn (A → B → C).

tick:
0
completed:
0
failed:
0
avg response (ticks):
-
busiest server queue:
1

1 tick = one step of the simulation.

Things to try:

  • Run round robin with mixed request sizes. Notice how one server can pile up slow requests while others sit idle.
  • Switch to least connections and run the same traffic. The queues stay much more even.
  • Mark server B as down mid-run. Its traffic moves to A and C, and no requests are sent to B.

Load balancing algorithms

AlgorithmHow it picks a serverGood whenWeakness
Round robinNext server in the list, in turnServers and requests are similarIgnores current load
Weighted round robinLike round robin, but bigger servers get more turnsServers have different capacityStill ignores current load
Least connectionsServer with the fewest active requestsRequest durations vary a lotNeeds live connection counts
IP hashHash of the client IP picks the serverYou need a client to keep hitting the same serverUneven if a few clients send most traffic
Power of two choices (pick two at random)Pick two at random, send to the less busy oneVery large fleetsSlightly less even than least connections

Layer 4 vs layer 7

  • A layer 4 (L4) load balancer routes by IP address and port only. It is very fast but cannot see the HTTP request.
  • An L7 load balancer reads the HTTP request, so it can route /api to one pool and /images to another, handle HTTPS encryption so the servers don't have to (called TLS termination), and add headers.

How load balancer health checks work

The load balancer calls a small status URL, such as GET /healthz, on each server every few seconds. After a few failures in a row, the server is taken out of rotation. After a few successes, it comes back. This is what makes the system self-healing.

Avoiding a new single point of failure

A single load balancer is itself a single point of failure. Real setups run two or more load balancers, for example an active-passive pair (one handles traffic, and the other takes over its IP address if it fails, called a floating IP), or DNS pointing at several of them.

How to talk about load balancers in an interview

  • Draw it early: clients, then the load balancer, then several stateless app servers.
  • Say which algorithm you would use and why. Least connections is a safe default when request times vary.
  • Mention health checks, keeping servers stateless (store login sessions in a shared store such as Redis, not in one server's memory, so any server can handle any request), and running the load balancer redundantly.

Where you meet load balancers in real systems

Every large website

When you open a streaming app or an online store, your request almost certainly hits a load balancer first. Cloud providers sell them as managed services, for example AWS Elastic Load Balancing, Google Cloud Load Balancing and Azure Load Balancer. Open source options like NGINX, HAProxy and Envoy do the same job on your own servers.

Kubernetes

A Kubernetes Service spreads traffic across the pods behind it, and an Ingress controller acts as an L7 load balancer that routes by host name and path.

Zero-downtime deploys

During a deploy, servers are taken out of the load balancer one at a time, upgraded, health-checked and put back. Users never notice because the remaining servers keep serving traffic.

Global traffic

Large companies balance at several levels. DNS sends you to the nearest region, a regional load balancer picks a cluster, and a local one picks a server. This is why a whole data centre can fail and the site stays up.

Database read replicas

Apps often send writes to a primary database and spread reads across several replicas, which is load balancing applied to databases.

The takeaway

Whenever a design needs to scale out or survive a server failure, a load balancer is usually the first box you draw.

Interview questions

Try answering out loud before you reveal hints. Interviewers care about how you reason, not just the final answer.

  1. 1.

    Your API has some requests that take 10 ms and some that take 5 seconds. Which load balancing algorithm would you pick?

    easy
  2. 2.

    What are sticky sessions, and why do many teams avoid them?

    medium
  3. 3.

    What is the difference between an L4 and an L7 load balancer? When would you use each?

    medium
  4. 4.

    Isn't the load balancer itself a single point of failure? How do you fix that?

    medium
  5. 5.

    How should health checks be designed so they do not cause outages?

    hard

↑ Back to top

Next lesson