Linux and networking
Cloud load balancers
What a cloud load balancer does, how ALB, NLB and Gateway LB differ, why health checks can take the whole fleet down, and where Ingress fits inside the cluster.
A load balancer stands in front of several servers and spreads incoming connections across the healthy ones. Cloud providers offer several kinds. Most of the choice comes down to one question: do you need to read the request, or only move the bytes?
Concept
Layer 4 (L4) balancers route TCP or UDP connections without reading the content. Layer 7 (L7) balancers read HTTP, so they can route by host name or path. A health check is a regular probe that marks each backend as healthy or not. Unhealthy backends stop getting traffic. See networking basics for the layers.
The big idea
Think of a host at a restaurant. Guests are seated only at tables marked open. A table that is closed for cleaning gets no guests until it reopens. The host does not cook, and does not decide what you order.
The analogy hides one thing. A real load balancer also has to hold connections open and close them cleanly. The metaphor covers routing, not the connection handling.
The types
| Type | Layer | Best for | Notes |
|---|---|---|---|
| ALB (Application) | L7 HTTP/HTTPS | Web APIs and microservices with path or host routing | Works with WAF, OIDC and Lambda targets |
| NLB (Network) | L4 TCP, UDP, TLS | Very low latency, static IPs, non-HTTP, very high packet rates | Often the front of a PrivateLink service |
| Gateway LB | L3/L4 | Third-party appliances such as firewalls | Sends traffic through an inspection fleet |
Target groups hold the backends. Health checks decide which targets get traffic.
On GCP, the global external HTTP(S) load balancer plays the ALB role. It uses an anycast front IP, URL maps and backends in several regions.
Trade-off
Default to ALB or an HTTP(S) load balancer for HTTP APIs. Choose NLB when you need a static IP, a non-HTTP protocol, or PrivateLink. Chaining NLB into ALB adds a hop and cost, so do it only when you need both.
Walking through a health check failure
Say three API instances sit behind a load balancer, and the health check calls /healthz. That endpoint runs a database query.
- A short database blip makes the query fail.
- All three instances fail the health check at once.
- The load balancer has no healthy target left, so it returns errors to every client.
- The instances were fine. The check made them look dead.
The fix: use a shallow check for load balancer health, such as "the process is up." Put dependency checks in your monitoring, not in the traffic path.
Cross-zone balancing
Cross-zone load balancing sends traffic to targets in any availability zone. On ALB it is usually on by default, which evens out load when zones have uneven target counts. NLB's default has differed in the past, so check your setting. Cross-zone traffic can add inter-AZ data charges.
Behind the load balancer: Ingress and Gateway API
Inside a cluster, a Service of type LoadBalancer gets its own cloud load balancer. That is simple for a few entry points and expensive at scale. An Ingress controller instead shares one or a few load balancers across many hosts and paths. Gateway API is the newer model. It splits roles: infrastructure owns the Gateway, and app teams own the routes.
Wrong target settings break things quietly. A target type that points at nodes instead of pods can lose the client IP, or fail health checks.
Common mistakes
- Health checks that run a dependency query. They can remove the whole fleet.
- Using NLB for every API. You lose L7 routing for no reason.
- Forgetting cross-zone charges. Balancing evenly can cost more.
- Copying controller annotations from old answers. Read the docs for your controller first.
Check yourself
- You need a static IP for a PrivateLink entry point. Which type fits? An NLB. It offers static IPs and PrivateLink, which the ALB does not.
- A health check runs a database query, and the database blips briefly. What happens? Every target can fail at once, leaving no healthy backend, so clients get errors.
Key takeaways
- L7 balancers read HTTP. L4 balancers move connections.
- Health checks should be shallow, so a dependency blip does not remove the fleet.
- Ingress and Gateway API share one entry point across many services.
Further learning
Next, see how DNS decides which load balancer a name points at in DNS routing and TTL.
Related
Load balancers, DNS and the edge in the cloud
Configuring the traffic layer, from DNS and cloud load balancers through TLS, WAF and CDN to a regional traffic shift. Start here for the map of edge pages.
Cloud networking and VPCs
VPCs, subnets, routing, NAT, security groups and private connectivity in AWS and GCP, plus a three-AZ reference architecture and how to debug when A cannot reach B.
Load balancing and proxies
How a load balancer chooses which server gets each request, and how a badly set up balancer can cause the outage it was meant to prevent.