Skip to content

What's the difference?

502vs503vs504

The same request, three ways to fail. Watch which box lights up, and when.

  1. 502The app is dead. The request bounces straight back and the proxy lights up.
  2. 503The app is alive but full. The app itself lights up and says try later.
  3. 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

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

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.

Practise this in the DNS / HTTP track

Two-minute animated lessons, then boss fights from real outages. Free, no account needed.

Start the track