Load Balancing

Spreading requests across several servers so no single one gets overwhelmed.

What is it?

One server can only handle so many requests at once. Past that, you need more servers, but then which server should each request go to?

A load balancer sits in front of a group of servers and picks one for every incoming request. It spreads the work so no server is overloaded while others sit idle.

Explain like I'm 10

A load balancer is like the host at a busy restaurant who seats guests across all the tables, instead of giving one table 20 parties while the rest sit empty.

Examples

Conceptual round-robin balancing

const servers = ["server-1", "server-2", "server-3"];
let next = 0;

function pickServer() {
  const server = servers[next];
  next = (next + 1) % servers.length; // cycle back to 0 after the last one
  return server;
}
// Requests get spread evenly: server-1, server-2, server-3, server-1, ...

A simplified 'round robin', one of several strategies real load balancers use.

How it works

Every request reaches the load balancer first. It picks a healthy server and forwards the request there, using a strategy such as:

  • Round robin: cycle through servers in a fixed order, however busy each one is.
  • Least-connections: send each new request to the server with the fewest requests still in progress.
  • Server load.

It also keeps checking server health and stops sending traffic to any server that's down.

┌─────────────┐
Requests →│Load Balancer│
          └──────┬──────┘
        ┌────────┼────────┐
        ▼        ▼        ▼
    Server 1  Server 2  Server 3

Why does it exist?

Without it, scaling means one huge server: expensive, and still a single point of failure. Load balancing lets you add ordinary servers as traffic grows and keeps the system running if one server fails.

When to use it

Use one as soon as a single server can't handle your traffic reliably, or when the app must survive one server failing.

When not to use it

A small, low-traffic app on one server doesn't need one yet. It adds infrastructure and cost for a problem you don't have. Add it when traffic or reliability needs demand it, not for imagined scale.

Common mistakes

  • Treating the load balancer as unbreakable. It needs redundancy too, or it becomes a single point of failure.

  • Ignoring 'sticky sessions' (routing a user's later requests back to the same server) when that user's data lives only on that server.

  • Assuming load balancing alone solves scaling. The database or other shared resources behind it can still be the bottleneck.

Practice exercises

  1. Easy:

    Explain in your own words why a single server isn't enough for a popular application.

  2. Medium:

    Describe the difference between 'round robin' and 'least connections' as load balancing strategies.

  3. Hard:

    Explain what could go wrong if a load balancer keeps sending traffic to a crashed server, and how health checks fix it.

Interview questions

What problem does a load balancer solve?

It spreads requests across several servers so none is overwhelmed, and traffic keeps flowing if one fails.

What is a health check in load balancing?

A regular check the load balancer runs on each server to confirm it still responds, so it can stop routing to unhealthy ones.

Can a load balancer itself be a single point of failure?

Yes. That's why production systems often run several load balancers with their own failover.