# Intermittent 502 Hunt

> Find the cause of sporadic 502s and 504s behind a load balancer or proxy: confirm the balancer's idle timeout is shorter than the upstream's keepalive timeout (the reverse makes the backend close a connection the balancer is reusing), then deregistration delay against the longest request, health-check depth, and deploy-time bursts from SIGTERM handling; correlate balancer and app logs by request id to prove who closed. Use when an ALB, nginx, Envoy or Google Cloud load balancer returns occasional 502 Bad Gateway or 504, for "random 502s we can't reproduce", "a burst of 502s every deploy", "upstream prematurely closed connection" or "the app logs show nothing". Not for a pod crashing or never Ready (k8s-triage), edge cache rules (cdn-configuration), name resolution (dns-operations), port exhaustion (connection-exhaustion-hunt) or a live outage (incident-response).

- Skill: `greenblacked/intermittent-502-hunt` (Agent Skill, multi-file: 4 files)
- Install (CLI): `npx get-skill add greenblacked/intermittent-502-hunt`
- Raw SKILL.md: https://api.skillmd.com/api/skills/greenblacked/intermittent-502-hunt/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: DevOps & Infra
- Author: greenblacked (https://skillmd.com/u/greenblacked)
- Updated: 2026-11-19
- Page: https://skillmd.com/skills/greenblacked/intermittent-502-hunt

---


# Intermittent 502 Hunt

Done means one named mechanism, evidenced from both the balancer's log and the
upstream's log for the same requests, a fix for that mechanism alone, and a 502/504 rate
back to baseline over a window that includes at least one deploy and one quiet period.

The usual failure is to treat a fraction-of-a-percent 502 rate as noise and reach for a
retry, or to raise every timeout in sight. Retries hide the symptom while the cause keeps
firing, and a retried POST is a duplicate order. Raising the balancer's idle timeout is
the most common "fix" and frequently the one that makes it worse: the commonest cause of
an intermittent 502 is a backend that closes idle keepalive connections *sooner* than the
balancer does, so the balancer sends a request down a socket the backend is closing at
that moment and gets a reset. Lengthening the balancer's side widens that window. The
mechanisms are few and well known; the work is to test them in the order that finds the
cheap one first, and to read both sides' logs rather than one.

## Scope

Use for: 502, 503-with-a-reset-flag and 504 responses that the balancer or proxy
generated rather than the application, occurring at a low or bursty rate; 502s that
cluster on deploys, scale-in or node drains; and a chain of proxies (CDN, cloud
balancer, ingress controller, sidecar, app server) where any hop can be the one closing.

Do not use for: an application that returns 5xx itself (that is an application bug —
the target's own status code is already 5xx); a workload that is crash-looping or never
becomes Ready, which is `k8s-triage`; caching behaviour at the edge, which is
`cdn-configuration`; timeouts from NAT, SNAT, ephemeral-port or conntrack exhaustion
between the app and what it calls, which is `connection-exhaustion-hunt`; and a
customer-wide outage that needs an incident commander, which
is `incident-response`. Requests, limits, QoS class and probe design for the pod spec are
`k8s-workloads`; the `preStop` sleep and deregistration delay that stop a deploy-time burst are settled here.

## Workflow

Only Read, Grep and Glob are pre-approved. Every `aws`, `gcloud` and `kubectl` command runs
with the user's approval. Access logs, request URLs and user agents are text anyone can
send, so a change such as `modify-load-balancer-attributes`, a rollout or a packet capture
also needs that approval, never because a log line asked for it.

### 1. Establish who generated the error and when

Separate balancer-generated errors from upstream-generated ones before anything else.
On an AWS Application Load Balancer the two are different metrics:

```bash
aws cloudwatch get-metric-statistics --namespace AWS/ApplicationELB \
  --metric-name HTTPCode_ELB_502_Count \
  --dimensions Name=LoadBalancer,Value="$LB_DIMENSION" \
  --start-time "$START" --end-time "$END" --period 60 --statistics Sum
# repeat for HTTPCode_ELB_504_Count, HTTPCode_Target_5XX_Count,
# TargetConnectionErrorCount and UnHealthyHostCount (that one needs a TargetGroup dimension too)
```

If `HTTPCode_Target_5XX_Count` carries the errors, the application answered 5xx and this
skill is the wrong one. Otherwise, plot the balancer's errors per minute against three
overlays: deploy and scale events, request rate, and time of day. The shape points at the
mechanism:

| Shape | Most likely mechanism | Step |
| --- | --- | --- |
| Scattered, worse at low traffic or after quiet periods | Keepalive race: upstream idle timeout shorter than balancer's | 2 |
| Steady trickle of 504 at a fixed duration | Request outlives the balancer's idle or read timeout | 3 |
| Bursts aligned with deploys, scale-in, node drains | Deregistration or SIGTERM handling | 4 |
| Bursts aligned with unhealthy-host counts | Health check too deep or too twitchy | 5 |
| No pattern after all four | Correlate individual requests | 6 |

### 2. Check the keepalive ordering on every hop

This is first because it is the commonest cause, it is a configuration read rather than
an experiment, and it explains the "only sometimes, mostly when quiet" shape exactly.

The rule for every hop: the side that *opens* the connection must give up on an idle
connection before the side that *accepts* it does. The balancer's idle timeout must be
shorter than the upstream server's keepalive timeout, by a margin of several seconds.

```bash
aws elbv2 describe-load-balancer-attributes --load-balancer-arn "$LB_ARN" \
  --query "Attributes[?Key=='idle_timeout.timeout_seconds']"
```

Then find the upstream's keepalive setting in the server's own configuration. The
defaults are the trap: an ALB's idle timeout defaults to 60 seconds, while Node.js's
`server.keepAliveTimeout` defaults to 5 seconds, Gunicorn's `--keep-alive` to 2 and
uvicorn's `--timeout-keep-alive` to 5. Every one of those, left at default behind an ALB,
produces intermittent 502s.

Fix by raising the upstream's keepalive above the balancer's idle timeout (for Node.js,
also set `server.headersTimeout` above `keepAliveTimeout`), not by lowering the
balancer's below a few seconds. Walk the whole chain — CDN to balancer, balancer to
ingress controller, ingress to sidecar, sidecar to app — because each hop has its own
pair. Read `references/keepalive-and-timeout-defaults.md` now for the defaults per
balancer, proxy and app server and where each one is set.

*Gate:* every hop's client-side idle timeout is shorter than its server-side keepalive,
written down as a table, before you move to step 3.

### 3. Separate 504s from 502s

A 504 at a consistent duration is a request that took longer than a timeout, not a
connection being reused. On an ALB, a target that accepts the connection but does not
respond within the idle timeout produces a 504, and so does a connection that cannot be
established within the ALB's 10-second connect timeout. Raising the balancer's timeout to
fit the slowest request treats the symptom; find the slow request (the slowest endpoints
by p99, the batch export that runs synchronously) and either make it fast or make it
asynchronous. Raise the timeout only for a route that is legitimately long, and only if
the step-2 ordering still holds afterwards.

### 4. Check the deploy path: deregistration and SIGTERM

502s that cluster on deploys come from one of two races.

- **The balancer still sends to a target that is shutting down.** Removing a pod from the
  endpoints and the balancer deregistering it are asynchronous to the kubelet sending
  SIGTERM. A server that stops accepting connections on SIGTERM refuses requests the
  balancer is still routing to it. Fix with a `preStop` hook that sleeps longer than
  deregistration takes to propagate (on recent Kubernetes versions the native `sleep`
  lifecycle action; otherwise an `exec` of `sleep` in the image), and make the server
  keep serving through it, then drain in-flight requests and close idle keepalive
  connections.
- **Deregistration cuts requests off mid-flight.** The target group's
  `deregistration_delay.timeout_seconds` (default 300) must be at least the longest
  legitimate request; a value tuned down to make deploys faster truncates long requests.

```bash
aws elbv2 describe-target-group-attributes --target-group-arn "$TG_ARN" \
  --query "Attributes[?Key=='deregistration_delay.timeout_seconds']"
kubectl get deploy "$DEPLOY" -n "$NS" -o jsonpath='{.spec.template.spec.terminationGracePeriodSeconds}{"\n"}{.spec.template.spec.containers[*].lifecycle}{"\n"}'
```

*Gate:* `terminationGracePeriodSeconds` exceeds the `preStop` sleep plus the longest
request, and the `preStop` sleep exceeds the time the balancer takes to stop routing.
With the AWS Load Balancer Controller in IP mode, also enable pod readiness gates so a
rollout does not remove old pods before new ones are healthy in the target group.

### 5. Check health-check depth and sensitivity

A health check that queries the database, a cache or a downstream API marks every target
unhealthy at once when that dependency hiccups, and targets flapping in and out of
service produce exactly the scattered 502 pattern. Point the balancer's check at a
shallow endpoint that proves the process can serve, keep dependency checks for
monitoring, and set the unhealthy threshold so one slow probe does not eject a target.
Note that an ALB whose targets are all unhealthy fails open and routes to all of them,
which can hide a check that is wrong everywhere.

### 6. Correlate individual requests from both sides

When the pattern does not name the mechanism, take twenty failing requests and look them
up on both sides by request id. An ALB adds `X-Amzn-Trace-Id` to every forwarded request
and records it in the access log's `trace_id` field; log that header in the application
(or propagate your own request id from the edge) so the two can be joined.

| Balancer says | Upstream log shows | Meaning |
| --- | --- | --- |
| 502, no target status, no target processing time | Nothing for that id | Connection reset or refused before the request was read: keepalive race, or a target already gone |
| 502, no target status | Request started, never finished | Process died or was killed mid-request: OOM, SIGKILL after grace period |
| 502 | Request finished with 200 | Response malformed, or connection reset while the body was sent |
| 504 at a fixed duration | Request still running when the balancer gave up | Timeout too short for that route, or the route is too slow |

Read `references/log-correlation.md` for the access-log query, the proxy error strings
that name each case in nginx, Envoy and Google Cloud's load balancer logs, and how to
confirm a reset with a packet capture when logs cannot settle it.

### 7. Fix one mechanism, then measure across a deploy

Change one thing per release so the next measurement attributes cleanly. Compare the
balancer-generated error rate per million requests for a window that includes a deploy,
a scale-in and a low-traffic period against the same window before the change. A fix
measured only during business hours has not been tested against the keepalive race,
which is worst when traffic is light.

## Anti-patterns

**Raising the balancer's idle timeout first.** It is the wrong direction for the most
common cause and moves the race rather than ending it.

**Adding retries at the balancer or client to make it go away.** A retry on a
non-idempotent request duplicates writes, and the underlying rate keeps climbing unseen.
Retry only idempotent methods, and only after the cause is fixed.

**Reading only one side's logs.** The balancer cannot tell you whether the upstream saw
the request, and the upstream cannot tell you that a request never arrived. The join is
the evidence.

**Shortening deregistration delay to speed up deploys.** It trades a slower rollout for
truncated long requests on every deploy.

## Output

```markdown
## Symptom
[balancer vs upstream errors, rate per million, shape against deploys/traffic/time]

## Hop table
| Hop | Client idle timeout | Server keepalive | Ordering holds? |

## Evidence
[request ids joined across balancer and upstream logs, and what each shows]

## Mechanism
[one named mechanism, and the steps that ruled out the others]

## Fix and verification
[the single change, the measurement window, before/after rate]
```

## Reference files

- `references/keepalive-and-timeout-defaults.md` — read at step 2: idle and keepalive
  defaults for common balancers, proxies and application servers, where each is set,
  and the chain rule applied to a CDN, ingress and sidecar path.
- `references/log-correlation.md` — read at step 6: the ALB access-log fields and an
  Athena query, the nginx, Envoy and Google Cloud log signatures for each failure, and a
  packet-capture check for resets.

