Sub topic 13
Metrics, logs and troubleshooting
This chapter assumes you know how Layer 4 and Layer 7 balancing differ, how health checks mark backends up or down, and how to read an NGINX or HAProxy configuration. It covers what to measure on a load balancer, what to log, and how to narrow down common faults.
What to measure
A load balancer sees every request, so it is the best place to observe the whole service. Track these signals per frontend and per backend server.
| Metric | What it tells you | Typical alert |
|---|---|---|
| Request rate | Traffic volume and sudden spikes or drops | Drop of more than 50% against the same hour last week |
| Active connections | Load and possible exhaustion of limits | Above 80% of the configured maximum |
| Error rate (5xx) | Backend failures, split by 502, 503 and 504 | 5xx above 1% of responses |
| Latency percentiles (p50, p95, p99) | User-visible speed. Averages hide slow tails | p99 above your target |
| Healthy backend count | Remaining capacity | Fewer healthy servers than needed for peak load |
| Queue length and retries | Backends that cannot keep up | Queue growing for several minutes |
Alert on percentiles, not averages. A pool where 1% of requests take ten seconds can still show a fine average.
What to log
Access logs should record enough to follow one request from client to backend. In NGINX, add the backend address and timing fields to the log format.
log_format lb '$remote_addr [$time_local] "$request" $status '
'upstream=$upstream_addr '
'upstream_status=$upstream_status '
'req_time=$request_time '
'upstream_time=$upstream_response_time';
access_log /var/log/nginx/lb_access.log lb;The difference between req_time and upstream_time shows where time was spent. A large req_time with a small upstream_time points at a slow client or network. A large upstream_time points at the backend.
Also log a request ID. Add proxy_set_header X-Request-ID $request_id; so the backend can write the same ID in its own logs.
Reading the error codes
Most load balancer errors come from three status codes. Each one points at a different layer.
- 502 Bad Gateway: the backend closed the connection, sent an invalid reply, or refused the connection. Check whether the application crashed or restarted.
- 503 Service Unavailable: no backend is available. Every server is marked down, or a connection limit or queue is full.
- 504 Gateway Timeout: the backend accepted the request but did not answer within the proxy timeout. Look for slow queries or overloaded servers.
A troubleshooting routine
Work from the symptom toward the cause, and change one thing at a time.
- Confirm the scope. Is one client affected, one backend, or everyone? Compare the logs by
upstream_addr. - Check backend health. See which servers the balancer considers up. In HAProxy, the stats page or the runtime API shows this.
- Bypass the balancer. Send a request straight to a backend with
curl. If it fails there too, the fault is in the application. - Compare timings. Use the two timing fields to separate client, balancer and backend delay.
- Check limits. Look for exhausted file descriptors, connection limits or ephemeral ports on the balancer.
HAProxy exposes its state through a stats socket. This example enables it and reads backend status.
global
stats socket /var/run/haproxy.sock mode 660 level admin
defaults
mode http
log global
option httplogecho "show stat" | socat stdio /var/run/haproxy.sock | cut -d, -f1,2,18The command prints the proxy name, server name and status (UP or DOWN) as comma-separated fields.
Common faults
| Symptom | Likely cause | What to check |
|---|---|---|
| Backends flap between up and down | Health check is too strict or the endpoint is slow | Check interval, timeout and the `/healthz` response time |
| One backend gets all the traffic | Sticky sessions or a hash key with little variety | Persistence settings and key choice |
| Backend logs show the balancer's IP | Client address is not forwarded | `X-Forwarded-For` or the PROXY protocol |
| Intermittent 502 after deploys | Backend closes idle keepalive connections before the balancer does | Keepalive timeout on both sides |
Exercise
Your access log shows req_time=12.1 and upstream_time=0.04 for a slow request. Where is the delay most likely, and what would you check next?
Show answer
The backend answered quickly, so the delay is between the client and the balancer. The likely cause is a slow client or network, for example a large upload on a slow connection. Check the client's network path and the request size, and consider whether the client-side timeouts are set sensibly.
Exercise
Users report 503 errors, but every backend responds correctly when you test it with curl. Name two causes on the balancer side.
Show answer
The balancer may have marked all backends down because the health check fails, for example a wrong path or a firewall blocking the probe. Alternatively, a connection limit or queue on the balancer may be full, so it rejects new requests even though the backends are fine.