Proxies, load balancers, CDNs and timeouts
Between the client and the application sit proxies, load balancers and CDNs, and every node has its own timeout, so the shortest one fires. A reverse proxy terminates TLS and routes, a load balancer spreads requests, a CDN caches responses. Codes 502, 503 and 504 are often returned by the proxy, and 504 means it did not wait for the upstream. A round duration such as 60 seconds points to a timeout.
Networking · HTTP
A request keeps failing with 504 at exactly 60 seconds. Is it an application or an infrastructure bug?
A report export works on a small dataset and fails on a large one. The error is always 504, the time is always 60.0 seconds. Random slowness does not look like that: a round number is almost always a coincidence with somebody's timeout. A direct connection between the browser and the application is rare, and every node on the way has its own rules and timeouts. The lesson explains what these nodes do, why each has its own timeout and how to guess from the symptom who caused it.
How it works and why. Each node between the client and the application exists because it solves its own task. Proxies and balancers protect the application, spread the load and terminate TLS, a CDN brings content closer to the user. The price is more places where a request can be delayed or cut.
The path of a request and a timeout at every node
- A forward proxy stands on the client side (a corporate exit to the internet, Charles, mitmproxy). A reverse proxy stands on the server side (nginx, Envoy): it terminates TLS, serves static files, routes to services, compresses responses.
- A load balancer: L4 works with TCP connections (fast, does not see HTTP), L7 understands HTTP and routes by host, path or headers. Algorithms: round-robin, least connections. A health check removes failing instances, "sticky" sessions pin a client to an instance.
- A CDN caches responses at the edge, closer to the user, and offloads the origin. Cache errors (a stale file, somebody else's personalised response) are cured with
Cache-Control,Vary, versions in the file name and invalidation. - Proxy headers:
X-Forwarded-For(the client IP),X-Forwarded-Proto, the standardisedForwarded. If the application does not trust the proxy, it sees the balancer IP, and the rate limit fires for everyone at once.
Timeouts. There is one at every step, and the smallest fires:
- connect timeout: how long to wait for TCP to be established;
- read/response timeout: how long to wait for data from the server;
- idle timeout on a balancer (often 60 s): a connection with no traffic is closed. It also "kills" long requests without an answer and WebSockets without ping;
- the client timeout (
fetchhas none of its own, you needAbortSignal.timeout(ms)), the test timeout and the CI step timeout.
How to get rid of the timeout of a long operation
A long operation in one request
The client waits a minute or more. Any node with a shorter timeout cuts the request, and the work on the server may go on without an answer.
202 Accepted and status polling
The request returns a job id at once, the client polls for status. No timeout becomes a limit on how long the work takes.
The code shows who answered:
| Code | Who returned it and why |
|---|---|
| 502 | the proxy got an invalid response or could not connect to the upstream: the app crashed or is restarting |
| 503 | no healthy backends or an overloaded service; the application itself may also return it |
| 504 | the proxy did not get an upstream response within its timeout |
Example: breaking down "exactly 60 seconds". First look at who returned the error and after how many seconds:
curl -sS -o /dev/null -D - -w "%{http_code} %{time_total}\n" \
https://api.example.com/reports/42
# HTTP/2 504
# server: nginx # the proxy answered, not the app
# 504 60.012 # exactly 60 s: matches a timeout
The server: nginx header and an HTML page instead of JSON say that the proxy answered, not the application. The number 60 matches the default nginx proxy_read_timeout and the default idle timeout of many cloud balancers. Check the proxy configuration:
location /reports/ {
proxy_pass http://app;
proxy_read_timeout 60s; # the nginx default
}
Conclusion: the application does not finish in 60 seconds, and the proxy cuts the wait. The right fix is an asynchronous model with 202 (see figure 2). The timeout may be raised temporarily and deliberately, but then every node with a shorter value changes, not one. Note also that a test with a 30 second timeout will fail earlier than the 504, with a different message, and without that difference the diagnosis comes out wrong.
Limits and alternatives. Not every "slow" is a timeout. If the time is different every time, it is overload or a dependency; if it is the same, it is a timeout. For WebSocket and SSE, ping or a heartbeat solves the idle timeout. X-Forwarded-For can be forged by the client itself, so the application must trust only headers from known proxies: check it with a test that sends its own X-Forwarded-For and sees whether the IP used for limits and logs changes. A CDN cannot tell that a response is personalised unless the application marks it with Cache-Control: private (or no-store) or Vary, so the "somebody else's personalised response" bug is often in the cache, not the application.
Common mistakes.
- Curing a 504 with a retry in a test: it masks the timeout instead of removing it.
- Raising the timeout on one node only: the next one, with a shorter value, fires.
- Treating 502 and 504 as an application error: they are returned by a proxy or a balancer, and the problem may be behind it.
- Not setting a timeout in the test client:
fetchwill wait until someone else cuts it. - Not accounting for the application seeing the balancer IP until trust in the proxy is configured.
Check yourself
A request returns 504 at exactly 60 seconds every time. Which two hypotheses do you check first?
Answer
A round number points at a timeout: I check proxy_read_timeout or its equivalent in the proxy and the balancer idle timeout, which are often 60 seconds by default. The second hypothesis: the application really does not finish in 60 s, so I look at processing time in its logs. The response body (nginx HTML instead of JSON) and the server header confirm that the proxy answered.
A test with a 30 second timeout fails, while curl with a 120 second timeout gets a 504 at 60. Why do the results differ and what is the right conclusion?
Answer
The test gave up first through its own timeout and did not wait for the 504, so its message differs. Curl reached the proxy timeout. The conclusion about the cause comes from curl: the test and the proxy have timeouts that must be compared, and the real problem is that the operation takes longer than 60 seconds.