APIs
Status codes: multiple-choice review
Six scenarios, each one a status code you choose under real conditions. Not “what does 404 mean” — but which code makes the caches, dashboards, and retry loops downstream do the right thing.
Confirm you can pick the correct status for a given situation and defend it — connecting the 2xx nuances, the 4xx vocabulary, the 4xx/5xx ownership split, and the retry/idempotency rules the unit built toward.
A POST /orders endpoint validates the order, writes it to the DB synchronously, and the resource now exists. What status, and what else must the response carry?
An endpoint accepts a request, enqueues it to Kafka, and returns immediately; a worker processes it seconds later. What status is honest?
A request arrives with valid JSON, all required fields present and well-typed, but quantity is -3 — a business-rule violation. Which class and code fit best?
A reverse proxy forwards a request to an upstream service, which is up but returns a garbled, truncated response the proxy cannot parse. What status does the proxy return to the client?
A client is rate-limited and the server returns 429 with `Retry-After: 30`. The client also runs generic exponential backoff with jitter. What should it do?
A non-idempotent POST /charge times out as 504 with no idempotency key sent. The charge may or may not have run. What is the correct client behaviour?
The through-line: the first digit is a machine instruction, and the precise sub-code carries real contract information. 2xx splits into 201 (+Location) for a synchronous create, 202 for accepted-but-async, 204 for deliberately empty success. The 4xx vocabulary tells the client what to fix — 400 (syntax) vs 422 (values) vs 409 (state). The 5xx sub-codes attribute blame — 500 your bug, 502 a bad upstream, 503 intentional unavailability, 504 a timeout where the work may secretly have completed. Retry 5xx and 429, honour Retry-After before backing off, and never blindly retry a non-idempotent request after a timeout — reach for an idempotency key instead.