System DesignBeginner7 min readPart 2 of 14

Load Balancing

Load balancing spreads incoming traffic across many servers. Without it, one server handles every request and becomes both a bottleneck and a single point of failure. Almost every system design starts with a load balancer in front of the application servers.

Can you explain load balancing out loud?

I'm Alex. Read this, then I'll interview you on a design problem and push on the parts you skip.

Why load balancing mattersCore idea

%%{init: {"look":"handDrawn","handDrawnSeed":7,"theme":"base","fontFamily":"Virgil, \"Segoe Print\", \"Comic Sans MS\", cursive","themeVariables":{"fontFamily":"Virgil, \"Segoe Print\", \"Comic Sans MS\", cursive","fontSize":"16px","primaryColor":"#fff4e6","primaryBorderColor":"#1e1e1e","primaryTextColor":"#1e1e1e","secondaryColor":"#e7f5ff","tertiaryColor":"#ebfbee","lineColor":"#1e1e1e","textColor":"#1e1e1e","mainBkg":"#fff4e6","nodeBorder":"#1e1e1e","clusterBkg":"#f8f9fa","edgeLabelBackground":"#ffffff","actorBkg":"#e7f5ff","actorBorder":"#1e1e1e","noteBkgColor":"#fff9db","noteBorderColor":"#1e1e1e"}}}%%
graph LR
  C1["Client 1"] --> LB["Load Balancer"]
  C2["Client 2"] --> LB
  C3["Client 3"] --> LB
  LB --> S1["Server 1"]
  LB --> S2["Server 2"]
  LB --> S3["Server 3"]
  class C1,C2,C3 blue
  class LB orange
  class S1,S2,S3 green
classDef orange fill:#ffd8a8,stroke:#1e1e1e,color:#1e1e1e
classDef blue fill:#a5d8ff,stroke:#1e1e1e,color:#1e1e1e
classDef green fill:#b2f2bb,stroke:#1e1e1e,color:#1e1e1e
classDef red fill:#ffc9c9,stroke:#1e1e1e,color:#1e1e1e
classDef violet fill:#d0bfff,stroke:#1e1e1e,color:#1e1e1e
classDef yellow fill:#ffec99,stroke:#1e1e1e,color:#1e1e1e
classDef grey fill:#e9ecef,stroke:#1e1e1e,color:#1e1e1e
DiagramClients only ever talk to the load balancer. It decides which server answers.

Horizontal scaling

Add more servers to handle more load, instead of buying one bigger server.

Fault tolerance

If one server dies, traffic goes to the healthy ones. Users see no downtime.

Even load

No single server is overwhelmed while others sit idle.

Load balancing algorithmsHow it works

The algorithm is the rule the load balancer uses to pick a server for each request.

Round Robinsimple

Server 1, Server 2, Server 3, Server 1… Simple and fair, but ignores load: a slow server still gets its full share.

Weighted Round Robincapacity

Give each server a weight. A server with weight 3 gets 3x the traffic. Useful when machines have different specs.

Least Connectionslong-lived

Send to the server with the fewest open connections. Best for WebSockets and database connections: slow servers pile up connections and get skipped.

IP Hashsticky

Hash the client IP to pick a server, so the same client always lands on the same one. If that server dies, its sessions are lost.

Least Response Timeadaptive

Send to the server with the lowest average response time. Adapts in real time, but the balancer must track response metrics.

AlgorithmBest forDrawback
Round RobinEqual-capacity servers, short requestsIgnores server load
Least ConnectionsLong-lived connections (WebSocket)Requires connection tracking
IP HashSession affinity without shared stateUneven distribution, session loss on failure
Least Response TimeHeterogeneous serversRequires metrics collection

L4 vs L7 load balancersCompare

The “L” is the network layer the balancer works at. The lower it works, the less it can see, and the faster it is.

%%{init: {"look":"handDrawn","handDrawnSeed":7,"theme":"base","fontFamily":"Virgil, \"Segoe Print\", \"Comic Sans MS\", cursive","themeVariables":{"fontFamily":"Virgil, \"Segoe Print\", \"Comic Sans MS\", cursive","fontSize":"16px","primaryColor":"#fff4e6","primaryBorderColor":"#1e1e1e","primaryTextColor":"#1e1e1e","secondaryColor":"#e7f5ff","tertiaryColor":"#ebfbee","lineColor":"#1e1e1e","textColor":"#1e1e1e","mainBkg":"#fff4e6","nodeBorder":"#1e1e1e","clusterBkg":"#f8f9fa","edgeLabelBackground":"#ffffff","actorBkg":"#e7f5ff","actorBorder":"#1e1e1e","noteBkgColor":"#fff9db","noteBorderColor":"#1e1e1e"}}}%%
graph TD
  subgraph L4["L4 · transport layer"]
    A["Sees: IP address + port<br/>TCP / UDP level<br/>fast, no content inspection"]
  end
  subgraph L7["L7 · application layer"]
    B["Sees: headers, cookies, URL<br/>HTTP / HTTPS level<br/>content-based routing"]
  end
  class A blue
  class B violet
