Balanceador de Carga

Balanceador L4/L7

Escrito por Darjus Kazakevic · revisado el

Health checks decide who gets traffic; the algorithm only decides who is next.

A load balancer is one address in front of a pool of identical servers. It receives each request, picks a healthy server and forwards the request to it. Common products are NGINX, HAProxy, Envoy and cloud balancers such as AWS ALB. With identical servers, almost any simple algorithm spreads the load well.

The hard part is deciding who is in the pool. The balancer runs health checks and removes servers that fail them. A check that is too weak keeps a broken server in rotation. A check that is too strict removes every server during a short database blip.

En esencia — Spread requests over identical servers, and take a server out of the pool the moment it fails its health check.

Cuándo recurrir a él

Recurre a él cuando

  • Two or more identical servers must answer behind one address
  • Servers crash or redeploy, and traffic must route around them
  • You add capacity by adding servers, not by buying bigger machines

Reconsidéralo cuando

  • One server only: the balancer adds a hop and balances nothing
  • Servers are not identical: each holds state the next call needs
  • Clients already pick servers themselves, as in client-side gRPC

Conceptos clave

Layer 4 vs layer 7 (TCP vs HTTP balancing)
An L4 balancer forwards TCP connections without reading them, so it is very fast. An L7 balancer reads each HTTP request, so it can route by path or header, retry, and terminate TLS.
Balancing algorithms
The rule that picks the next server. Round-robin takes turns. Least-connections picks the server with the fewest open requests. Power of two choices picks two servers at random and uses the less busy one.
Health checks (Active and passive)
Active checks call an endpoint such as /health every few seconds. Passive checks watch real traffic and skip a server after several errors. Most setups use both.
Sticky sessions (Session affinity)
The balancer sends the same user to the same server, using a cookie or a hash of the client IP. It keeps in-memory sessions working, but load becomes uneven and a crash logs those users out.
Connection draining (Graceful removal)
Before a server leaves the pool for a deploy, the balancer stops sending it new requests but lets open ones finish, for example within 30 seconds. Deploys then cause no failed requests.

Reglas de diseño

Make “healthy” mean able to serve
Check what this server needs, such as its database pool, not only that the process is up. Remove a server only after 2–3 failed checks in a row, so one slow check does not empty the pool.
Count a timeout as a failure
Give each health check a short timeout, such as 2 seconds. Without one, a hung server looks alive for as long as the check waits, and it keeps receiving traffic.
Keep the servers stateless
Store sessions in Redis, a database or a signed token. Then any server can answer any request, and you can remove one without logging anyone out.
Run at least two balancers
One balancer is a single point of failure. Run a pair with a floating IP, or use a managed balancer that already spans several zones.

Errores habituales

  • A /health endpoint that always returns 200. The process is up but cannot reach its database, so users get errors while the balancer shows all green.
  • Every server deep-checks the same shared database. When it fails, all servers fail their checks at once and the pool is empty. Know whether your balancer then fails open.
  • Sticky sessions used for correctness. One server crashes and all its users lose their sessions, and new servers get almost no traffic.
  • L4 balancing for long-lived HTTP/2 or gRPC connections. Each client sticks to one server, so new servers stay idle. Balance per request at L7.