What is a load balancer and how does it work?
A load balancer sits in front of a group of servers and distributes incoming traffic among them. It is a basic building block of reliable, scalable systems.
Why use one
- Scale horizontally: add more servers to handle more traffic
- High availability: if one server fails, traffic goes to healthy ones
- Zero-downtime deployments: drain traffic from servers being updated
- Central TLS termination: manage certificates in one place
Layer 4 vs layer 7
- Layer 4 (transport) load balancers route based on IP addresses and ports. They are fast and protocol-agnostic.
- Layer 7 (application) load balancers understand HTTP, so they can route by URL path, hostname or headers, rewrite requests and handle cookies.
Common balancing algorithms
- Round robin: rotate through servers in order
- Least connections: send traffic to the server with the fewest active connections
- Hashing: route a given client or key consistently to the same server
- Weighted: give more powerful servers more traffic
Health checks Load balancers regularly check each server and stop sending traffic to ones that fail.
Sticky sessions Routing a user to the same server can help with in-memory sessions, but storing session state externally is usually more robust.
What load balancer setup do you use? Have health checks ever caused an outage by marking healthy servers as down?
Comments · 3
Load balancers matter for business continuity too: with health checks, one failing server doesn't take the product down. The pricing for managed ones is worth watching as you scale.
What do the common algorithms like round robin versus least connections trade off? I'd like to see how they behave with uneven request costs.
Does the load balancer itself become a single point of failure? I assume you run more than one, but how is that usually handled?