classDef orange fill:#ffd8a8,stroke:#1e1e1e,color:#1e1e1e
classDef blue fill:#a5d8ff,stroke:#1e1e1e,color:#1e1e1e
classDef green fill:#b2f2bb,stroke:#1e1e1e,color:#1e1e1e
classDef red fill:#ffc9c9,stroke:#1e1e1e,color:#1e1e1e
classDef violet fill:#d0bfff,stroke:#1e1e1e,color:#1e1e1e
classDef yellow fill:#ffec99,stroke:#1e1e1e,color:#1e1e1e
classDef grey fill:#e9ecef,stroke:#1e1e1e,color:#1e1e1e
DiagramL4 only sees where a packet is going. L7 opens the HTTP request and can route on its content.
L4 (transport)L7 (application)
Routes onIP address and portHeaders, cookies, URL path, body
SpeedFaster: never reads the contentSlower: parses every HTTP request
Can doPlain forwardingURL routing, TLS termination, caching, auth
Used forDatabases, game servers, streamingWeb apps and APIs

An L7 balancer can send different URL paths to different server pools:

L7 routing rulesText
/api/*        →  backend-servers
/static/*     →  cdn-servers
/websocket/*  →  realtime-servers

Health checksHow it works

%%{init: {"look":"handDrawn","handDrawnSeed":7,"theme":"base","fontFamily":"Virgil, \"Segoe Print\", \"Comic Sans MS\", cursive","themeVariables":{"fontFamily":"Virgil, \"Segoe Print\", \"Comic Sans MS\", cursive","fontSize":"16px","primaryColor":"#fff4e6","primaryBorderColor":"#1e1e1e","primaryTextColor":"#1e1e1e","secondaryColor":"#e7f5ff","tertiaryColor":"#ebfbee","lineColor":"#1e1e1e","textColor":"#1e1e1e","mainBkg":"#fff4e6","nodeBorder":"#1e1e1e","clusterBkg":"#f8f9fa","edgeLabelBackground":"#ffffff","actorBkg":"#e7f5ff","actorBorder":"#1e1e1e","noteBkgColor":"#fff9db","noteBorderColor":"#1e1e1e"}}}%%
graph LR
  LB["Load Balancer"] --> HC["Health checker"]
  HC -->|"GET /health every 10s"| S1["Server 1 ✓"]
  HC -->|"GET /health every 10s"| S2["Server 2 ✗"]
  HC -->|"GET /health every 10s"| S3["Server 3 ✓"]
  class LB orange
  class HC yellow
  class S1,S3 green
  class S2 red
classDef orange fill:#ffd8a8,stroke:#1e1e1e,color:#1e1e1e
classDef blue fill:#a5d8ff,stroke:#1e1e1e,color:#1e1e1e
classDef green fill:#b2f2bb,stroke:#1e1e1e,color:#1e1e1e
classDef red fill:#ffc9c9,stroke:#1e1e1e,color:#1e1e1e
classDef violet fill:#d0bfff,stroke:#1e1e1e,color:#1e1e1e
classDef yellow fill:#ffec99,stroke:#1e1e1e,color:#1e1e1e
classDef grey fill:#e9ecef,stroke:#1e1e1e,color:#1e1e1e
DiagramA server that fails its probe is taken out of the pool until it passes again.

Active checks

The load balancer sends probe requests itself, such as HTTP GET /health, on a timer.

Passive checks

The load balancer watches real traffic and marks a server bad after errors (5xx responses, timeouts).

Sticky sessionsDesign

Some apps keep session state in a server's memory, so the same user must keep reaching the same server. Sticky sessions pin a client to one server using a cookie or an IP hash.

Common interview mistakesPitfalls

No health checksA dead server keeps receiving traffic until users start reporting errors.
The wrong algorithmRound robin wastes capacity on long-lived connections. Use least connections.
A single load balancerThe balancer itself becomes the single point of failure. Run a pair with a floating IP (VIP) that fails over, as managed ones like AWS ELB do.
Ignoring SSL terminationLet the load balancer handle TLS so backend servers do not spend CPU on encryption.
Forgetting geographyFor global users, use DNS-based balancing (for example Route 53) to send people to the nearest region first.

How to bring it up in the interviewIn the interview

One sentence like that covers the layer, the algorithm, where state lives and how failures are handled.

Frequently asked questionsFAQ

What are the main load-balancing algorithms?

Round robin cycles servers in order, least connections routes to the least busy server, and consistent or IP hashing pins clients to servers when sticky sessions are needed. Weighted variants let stronger servers take a larger share of traffic.

What is the difference between L4 and L7 load balancing?

L4 balancing routes on transport-layer data like IP and port, so it is fast and sees only connection metadata. L7 balancing inspects HTTP headers and paths, enabling URL-based routing, caching, and authentication at the cost of more CPU per request.

How do you detect that a backend is unhealthy?

Active health checks periodically probe an endpoint and remove failing servers, while passive checks mark servers after repeated timeouts or 5xx responses. Combine both: keep probing removed servers so they rejoin automatically once they recover.

Now explain load balancing to me out loud

I'll give you a design problem and push on the trade-offs, like a real interviewer.

Practice with Alex

Keep readingKeep reading

Ready to practice?

Reading is step one. Saying it out loud is step two.

Start a mock interview with Alex, get pushed on the parts you skip, and see your hiring signal right after.