Load Balancer
A device or software that distributes network or application traffic across a cluster of servers.
Detailed Explanation
Load balancing prevents any single server from becoming a bottleneck, improving application responsiveness and availability. If one server fails, the load balancer redirects traffic to the remaining healthy servers. It can operate at the network layer (Layer 4) or the application layer (Layer 7), using algorithms like Round Robin or Least Connections.
Quick Summary
A load balancer spreads incoming requests across a pool of backend servers so no one server gets overwhelmed and any single failure is invisible to clients. It is the foundation of horizontal scaling and high availability.
Key Takeaways
- Layer 4 (TCP/UDP) load balancers route by IP and port; Layer 7 (HTTP) can route by path, header, cookie, or hostname.
- Common algorithms: round robin, least connections, IP hash, weighted variants of each.
- Active healthchecks remove failing backends automatically; without them, a load balancer just spreads the pain evenly.
- Sticky sessions (cookie or IP-based) pin a user to the same backend, sometimes necessary, usually a smell.
- Cloud equivalents: AWS ALB/NLB, GCP LB, Azure LB, plus CDN/edge balancers like Cloudflare for global distribution.
When to use it
- Horizontally scaling stateless web servers behind a single hostname.
- Failing traffic over from a primary to a standby region during an outage.
- Canary and blue-green deployments by adjusting traffic weights between backend groups.
- TLS termination plus distribution in one component for HTTPS-fronted services.
Common Mistakes
- Relying on sticky sessions to compensate for stateful backends instead of moving session state to a shared store.
- Configuring healthchecks that only confirm the process is running, not that it can actually serve requests.
- Ignoring connection draining on deploys, in-flight requests get killed when an instance is removed.
- Putting the load balancer in a single AZ; lose the AZ, lose the entire entry point.
Load Balancer, Frequently Asked
Layer 4 or Layer 7 load balancing?
L4 is faster and protocol-agnostic; pick it for raw TCP, databases, or gRPC where you do not need to inspect requests. L7 understands HTTP and lets you route on path/host/headers, pick it for web traffic and APIs.
Round robin or least connections?
Round robin is fine when request cost is uniform. Least connections is better when some requests take much longer than others, because it keeps slow requests from piling up on one backend.
Do I need a load balancer with one server?
Often yes, even with one backend, a managed load balancer gives you TLS termination, healthchecks, and a stable DNS entry that survives backend replacement. It also makes adding a second server trivial later.