Debugginghttp502503504gateway

HTTP Debugging: 502, 503 and 504 Are Different Failures

The three gateway errors usually describe three different upstream situations — 502 the backend answered wrongly or closed, 503 no backend was available, 504 the backend did not answer in time — and the debugging procedure is to find which hop generated the status, correlate with that hop’s upstream logs and health state, and check that the timeout ladder is ordered.

ConceptualLinux
Interview question
Progress

You have not marked these as done yet. Read on anyway if they feel familiar; come back to them if something here does not land.

The problem

The status page shows 502s at 0.3%, a spike of 504s at 09:00 and a solid wall of 503 for two minutes overnight. Same load balancer, same backends, three different stories. What generated each status, and what was it trying to tell you?

Three codes, three (usual) meanings

A 5xx from a gateway is a proxy, load balancer or CDN reporting on its upstream — the next hop toward your application. The three codes map, in common practice, to three different upstream events. 502 Bad Gateway: the gateway got a response it could not use — the upstream closed the connection before or during the response (a crashed or restarting process, a keep-alive race, an out-of-memory kill), sent malformed headers, or reset the connection. 503 Service Unavailable: the gateway did not have a usable upstream to send to — every backend failed its health check, the pool was empty during a deploy, an overload circuit tripped, or the application itself returned 503 for maintenance or shed load. 504 Gateway Timeout: the gateway sent the request and waited the configured upstream timeout without getting a complete response — a slow database query, a lock, a thread pool that is full, an event loop blocked.

That mapping is a convention, not a law, and infrastructure behaviour varies by proxy, load balancer and CDN. nginx returns 502 when it has no live upstreams; an AWS ALB returns 503 for a target group with no healthy targets; some CDNs return 52x codes of their own for origin failures; Envoy annotates with response flags. Read your gateway’s documentation once and pin the mapping to a wiki page — then the codes become evidence rather than folklore.

Gateway 5xx — the usual upstream event (varies by proxy/LB/CDN)
StatusUsual upstream eventTypical root causesFirst check
502 Bad GatewayUpstream connected but the response was unusable: closed early, RST, malformedBackend crashed/restarting, OOM-killed, keep-alive idle race, header too large, wrong protocol on the portGateway error log: "upstream prematurely closed", "connection reset by peer"; backend restarts
503 Service UnavailableNo usable upstream, or upstream said soAll health checks failing, empty pool mid-deploy, overload shedding, maintenance mode, rate-limit circuitHealth-check state of the pool; deployment timeline; Retry-After header
504 Gateway TimeoutUpstream accepted but did not finish within the gateway’s timeoutSlow DB query, lock contention, exhausted worker pool, blocked event loop, an even slower downstreamGateway’s upstream_response_time vs its timeout; backend latency and DB slow-query log

Which hop generated the status?

A request may pass CDN → load balancer → ingress → sidecar → application, and any of them can generate a 5xx. Before any other question, identify the author. The Server header often names it (nginx, awselb/2.0, cloudflare, envoy). Via lists proxies that added themselves. The body is a fingerprint: nginx’s default 502 page, the ALB’s bare-text 502 Bad Gateway, a CDN’s branded error page, or your application’s JSON error envelope. CDN-specific headers (cf-ray, x-cache, x-served-by) tell you the CDN was reached; a request id header (x-request-id, x-amzn-requestid, x-amz-cf-id) is the thread you pull through every log downstream.

Timing is the second fingerprint. A 504 that arrives at exactly 60.0 s was generated by something with a 60 s timeout — nginx’s proxy_read_timeout default, the ALB’s default idle timeout — and the number identifies the hop. A 502 that arrives in 3 ms never reached a backend; one that arrives after 30 s of streaming did.

  • Server, Via, body format, CDN headers, request id → the hop that wrote the status.
  • Time-to-error equal to a known timeout → the hop that owns that timeout.
  • Application-generated 503 (your code, on purpose) vs gateway-generated 503 (no backends) look the same in a dashboard; the body and Server header separate them.

The procedure

Linux

Take the request id from one failed response and search the gateway’s access log for it: the line includes the upstream address chosen, upstream_status, upstream_response_time and upstream_connect_time (nginx variable names; ALB and Envoy have equivalents). That single line tells you whether an upstream was chosen, whether it connected, how long it took, and what it returned. Then the gateway’s error log for the same second: upstream prematurely closed connection while reading response header from upstream (502), connect() failed (111: Connection refused) while connecting to upstream (502 — nothing listening, see TCP Debugging: Reading the Handshake on the Wire), no live upstreams while connecting to upstream (502 in nginx; a 503 elsewhere), upstream timed out (110: Connection timed out) while reading response header from upstream (504).

