Skip to content

Load Balancing

A load balancer sits in front of servers and distributes incoming requests. It’s the key to horizontal scaling.


flowchart LR
Users["👥 Users"] --> LB["Load Balancer"]
LB --> S1["Server 1<br/>🟢 Healthy"]
LB --> S2["Server 2<br/>🟢 Healthy"]
LB --> S3["Server 3<br/>🟢 Healthy"]
LB --> Sn["Server N<br/>🔴 Unhealthy"]
style LB fill:#7c3aed,color:#fff
style S1 fill:#059669,color:#fff
style S2 fill:#059669,color:#fff
style S3 fill:#059669,color:#fff
style Sn fill:#dc2626,color:#fff

AlgorithmHow It WorksBest For
Round RobinRequests go to each server in turnSimple, equal-capacity servers
Least ConnectionsSend to server with fewest active connectionsUneven request processing times
IP HashHash client IP → same server alwaysSticky sessions (stateful apps)
Weighted Round RobinHeavier servers get more requestsMixed-capacity servers
RandomPick a server at randomStateless, any server works

LayerHow It RoutesSpeedFeatures
L4 (Transport)By IP + port⚡ FastNo content awareness
L7 (Application)By HTTP path, headers, cookiesSlowerCan route /api → one pool, /web → another

Analogy: L4 is like a mail sorter that reads only the ZIP code. L7 reads the full letter and decides based on content.


  • Health checks — regularly pings servers, removes dead ones
  • SSL termination — handles HTTPS decryption, servers get plain HTTP
  • Session persistence — sends the same user to the same server (if needed)
  • Rate limiting — rejects excess requests

  • The load balancer itself is a Single Point of Failure (SPOF) — always run at least two in active-passive mode.
  • L7 gives more control but adds latency.
  • Most cloud providers offer managed LBs (AWS ALB, GCP HTTP LB).

  • Load balancer = traffic cop. Spreads requests so no server gets overwhelmed.
  • Round robin is simplest. Least connections is smarter for uneven work.
  • L4 is fast but dumb. L7 is slower but can route by URL.