504 Gateway Timeout means: A gateway or proxy did not receive a timely response from the upstream server. The edge gave up waiting — the request may still be executing (or may have died) upstream, which makes blind retries of non-idempotent operations risky. Server-error 5xx codes mean the server failed while trying to fulfill a valid request. The fault lies with the service or its upstreams, not the caller. Some members are transient and retryable with backoff; others indicate bugs or configuration errors that only the operator can fix. In practice you will meet 504 when a slow query or external api call exceeds the gateway timeout; an upstream deadlock or resource starvation stalls processing; timeout misalignment: the lb waits 30 s while the app needs 60 s. On the client side, retry idempotent requests with backoff; for non-idempotent ones, check state first (the upstream may have completed the work). Report persistent timeouts to the operator. On the server side, align timeouts across hops (client > LB > app > DB), optimize the slow path, and move long work to async jobs returning 202 instead of blocking requests.
When 504 happens
- A slow query or external API call exceeds the gateway timeout
- An upstream deadlock or resource starvation stalls processing
- Timeout misalignment: the LB waits 30 s while the app needs 60 s
How to fix it
On the client
Retry idempotent requests with backoff; for non-idempotent ones, check state first (the upstream may have completed the work). Report persistent timeouts to the operator.
On the server
Align timeouts across hops (client > LB > app > DB), optimize the slow path, and move long work to async jobs returning 202 instead of blocking requests.
Should you retry?
Retry idempotent requests with backoff; for non-idempotent ones verify state first — the upstream may have completed the work.
Debugging steps
When 504 appears unexpectedly, start by capturing the full exchange: 'curl -i' or the browser DevTools Network tab shows the exact status line and response headers, which often carry the real explanation. Determine whether the error came from the edge or the origin: a proxy-generated error page looks different from your app's. Then trace upstream health, timeouts and recent deployments. For this code specifically, check whether the most common trigger applies to your situation: a slow query or external api call exceeds the gateway timeout. Compare your exchange against the raw HTTP example above. If you operate the server, correlate the timestamp with application and proxy logs using the request id; if you are the client, verify the request shape against the API documentation before changing anything or retrying.
Notes & gotchas
A 504 leaves the outcome unknown: the upstream may have completed the work after the gateway gave up. For payments and creates, verify state or use idempotency keys before retrying.
Examples
HTTP/1.1 504 Gateway Timeout
Content-Type: application/json
Content-Length: 110
{
"error": "gateway_timeout",
"message": "A slow query or external API call exceeds the gateway timeout"
} curl -i https://api.example.com/v1/resource # observe the 504 response and its headers const res = await fetch("https://api.example.com/v1/resource");
if (res.status === 504) {
// server-side failure: retry idempotent calls with backoff + jitter,
// surface a friendly error after the retry budget is exhausted
throw new TransientError(await res.text());
} import requests
res = requests.get("https://api.example.com/v1/resource", timeout=10)
if res.status_code == 504:
# server fault: back off and retry idempotent calls;
# log res.headers.get("X-Request-Id") for the operator
res.raise_for_status() Frequently asked questions
Did my request succeed if I got 504?
Unknown — the gateway stopped waiting, but the upstream may have finished afterwards. For payments/creates, query the resulting state before retrying to avoid duplicates.
How do I stop recurring 504s?
Find the slow upstream path (APM traces), then either speed it up, raise the whole timeout chain consistently, or convert it to an async job with a status endpoint.
Are 5xx errors safe to retry?
Idempotent requests (GET, PUT, DELETE) generally yes, with exponential backoff and jitter. Non-idempotent POSTs need care: a 504 may mean the work completed upstream. Check state or use idempotency keys before retrying.
Who should fix a 5xx — the caller or the provider?
The provider: 5xx asserts a server-side fault. Callers can only retry politely (backoff, idempotency) and report correlation ids; persistent 5xx need operator action on logs, upstreams or configuration.
Try the JSON Validator
Check JSON syntax and locate errors by line and column.