Cross-check the health-check state at that moment — the load balancer’s target health history, the ingress controller’s endpoint list, kubectl get endpoints — because a 503 wall coincides almost always with a health check flipping. Then the backend: was it restarting (deploy, crash loop, OOM)? Was it slow (p99 latency, DB slow-query log, lock waits)? Load Balancers: L4 vs L7 explains how health checks decide pool membership and why a check that passes on / and fails on /health matters.

curl -sv -w timings from a machine that can reach both the gateway and the backend directly reproduce the split: if the backend answers directly in 40 ms but through the gateway in 60 s then 504, the gateway is not reaching the backend it thinks it is (wrong upstream address, security group between LB and instance, a stale endpoint).

nginx access + error log lines for the three statuses (log_format with upstream_* variables)
# 502: upstream closed the connection while nginx was reading the response header
10:14:02 GET /api/orders 502 rid=7c1a upstream=10.0.3.11:8080 up_status=502 up_connect=0.001 up_resp=0.004
10:14:02 [error] upstream prematurely closed connection while reading response header from upstream, client: 198.51.100.23, upstream: "http://10.0.3.11:8080/api/orders"

# 503 elsewhere (ALB / Envoy) for no healthy targets; nginx reports the same condition as 502:
10:31:55 [error] no live upstreams while connecting to upstream, client: 198.51.100.23, upstream: "http://orders-pool/api/orders"

# 504: nginx waited proxy_read_timeout (60 s) for the first byte and gave up
09:00:41 GET /api/reports 504 rid=9be0 upstream=10.0.3.12:8080 up_status=504 up_connect=0.001 up_resp=60.001
09:00:41 [error] upstream timed out (110: Connection timed out) while reading response header from upstream

The keep-alive race: the 502 that happens 0.3% of the time

Gateways keep idle connections to backends open and reuse them. The backend also has an idle timeout, after which it closes the connection. If the backend’s idle timeout is shorter than the gateway’s, there is a window in which the gateway picks an idle connection at the same instant the backend is closing it, sends the request into a closing socket, and gets a FIN or RST back — a 502 with upstream prematurely closed connection. Node’s default server.keepAliveTimeout is 5 s; an ALB’s default idle timeout is 60 s; the race fires on a small fraction of requests and looks like a flaky backend that is perfectly healthy.

The rule is: the backend’s keep-alive idle timeout must be longer than the gateway’s (and the backend’s headersTimeout longer still), so the gateway is always the one to close. Set Node to 65 s behind a 60 s ALB, or nginx’s keepalive_timeout above whatever fronts it; and configure the gateway to retry idempotent requests on a connection that failed before any bytes were received. Keep-Alive and Connection Reuse and Connection Pooling cover the mechanism; the 502 challenge in this module is the crash-loop variant.

  • Backend idle timeout > gateway idle timeout, always. Node: server.keepAliveTimeout = 65000; server.headersTimeout = 66000 behind a 60 s LB.
  • Sporadic 502s at low rate with a healthy backend and "prematurely closed" in the log = this race.

The timeout ladder must be ordered

Every hop has a timeout, and they must decrease as you go inward: client timeout > CDN timeout > load balancer timeout > application request timeout > database statement timeout. When the order is broken — a 30 s client with a 60 s gateway with a 120 s query — the client gives up and retries while the gateway is still waiting and the database is still working. The retry starts a second query; the client gives up again; the database now has three copies of the same slow query competing for the same lock. That is how a single slow endpoint becomes an outage: the retries amplify load exactly when capacity is gone.

Ordered timeouts fail inward-first: the statement timeout kills the query at 20 s, the application returns a clean 500 or 503 at 21 s, the gateway forwards it at 21 s, the client sees an honest error at 21 s and does not retry a request that is still running. The 504 disappears not because the backend got faster but because the failure is reported by the layer that owns it. Pair this with a retry budget and Retry-After on 503 so clients back off rather than pile on. The 504 challenge in this module walks a mis-ordered ladder.

