← Back to Chapters

504 Gateway Timeout

⏱️ 504 Gateway Timeout

? Quick Overview

The 504 Gateway Timeout status code indicates that a server acting as a gateway or proxy did not receive a timely response from an upstream server. This error commonly appears in distributed systems where multiple servers communicate with each other.

? Key Concepts

  • Occurs in gateway or proxy-based architectures
  • The request reached the gateway successfully
  • The upstream server failed to respond in time
  • Often related to network latency or server overload

? Syntax / Theory

HTTP status code 504 belongs to the 5xx category, which represents server-side errors. The gateway waits for a response within a configured timeout window. If the upstream server exceeds this duration, the gateway aborts the request and returns 504.

? Code Example(s)

? View Code Example
// Example HTTP response showing a 504 status code
HTTP/1.1 504 Gateway Timeout
Content-Type: text/html
Connection: close
? View Code Example
// Node.js Express example simulating a delayed upstream service
app.get("/api/data", (req, res) => {
setTimeout(() => {
res.send("Response from upstream service");
}, 10000);
});

? Live Output / Explanation

What Happens?

If the gateway timeout is configured to 5 seconds and the upstream service responds after 10 seconds, the client receives a 504 Gateway Timeout instead of the actual data.

? Interactive Example / Diagram

Client Gateway Upstream Upstream delay causes timeout

? Interactive Simulator

Adjust the sliders to see how the timeout limit affects the request outcome. If the upstream response takes longer than the timeout limit, a 504 occurs.

Ready to start...
 
0s Timeout Limit

? Use Cases

  • Microservices architecture with API gateways
  • Reverse proxies like Nginx or Apache
  • Cloud load balancers
  • Third-party API integrations

✅ Tips & Best Practices

  • Optimize upstream server performance
  • Increase gateway timeout limits carefully
  • Implement caching for slow responses
  • Monitor network latency and server health

? Try It Yourself

  1. Create a server route with an intentional delay
  2. Configure a short timeout in a proxy
  3. Trigger the request and observe the 504 error
  4. Adjust timeout values and test again