Load Balancing

Published on:

Most important commands to remember

  • haproxy -c -f FILE — validate configuration without starting the proxy.
  • curl -i URL — inspect an HTTP response including headers.

Commands and flags

Option Meaning
-c / -f for HAProxy Check configuration / select the configuration file.
-i for curl Include response headers with the body.
--max-time 5 Limit a transfer to five seconds.

The example inspects an existing lab installation. A configuration check does not reload its running process.

The concepts that matter

1. One endpoint can represent several backends

A load balancer selects from a pool of backends behind a shared endpoint. Clients address the frontend while selection directs work toward a suitable server.

Layer 4 balancing commonly chooses for a transport connection; Layer 7 balancing can inspect application information such as HTTP hostnames and paths. Neither guarantees that each individual request opens a new backend connection or selects a different server.

2. An algorithm chooses among eligible servers

Round robin rotates choices; least-connections algorithms consider current connection counts; weights can represent different intended shares. The selection unit and connection reuse affect the distribution you observe.

Two identical responses do not prove that one backend served both. Two different responses do not establish even distribution. To identify the selected backend, use an explicitly provided diagnostic identifier or authorized backend logs, not guesses from response timing.

3. Health checks define eligibility

A health check decides whether a server remains eligible. A TCP connection check asks a narrower question than an HTTP readiness endpoint. Probe intervals and success/failure thresholds determine how quickly state changes.

Removal from new selection does not magically move existing sessions to another server. Connection draining lets existing work finish during planned changes. Recovery policy and readiness need to match what the application can actually serve.

4. Affinity trades distribution for continuity

Session affinity tries to keep related traffic on the same backend, for example through a cookie or source-address mapping. It can help applications with local session state, but can concentrate traffic and complicate failure recovery.

A resilient application should know where its state lives and what happens when that backend disappears. Load balancing increases routing choices; it does not replicate application data or guarantee that retrying an interrupted request is safe.

One small example

Optional: use the existing local lab described above. If you have no HAProxy deployment, the concepts do not require installing one.

haproxy -c -f /etc/haproxy/haproxy.cfg
curl -i --max-time 5 http://127.0.0.1:8080/
curl -i --max-time 5 http://127.0.0.1:8080/

The first command checks whether the configuration is acceptable to this HAProxy binary. A missing file or unreadable certificate is a validation failure, not evidence that a backend is down. No successful configuration check proves that its servers are reachable.

The next commands issue separate GET transfers. Read status, headers, and body. If the lab deliberately exposes a backend identifier, compare it; otherwise the responses cannot tell you which backend handled them. Cookies, weights, health, and reuse can all affect selection.

A refused local connection means no reachable frontend at that address. Nothing is reconfigured or started, and no cleanup is required.

Keep this idea: Load balancing selects eligible backends; health checks, connection lifetime, and application state determine the result.