Timeouts must shrink inward so the innermost layer fails first (illustrative numbers)
  1. Client / browser: 30 slongest; retries only if it never got a response
    ↓
  2. CDN / edge: 25 spasses through 5xx; returns its own on origin timeout
    ↓
  3. Load balancer / ingress: 20 s`proxy_read_timeout`, ALB idle timeout
    ↓
  4. Application request: 15 sserver-side deadline; cancels work and returns 503/500
    ↓
  5. Database statement: 10 s`statement_timeout`; the first thing to give up

Key points

  • 502 = upstream answered badly or closed; 503 = no usable upstream (or the app said so); 504 = upstream did not finish in time — usual meanings, varying by proxy, LB and CDN.
  • First find the hop that generated the status: Server/Via headers, body fingerprint, CDN headers, request id, and the time-to-error.
  • The gateway’s access log has upstream_status and upstream_response_time; its error log names the cause verbatim.
  • A 503 wall almost always coincides with health checks flipping; check the pool’s health history first.
  • Low-rate 502s on a healthy backend are usually the keep-alive race; backend idle timeout must exceed the gateway’s.
  • Order the timeouts to shrink inward, or retries amplify a slow endpoint into an outage.

Why does this exist?

Mechanisms are answers to constraints. Open each question before reading the answer.

▸Why three different gateway codes?

So the client can react differently: 503 with Retry-After is "come back later", 504 is "your request may have run — be careful retrying non-idempotent work", 502 is "something between us is broken". Collapsing them destroys that signal.

▸Why does the gateway have a timeout at all?

Without one, a hung backend holds a gateway connection and a client forever, and the gateway’s connection budget drains until it can serve nobody. The timeout converts one slow request into one failed request instead of a cascading stall.

▸Why must the backend close last in keep-alive?

Only the side that sends a request can lose it to a closing socket. If the gateway is always the closer, it never sends into a socket the other side is shutting.

How it fails

What the failure looks like from inside real software.
Reference · show
  • Treating 502/503/504 as one "server error" metric and paging on the sum, losing the difference between a crash loop, a health-check flip and a slow query.
  • Debugging a 503 in the application when it was generated by the load balancer because the health-check path started returning 401.
  • Node behind an ALB with default keep-alive timeouts, producing 502s at 0.2% that are blamed on "network flakiness" for months.
  • A 30 s client timeout in front of a 60 s gateway and a 120 s query: every slow request runs three times.
  • Reading nginx’s 502 for "no live upstreams" as a backend bug when it is the nginx equivalent of the ALB’s 503.
  • No request id propagated end to end, so a single failure cannot be found in the backend log among thousands of successes.

Check yourself

