Load Balancing: The Architecture Behind Every Application That Scales
Every request you make online passes through a load balancer. Here's how it decides where your request goes — and what happens when it gets it wrong.
Before Your Code Runs
Every time you open Instagram, hit a Stripe payment endpoint, or search on Google — your request never goes directly to a server. It goes through a load balancer first.
The load balancer makes a decision in microseconds: which server handles this? Then it forwards the request and gets out of the way.
This sounds simple. It isn't. And getting it wrong at scale means cascading failures, slow responses, and servers dying under uneven load while others sit idle.
Why It Exists
A single server has limits. CPU, memory, network bandwidth — all finite. When traffic grows beyond what one machine can handle, you add more servers. But now you have a new problem: how does traffic know where to go?
Without a load balancer, every client would need to know the address of every server. Servers would need to coordinate amongst themselves. One overloaded server would cause failures while others sit idle.
A load balancer solves this by being the single entry point. Clients talk to the load balancer. The load balancer decides which server handles the request. Servers are invisible to the outside world.
Client → Load Balancer → Server A
→ Server B
→ Server C
This also means if Server B crashes, the load balancer stops sending it traffic. The client never knows Server B existed. This is the foundation of high availability.
The Algorithms That Drive the Decision
The load balancer doesn't randomly pick a server. It uses an algorithm. The choice of algorithm determines how evenly load is distributed — and what breaks when things go wrong.
Round Robin
Requests are distributed sequentially. Request 1 goes to Server A, Request 2 to Server B, Request 3 to Server C, Request 4 back to Server A.
This is NGINX's default. It works well when all servers are identical and requests take roughly the same time to process.
The problem: if Server A is processing a slow database query when the next request arrives, Round Robin sends it there anyway. The queue builds up. Latency spikes.
Least Connections
Instead of cycling through servers, the load balancer tracks how many active connections each server currently has. New requests go to the server with the fewest.
This is what production systems use for variable workloads — API servers handling requests of wildly different complexity. HAProxy implements this as balance leastconn. NGINX uses least_conn.
Server A: 45 active connections
Server B: 12 active connections ← next request goes here
Server C: 38 active connections
IP Hash
The client's IP address is hashed to determine which server they always hit. The same user always reaches the same server.
This matters when your application is stateful — when a user's session data lives in server memory rather than a shared store like Redis. Breaking the hash means breaking the session.
The tradeoff: if that server dies, all users hashed to it lose their sessions. This is why stateless architectures — where session state lives in Redis or a database — are strongly preferred at scale. IP hash becomes a workaround for a design decision you shouldn't have made.
Layer 4 vs Layer 7
Load balancers operate at different layers of the network stack, and the distinction matters.
Layer 4 (Transport Layer) — routes based on IP address and TCP/UDP port. It doesn't read the request content. Fast, low overhead, but unintelligent. It can't distinguish a /health check from a complex /search query.
Layer 7 (Application Layer) — reads the actual HTTP request. It can route based on URL path, headers, cookies, or request method. This is where modern load balancing gets powerful.
With Layer 7, you can:
- Route
/api/*to your Node.js servers and/static/*to a CDN origin - Send traffic to a canary deployment for 5% of users based on a header
- Route requests to the nearest geographic region based on the
Accept-Languageheader - Strip and add headers before forwarding
AWS ALB (Application Load Balancer) operates at Layer 7. AWS NLB (Network Load Balancer) operates at Layer 4. The choice depends on whether you need content-aware routing.
Health Checks: How Load Balancers Know When a Server Dies
A load balancer is only useful if it stops sending traffic to failing servers. It discovers failures through health checks — periodic requests to each server to verify it's alive and functioning.
A shallow health check just checks if the server responds at all:
GET /health → 200 OK
A production health check goes deeper — it verifies the server can actually serve traffic:
javascript
// Node.js deep health check app.get('/health', async (req, res) => { try { await db.query('SELECT 1'); // database reachable? await redis.ping(); // cache reachable? res.status(200).json({ status: 'ok' }); } catch (err) { res.status(503).json({ status: 'degraded', error: err.message }); } });
If this endpoint returns anything other than 200, the load balancer marks the server unhealthy and stops routing traffic to it. When it recovers, traffic resumes automatically. No human intervention required.
Netflix calls this pattern "automated recovery." It's why Netflix services can lose individual servers without users noticing.
The Real-World Stack
Production systems rarely use a single load balancer. They layer them:
User Request
↓
Cloudflare (edge — global DNS, DDoS protection, anycast routing)
↓
AWS ALB (regional — Layer 7, path-based routing, SSL termination)
↓
NGINX / Envoy (service mesh — microservice-to-microservice routing)
↓
Your Application Servers (Kubernetes pods, ECS tasks)
Cloudflare routes users to the nearest data center globally. The AWS ALB distributes across availability zones within a region. NGINX or Envoy handles internal service routing. Kubernetes manages the actual pods.
Each layer solves a different scope of the same problem: how do we distribute traffic intelligently at this level?
The Tradeoff Nobody Mentions
A load balancer is itself a single point of failure. If it goes down, everything behind it goes down with it.
The solution is running load balancers in pairs with a failover mechanism — active-passive or active-active configurations. AWS manages this for you with ALB. If you run your own HAProxy or NGINX, you need to handle this explicitly with something like Keepalived.
This is the recurring pattern in system design: every solution introduces a new problem at a different level. Solving the single-server problem with multiple servers creates the routing problem. Solving the routing problem with a load balancer creates the load-balancer-as-single-point-of-failure problem.
Good system design is recognising these tradeoffs early and choosing which ones you can live with.
System Design Series — Post 2. Next: Horizontal vs Vertical Scaling — when to add more servers vs bigger servers, and why the answer isn't always obvious.