DevDesk

GUIDE

HTTP status codes, explained properly

A status code is a contract between three parties: the client, the server and anything caching in between.

Most developers learn status codes as a list of numbers to return. The useful mental model is a contract: the code tells the client what happened, whether the request can be retried, and whether a cache may store the response. Get it wrong and clients retry the wrong things, caches store failures, and monitoring pages the wrong team.

Classes, not individual codes

The first digit is the category. A client that understands 4xx versus 5xx can behave sensibly even when it has never seen a particular code: a 4xx is the client's problem to fix, a 5xx is the server's. This is why inventing a 6xx code breaks intermediaries — they have no rule for it.

Redirects and methods

301 and 308 are permanent; 302 and 307 are temporary. The difference between 302 and 307 is method preservation: 307 tells the client to repeat the same method and body, which matters for POST. In practice many clients rewrite POST to GET on 302 anyway, so use 307 when that would be wrong.

Caching

200, 203, 204, 206 and 404 are cacheable by default in some contexts; 500 is not. 304 is not a failure — it means the cached copy is still valid and the body is intentionally empty. A monitoring check that treats 304 as an error will report false outages.

When to retry

  1. 429: honour Retry-After, then back off. Retrying immediately makes the limit worse.
  2. 503: the server is temporarily unable; retry with jitter and a cap.
  3. 502 / 504: the upstream failed or timed out. Retrying can help, but check whether the request is idempotent first.
  4. 500: may be transient, but a blind retry of a payment POST is how duplicate charges happen.

Proxy-shaped errors

502 and 504 rarely originate in your application. They come from a gateway that could not get a valid response from the origin: the process crashed, the port is wrong, a TLS handshake failed, or the upstream took longer than the gateway's timeout. When you see them, check the proxy logs before the application logs.

Codes that are usually a design smell

200 with an error body defeats every layer that inspects status. 400 for everything hides which field was wrong. 401 for permission errors sends clients into a login loop. 302 for a deleted resource makes a temporary condition permanent in caches.

Use the searchable status table for a specific code, or the JSON formatter to inspect the error payload that came with it.