Answer out loud or on paper first, then compare. These come from the interview questions linked to this lesson.

  1. Your service sits behind a load balancer. Users report 502, 503 and 504 errors. What does each one mean, who generated it, and where do you look first for each?

    Show a strong answer

    All three are almost always written by the proxy in front of your service — the L7 balancer, ingress or CDN (Forward and Reverse Proxies, Load Balancers: L4 vs L7) — which is why the application logs often show nothing. They describe what happened on the proxy’s upstream connection. 502 Bad Gateway: the proxy connected (or tried to) and got something it cannot use — connection refused because no process is listening (crashed, restarting, wrong port), a reset because the backend closed an idle keep-alive connection the proxy was about to reuse (Keep-Alive and Connection Reuse), a TLS failure toward the upstream, or a malformed or oversized response header. First look: is the process up, did a deploy just happen, and do the proxy’s error logs say connect() failed, upstream prematurely closed, or upstream sent too big header.

    503 Service Unavailable: there is nothing to send to, or the backend refused to serve. From the proxy: no healthy backends — every target failed health checks, the target group is empty, all pods are not-ready during a rollout, the deployment scaled to zero. From the application itself: deliberate load shedding, a maintenance mode, an open circuit breaker, or a queue full — the one code of the three that a backend *should* emit on purpose, ideally with Retry-After. First look: the health-check dashboard and readiness state, then whether the 503 body or Server header is the proxy’s or yours.

    504 Gateway Timeout: the proxy sent the request and the backend did not answer within the proxy’s timeout — nginx proxy_read_timeout (60 s default), ALB idle timeout (60 s), Envoy route timeout (15 s). The backend is alive and probably still working on that request: a slow query, a lock, a downstream call without its own timeout, a thread pool exhausted so the request sat in a queue. First look: backend latency percentiles and saturation, not errors. The ugly part: the client got an error but the work may complete anyway, so a retry can double-charge; deadlines must propagate so the backend stops when the proxy gives up.

    The mappings are not standard across proxies, and you must know your own. nginx returns 502 for connection refused and 504 for a *connect* timeout; Envoy returns 503 for upstream connect failure (UF), no healthy hosts (UH) and overflow (UO) and 504 only for UT; AWS ALB returns 502 for reset/refused/malformed and 503 for a target group with no registered targets; Cloudflare uses its own 52x family — 521 origin refused, 522 connect timeout, 524 response timeout after 100 s, 525/526 TLS problems — so a "504" from an origin behind Cloudflare arrives at the user as 524. Reading the code without knowing which hop wrote it is guessing. See HTTP Debugging: 502, 503 and 504 Are Different Failures.

    Full interview question, with follow-ups and red flags →
  2. Requests to your API take 3 seconds at p99, but the server sits at 5% CPU and the handler itself measures 20 ms. Where can the other 2.98 seconds be, and how would you find them?

    Show a strong answer

    The handler’s 20 ms is measured from the moment the framework calls it — a request has a long life before and after that point, and none of it burns CPU. Lay out the timeline (Where the Time Goes: The Request Timeline): client DNS lookup → TCP handshake → TLS handshake → request bytes cross the network → the kernel’s accept queue → the application’s own queue (thread pool, event loop) → the handler → response bytes written to the socket buffer → transferred to the client → parsed. "Idle CPU plus slow requests" means the request is *waiting*, and each stage has a characteristic wait.

    Exact values are fingerprints. A p99 of ~1 s or ~3 s is the signature of a lost SYN: Linux retransmits at 1 s, then 3 s, then 7 s, so a handshake whose first SYN was dropped — by a full accept queue, a conntrack table at capacity, or a firewall — completes at exactly 1 or 3 s (The Three-Way Handshake, TCP Debugging: Reading the Handshake on the Wire). Check nstat -az TcpExtListenOverflows TcpExtListenDrops and ss -ltn for Recv-Q at the backlog limit. Steps of ~200 ms or multiples are retransmission timeouts on established connections (RTO_MIN, Packet Loss: Duplicate ACKs, Fast Retransmit and the RTO); ~40 ms per small write is the Nagle + delayed-ACK interaction; exactly 5 s is the glibc DNS resolver timeout — the first nameserver in resolv.conf is dead or drops UDP, or an AAAA lookup goes unanswered; 1–3 RTTs of extra latency on *every* request means no keep-alive, so each request re-runs the TCP and TLS handshakes (Keep-Alive and Connection Reuse).

    Queueing without CPU is the other big family. A thread pool of 32 workers all blocked on a downstream call with a 3 s timeout serves 20 ms handlers with a 3 s wait — the thread-pool queue length, not CPU, is the saturated resource (The Thread Pool Server). An event loop stalled on a synchronous call queues everything behind it. A slow client or a small socket buffer makes write() fast (it copies into the kernel) but the *transfer* slow, so server-side timing ends before the client has received anything; the LB then reports a long response time the app never saw (What Happens When the Receiver Is Slow). And on the client side, browsers queue requests beyond six per host — DevTools shows that as "Stalled" — and a proxy in between may buffer.

    Find it by measuring each hop, not the handler. From a client: curl -w "%{time_namelookup} %{time_connect} %{time_appconnect} %{time_starttransfer} %{time_total}" splits DNS, TCP, TLS and time-to-first-byte. At the balancer: ALB request_processing_time/target_processing_time/response_processing_time, or nginx $request_time versus $upstream_connect_time, $upstream_header_time, $upstream_response_time — the gap between request_time and upstream_response_time is client or network. On the server: ss -ti for retrans, rto and cwnd per connection, thread-pool and event-loop lag metrics, and a distributed-tracing span opened at the *ingress* rather than in the handler. Compare p50 to p99: a flat p50 with a spiky p99 at round values is a timer; a smeared p99 is a queue.

    Full interview question, with follow-ups and red flags →
  3. ALB logs show elb_status_code=502 and target_status_code=-. What does the dash tell you?

    Show a strong answer

    The target never returned a response: connection refused, reset, or closed before headers. The application did not see the request as an error — or at all.

    Full interview question, with follow-ups and red flags →