server error RFC 9110

HTTP 504 Gateway Timeout

A gateway or proxy timed out waiting for a response from the upstream server.

What it means

The proxy successfully forwarded the request but the upstream took too long to respond and hit the proxy's timeout. Different from 503 (server refusing) and 502 (bad response) — 504 is specifically "waited, gave up". CloudFront's default is 30 seconds; API Gateway is 29 seconds; Cloudflare Free is 100 seconds.

When servers send it

  • Upstream taking longer than the proxy timeout
  • Long-running query or computation blocking the response
  • Downstream API dependency slow to respond

What the client does

Retries after a delay. Retries safe on idempotent methods only.

Common causes

  • Slow database query
  • External API call inside the request handler taking too long
  • Deadlock or long-running lock
  • CPU-bound work in the request path

How to fix it

  • 1.Move long work off the request path (background job + polling)
  • 2.Add database indexes to speed up slow queries
  • 3.Set client-side timeouts LOWER than server timeouts to fail fast
  • 4.Return 202 Accepted immediately for long operations and poll for result

Similar codes

← All HTTP status codes · HTTP status cheat sheet · HTTP headers reference