Load balancer
Elastic Load Balancing (ALB / NLB)
Spreads incoming requests across healthy instances and takes dead ones out of rotation.
In a monolith
Optional (one server)
With one server there’s nothing to balance. The moment you run two instances, an LB (Nginx/HAProxy or managed) becomes the front door.
In a distributed system
Layer-7 routing per service
Path/host rules send /orders/* and /catalog/* to different target groups; health checks, TLS termination and connection draining during deploys.
On AWS
Elastic Load Balancing (ALB / NLB)
ALB (layer 7: paths, headers, WebSockets, gRPC) or NLB (layer 4: TCP/UDP, static IPs). Spans multiple AZs; targets are EC2, ECS, Lambda or IPs.
- Application Load Balancer (ALB)$$HTTP routing, OIDC/Cognito auth actions, WAF attachable
- Network Load Balancer (NLB)$$Layer 4, very low latency, static IPs, no HTTP awareness
- Self-managed Nginx/HAProxy on EC2$Full control — but you must make it redundant yourself
- Reach for it when
- One server isn’t enough anymore, or you need zero-downtime deploys.
- What it costs you
- Servers behind it must be stateless (or you need sticky sessions)
- One more hop (~1 ms)
- A single self-hosted LB is a SPOF unless paired with a floating IP
- When it dies
- A managed LB is redundant across AZs. A lone self-hosted LB is the textbook SPOF: it dies and everything behind it is unreachable.
Interview follow-ups
L4 vs L7 load balancing?
L4 routes TCP/UDP connections by IP and port — fast and protocol-agnostic. L7 understands HTTP (paths, headers, cookies), enabling per-route rules, auth and smarter health checks.
Round robin, least connections or consistent hashing?
Round robin for uniform requests; least connections when request durations vary; consistent hashing when the same key should land on the same server (cache affinity).
What are sticky sessions and why avoid them?
The LB pins a user to one server so in-memory sessions keep working. It skews load and loses sessions when that server dies — move state to a shared store instead.
Connection rules
It talks to
- App server
- Service
- Realtime server
- API gatewayOnly when: Self-hosted gateways (Kong, Envoy) run as a fleet behind an LB.
Gets traffic from
Never
- Load balancer → WorkerWorkers pull from queues; they don’t receive HTTP traffic.
- Load balancer → SQL databaseA load balancer forwards HTTP to compute. Data access needs code: auth, queries, validation.
- Load balancer → Read replicaA load balancer forwards HTTP to compute. Data access needs code: auth, queries, validation.
- Load balancer → NoSQL storeA load balancer forwards HTTP to compute. Data access needs code: auth, queries, validation.
- Load balancer → CacheA load balancer forwards HTTP to compute. Data access needs code: auth, queries, validation.
- Load balancer → Object storageA load balancer forwards HTTP to compute. Data access needs code: auth, queries, validation.
- Load balancer → SearchA load balancer forwards HTTP to compute. Data access needs code: auth, queries, validation.
- Load balancer → DB proxyA load balancer forwards HTTP to compute. Data access needs code: auth, queries, validation.