504

Gateway Timeout

5xx Server Error

ELI5

I'm a middleman server, and the server I was trying to talk to took too long to respond, so I gave up waiting.

Server perspective

Use 504 when acting as a gateway or proxy and the upstream server did not respond before your timeout expired.

When to use

  • Return 504 when a backend or origin takes too long and the intermediary times out waiting
  • Use it only when this server depends on an upstream response to finish the request
  • Upstream server response timeout
  • Load balancer timeout waiting for backend
  • API gateway timeout
  • Proxy timeout to origin server

How to respond

  • Set and monitor realistic upstream deadlines, then log which dependency timed out and after how long
  • Return a clear timeout response instead of leaving the client hanging on an open connection

Headers to consider

  • No status-specific header is required; include request or trace metadata when it helps operators correlate the upstream timeout

Response body

  • A body is optional but helpful; explain that an upstream dependency timed out and include safe retry guidance when appropriate

Server-side pitfalls

  • Do not use 504 for client-to-server request timeouts; that is 408 Request Timeout
  • Do not use 504 for malformed upstream responses; that is 502 Bad Gateway
  • Client request timeout (use 408 Request Timeout)
  • Bad upstream responses (use 502 Bad Gateway)
  • Service unavailable (use 503 Service Unavailable)

Examples

Backend API timeout

Request:GET https://api.example.test/api/slow-operation
Response:504 Gateway Timeout # Headers Content-Type: application/json # Body { "error": "gateway_timeout" }

Backend API took longer than 30 seconds, proxy timed out

References

Related Status Codes