What's the difference?
502vs503vs504
The same request, three ways to fail. Watch which box lights up, and when.
- 502The app is dead. The request bounces straight back and the proxy lights up.
- 503The app is alive but full. The app itself lights up and says try later.
- 504The app is still working. The proxy's timer fills, then the proxy lights up. The late 200 is thrown away.
How to tell them apart in 10 seconds
| Clue | 502 | 503 | 504 |
|---|---|---|---|
| Who sends it | Typically the proxy, load balancer or CDN | Typically the app, or a load balancer with no healthy backend | Typically the proxy, load balancer or CDN |
| How long it takes | Milliseconds | Milliseconds | Exactly the proxy's timeout, such as 60.0 s |
| What the app is doing | Down, restarting or just crashed | Running, and refusing new work | Running, and still busy with this request |
| What's in the logs | Proxy: “connection refused” or “prematurely closed”. App: nothing | App or limiter logs the refusal. Often a Retry-After header | Proxy: “upstream timed out”. App: the request finishes later |
| Retry? | Yes with backoff, if the request is idempotent | Yes, after Retry-After or a backoff | Reads yes. Writes only with an idempotency key |
Who sends it
- 502
- Typically the proxy, load balancer or CDN
- 503
- Typically the app, or a load balancer with no healthy backend
- 504
- Typically the proxy, load balancer or CDN
How long it takes
- 502
- Milliseconds
- 503
- Milliseconds
- 504
- Exactly the proxy's timeout, such as 60.0 s
What the app is doing
- 502
- Down, restarting or just crashed
- 503
- Running, and refusing new work
- 504
- Running, and still busy with this request
What's in the logs
- 502
- Proxy: “connection refused” or “prematurely closed”. App: nothing
- 503
- App or limiter logs the refusal. Often a Retry-After header
- 504
- Proxy: “upstream timed out”. App: the request finishes later
Retry?
- 502
- Yes with backoff, if the request is idempotent
- 503
- Yes, after Retry-After or a backoff
- 504
- Reads yes. Writes only with an idempotency key
In short
502 vs 503 vs 504: the difference
502: the proxy got a broken answer. 503: something said “not now”. 504: the proxy got no answer in time.
All three codes mean the request was fine and the server side was not, but they describe different failures. A 502 comes from a proxy that reached for the application and got something unusable straight away: a refused connection, a dropped one, or garbage. A 503 is a deliberate refusal by something that is alive, usually the application shedding load or a load balancer with no healthy backend, and it can say when to come back. A 504 comes from a proxy whose timer ran out while the application was still working. Timing separates them fastest. A 502 and a 503 fail in milliseconds; a 504 takes exactly as long as the proxy's timeout. Then ask who wrote the response. If the application has a log line for the request, it was probably a 503. If only the proxy does, it was a 502 or a 504.
Your turn
Which one is this?
Which one is this? Errors start at 14:02. Every failed request takes 30.0 seconds and shows the load balancer's error page. App CPU is normal, and the database shows one long-held lock.
Each code in depth
- 502Bad GatewayA proxy passed your request on and got back something that was not a valid response.
- Browser
- CDN
- Proxy
- App
- DB
- 503Service UnavailableThe server is up but is refusing work for now, because it is overloaded or in maintenance.
- Browser
- CDN
- Proxy
- App
- DB
- 504Gateway TimeoutA proxy passed your request on, waited, and gave up before the server behind it answered.
- Browser
- CDN
- Proxy
- App
- DB
Questions
- What is the difference between 502 and 504?
- Both are sent by a proxy. A 502 means it got an invalid or no response from the server behind it straight away. A 504 means it waited for its full timeout and heard nothing. A 502 is fast; a 504 is as slow as the timeout.
- What is the difference between 502 and 503?
- A 502 is an accident reported by a proxy: the server behind it was broken or gone. A 503 is a decision made by a server that is running but will not take the request right now, because of overload or maintenance.
- Which of 502, 503 and 504 is safe to retry?
- A 503 is the safest, after waiting for Retry-After or a backoff, because the work was not started. A 502 is usually safe for idempotent requests. A 504 is the risky one: the server may still complete the request, so only retry writes that carry an idempotency key.
Sources: RFC 9110: 502 Bad Gateway (section 15.6.3) ↗ · RFC 9110: 503 Service Unavailable (section 15.6.4) ↗ · RFC 9110: 504 Gateway Timeout (section 15.6.5) ↗ · nginx: ngx_http_proxy_module ↗ · Report a problem
Diagrams are our own simulations. All wording is original and paraphrases the linked sources.
Practise this in the DNS / HTTP track
Two-minute animated lessons, then boss fights from real outages. Free, no account needed.