Skip to content

    Frontend System Design

    HTTP Methods, Status Codes and Headers a Front End Relies On

    Learn what HTTP methods and status codes promise a front end: safe and idempotent methods, redirects, 401 versus 403, 412 conflicts and when a retry is safe.

    By Published 12 min read

    • frontend-system-design
    • http
    • status-codes
    • idempotency
    Share

    Standard lessonbeginnerTypeScript labFrontend System Design

    Before this: DNS, TCP, TLS and the Request Lifecycle

    What you'll learn: By the end of this lesson you will be able to say what safe, idempotent and cacheable mean for a request method and why a front end cares, decide whether a request may be repeated after a lost response, read a status code by its class even when you have never seen it, tell the redirects that keep a method from those that turn a POST into a GET, separate 401, 403, 409, 412 and 422, and use ETag with If-Match to stop two tabs overwriting each other.

    Ask a shop assistant "do you have this in blue?" and nothing changes, however often you ask. Say "put one aside for me" and saying it twice still means one item aside. Say "send me another box" and every repeat sends another. HTTP methods are those three kinds of sentence, and a front end that cannot tell them apart will sooner or later send the box twice.

    The running example is the storefront of a grocery marketplace (the front end of the ShopStream marketplace used in this course): an order page where a shopper edits a delivery note or cancels, and a checkout that places orders. Calls and hosts are invented.

    The core idea

    1. A method is a promise about meaning. GET promises to read; PUT and DELETE promise that repeating them changes nothing more; POST promises nothing of the kind.
    2. A status code is a promise about outcome, and its first digit is the part you must always understand.
    3. The promises let caches, proxies, browsers and your data layer act without asking. A front end that ignores them re-invents each rule badly.

    How requests travel is in DNS, TCP, TLS and the Request Lifecycle; the cache rules for GET responses are in HTTP Caching for the Front End.

    Methods and what they promise

    RFC 9110 defines three properties. A method is safe when its defined semantics are essentially read-only: the client does not request, and does not expect, a state change. It is idempotent when the intended effect of several identical requests is the same as that of one. A method is cacheable when the specification lets a cache store its response.

    Method Safe Idempotent Cacheable Typical use
    GET yes yes yes Read an order
    HEAD yes yes yes Read the headers only
    OPTIONS yes yes no Ask what is allowed (CORS preflight)
    PUT no yes no Replace an order
    DELETE no yes no Cancel an order
    POST no no only with explicit freshness and Content-Location Create an order
    PATCH no no likewise Change part of an order

    PATCH comes from RFC 5789, which says it is neither safe nor idempotent, though it can be issued in a way that makes it idempotent. RFC 9110 adds that the overwhelming majority of caches only support GET and HEAD.

    Three details bite front ends.

    • Safe does not mean "no side effects at all." A server may log every GET; safe means the client did not ask for, and cannot be blamed for, any change. A GET link that deletes something breaks prefetching and crawlers, and RFC 9110 makes the resource owner responsible.
    • Idempotent describes the effect, not the response. Repeating a DELETE may return 204 the first time and 404 the next. The state is the same; the status is not.
    • Method names are case-sensitive. fetch upper-cases only six of them (DELETE, GET, HEAD, OPTIONS, POST, PUT), and the Fetch standard itself warns that patch is likely to get a 405.

    Illustration 1: the minimal example

    Run in Node (the patch line also prints a warning about the lowercase name on stderr):

    for (const method of ["get", "put", "patch"]) {
      console.log(method.padEnd(6), "->", new Request("https://shop.example/orders/1", { method }).method);
    }
    try {
      new Request("https://shop.example/orders", { method: "GET", body: "{}" });
    } catch (error) {
      console.log(`${error.name}: ${error.message}`);
    }
    console.log("204 ok:", new Response(null, { status: 204 }).ok, " 304 ok:", new Response(null, { status: 304 }).ok, " 412 ok:", new Response("x", { status: 412 }).ok);

    Output, from running it in Node 26.13.1:

    get    -> GET
    put    -> PUT
    patch  -> patch
    TypeError: Request with GET/HEAD method cannot have body.
    204 ok: true  304 ok: false  412 ok: false

    Lowercase get and put were repaired, patch was not. This runtime refuses a body on a GET; RFC 9110 says such content has no generally defined meaning. And Response.ok is true only for 200 to 299, so a 304 is not "ok" though it is no error.

    May I send it again?

    A connection can fail after the request left and before the answer arrived, and the client cannot know whether the server acted. RFC 9110 says an idempotent request can then be repeated automatically, and that a client should not automatically retry a non-idempotent method unless it knows the request is actually idempotent or can detect that the original was never applied. A proxy must not retry non-idempotent requests, and a client should not retry a failed retry.

    Figure 1. Deciding whether to resend

    flowchart TD accTitle: Deciding whether a client may resend a request automatically accDescr: A decision tree. If the method is idempotent, an automatic resend is allowed, once. If it is not, the client checks whether the request carries a key the server uses to detect repeats. With a key, resend with the same key. Without a key, do not resend automatically and let the user decide. A{"Is the method
    idempotent?"} A -->|"yes: GET, PUT, DELETE..."| B["Resend is allowed
    (limit attempts)"] A -->|"no: POST, PATCH"| C{"Does the request carry
    a key the server uses
    to detect repeats?"} C -->|"yes"| D["Resend with the same key"] C -->|"no"| E["Do not resend automatically
    ask the user"]

    How to read this: the method alone settles the common case. A non-idempotent request becomes safe to repeat only when something else makes a repeat harmless, here a key the server remembers.

    The key is an application convention, not part of RFC 9110. The IETF has an Internet-Draft for an Idempotency-Key header (revision 09 when this lesson was written); it is a draft, not a standard, so check what your own API documents. Backoff and timeouts are in Request Cancellation, Retries and Timeouts.

    Illustration 2: a lost answer, run in the model

    The lab for this lesson is a small in-memory orders server that follows these rules. In each scenario the first request reaches the server, the answer is lost, and the client sends the same request again.

    Expected output, captured from a real run (the last five lines of the lab's demo):

    DELETE /orders/1: first=network-error, retry=404, orders stored=0 []
    PUT    /orders/1: first=network-error, retry=204, orders stored=1 ["x"]
    POST   /orders (no key): first=network-error, retry=201, orders stored=3 ["seed","2 x tea","2 x tea"]
    POST   /orders (key)   : first=network-error, retry=201, orders stored=2 ["seed","2 x tea"]
    may a client resend automatically? DELETE: yes, POST: no, POST with key: yes

    The repeated DELETE came back 404, but the end state is the one the shopper wanted, and the repeated PUT left one order with the same content. The POST without a key stored the order twice; with a key the second request got the stored answer and created nothing. The model says nothing about how long a real server keeps keys.

    Status codes: read the class first

    The first digit is the class: 1xx informational, 2xx successful, 3xx redirection (more action needed), 4xx client error, 5xx server error. RFC 9110 says a client must understand the class of any status code and treat an unrecognised one as the x00 code of its class: a 471 is handled as a 400. A code outside 100 to 599 is invalid and is processed as a 5xx. A data layer can therefore keep a table of the codes it has special handling for and fall back on the class for the rest.

    Figure 2. Where each kind of status is handled

    Status handling: classify in the data layer, react in the layer that owns the reactionResponse to Data layer; Data layer to Redirects; Data layer to Session (401); Data layer to Form (4xx); Data layer to Retry policy (transient)Response: status + headersResponsestatus + headersData layer: class, then tableData layerclass, then tableRedirects: 3xx: the browser followsRedirects3xx: the browserfollowsSession: 401: sign in againSession401: sign inagainForm: 400, 409, 412, 422Form400, 409, 412,422Retry policy: 429, 503, 5xxRetry policy429, 503, 5xx4014xxtransient

    How to read this: one place turns a status into a decision, and each decision goes to the part of the app that owns it. The view never compares numbers with 404.

    The codes a storefront meets most often, with what RFC 9110 and RFC 6585 say about them:

    Status Meaning What the front end does
    200, 204 Done; 204 has no body Use the body, or just report success
    201 Created; a Location header identifies the new resource Read Location, go to the new order
    304 You already hold a valid copy (conditional request) Use the stored copy
    400, 422 Malformed request; well-formed but unprocessable Show the problem, let the user fix it
    401 No valid credentials; must carry a WWW-Authenticate challenge Sign in again, then repeat
    403 Understood and refused Stop; do not repeat with the same credentials
    404 No current representation, or not disclosed Show not found
    409 Conflict with the resource's current state Let the user resolve it and resubmit
    412 A precondition in your headers was false Reload, merge, resubmit
    429 Too many requests; may carry Retry-After Slow down
    503 Temporarily unable; may carry Retry-After Wait, then retry if allowed
    504 A gateway got no timely answer upstream Treat as failed; the work may have happened

    Two traps. 401 and 403 differ: a 401 invites new credentials; a 403 says more of the same will not help, and a server may even answer 404 to hide that a resource exists. And 404 is heuristically cacheable (so are 200, 301, 308, 405 and 410), while a 429 must never be stored by a cache. That is why HTTP Caching for the Front End keeps long lifetimes off error responses.

    Retry-After is either a number of seconds (Retry-After: 120) or an HTTP date, and RFC 9110 defines it for 503 and for 3xx responses; RFC 6585 allows it on 429.

    Redirects and the method

    A redirect carries a Location, and the browser follows it by default. What it sends next depends on the status, because the specifications kept the habits of old browsers. RFC 9110 says that after 301 or 302 a client may change the method from POST to GET, and offers 308 and 307 as the variants that must not. The Fetch standard says a 301 or 302 after a POST, and a 303 after anything but GET or HEAD, becomes a GET with no body.

    Expected output, captured from a real run (the lab applies that rule):

    method   301     302     303     307     308
    GET      GET     GET     GET     GET     GET
    POST     GET     GET     GET     POST    POST
    PUT      PUT     PUT     GET     PUT     PUT
    DELETE   DELETE  DELETE  GET     DELETE  DELETE

    A 303 is the deliberate form: after a POST it says "fetch the answer with GET". The accident is a 301 or 302 put in front of an API, which silently turns the write into a read.

    Conditional requests: stopping a lost update

    Two tabs load the same order, and each saves a change. The second PUT replaces the first without knowing it was there. The standard fix is a validator. The server sends an ETag with the representation, and the client sends it back in If-Match. RFC 9110 says If-Match is most often used with state-changing methods to prevent the "lost update" problem, and that a failed precondition gets 412. (The same validator, sent in If-None-Match on a GET, is what produces the 304 of the caching lesson.)

    Figure 3. The second save is refused

    sequenceDiagram accTitle: If-Match refuses a stale update accDescr: Tab A and tab B both read the order and receive ETag v1. Tab A saves with If-Match v1 and receives 204 with ETag v2. Tab B saves with If-Match v1 and receives 412 Precondition Failed. Tab B reloads, shows the difference and saves again. participant A as Tab A participant B as Tab B participant S as Server A->>S: GET /orders/1 S-->>A: 200, ETag v1 B->>S: GET /orders/1 S-->>B: 200, ETag v1 A->>S: PUT with If-Match v1 S-->>A: 204, ETag v2 B->>S: PUT with If-Match v1 S-->>B: 412 Precondition Failed Note over B: reload, show the difference, save again

    How to read this: the header turns "last writer wins silently" into "the second writer is told".

    Expected output, captured from a real run (scenario 4 of the lab's demo):

    without If-Match:
      tab A PUT "2 x tea, 1 x honey" -> 204
      tab B PUT "2 x tea, 1 x milk" -> 204
      stored order: "2 x tea, 1 x milk"
    with If-Match:
      tab A PUT "2 x tea, 1 x honey" If-Match "v1" -> 204
      tab B PUT "2 x tea, 1 x milk" If-Match "v1" -> 412
      stored order: "2 x tea, 1 x honey"

    Without the header the honey is gone and nobody was told; with it, the second tab gets a 412. The ETags are the lab's own; a real server builds them as it likes.

    Cross-origin calls add rules the browser enforces before your code sees a status; those are in CORS, Preflight and Credentials Debugging.

    Illustration 3: a failure that looks like success

    A gateway starts answering POST /orders with 301 to /orders/. The browser repeats it as a GET, which returns 200 and the order list. Code that tests only response.ok shows "Order placed" and no order exists. For a create, treat only 201 with a Location as success.

    Trade-offs

    Choice Gain Cost
    Retry only idempotent requests No duplicate writes A failed POST needs the user or a key
    Idempotency key on POST Safe automatic retry The server must store keys; a key discipline in every client
    If-Match on every PUT No silent overwrites A 412 flow to design, and ETags to keep

    Build it

    The lab is examples/frontend/labs/http-semantics-model (22 tests, strict TypeScript, no dependencies beyond Vitest). It holds the method table, the status rules and the redirect rule as pure functions, and the in-memory orders server used above.

    cd examples/frontend/labs/http-semantics-model
    ../../node_modules/.bin/vitest run
    node src/demo.ts

    Verified. All 22 tests passed and the demo ran twice with identical output before it was stored as examples/frontend/golden/http-semantics-model.txt. The lab is a model; it observed no real browser, proxy or server.

    In practice: a checkout that created two orders

    NOTE

    This is a realistic scenario, not a case history. It is a composite built from common production patterns. It describes no specific company or product, and every number in it is an illustrative assumption.

    The situation

    Some shoppers on mobile networks get two identical orders. The checkout sends POST /orders; a shared wrapper retries any network error up to three times.

    Starting architecture

    Figure 4. A retry that does not look at the method

    Starting architecture: a wrapper that retries every network errorCheckout page to Wrapper; Wrapper to Orders API (same POST again)Checkout page: Place orderCheckout pagePlace orderWrapper: retries errorsWrapperretries errorsOrders API: POST /ordersOrders APIPOST /orderssame POST again

    How to read this: the warning box repeats a request whose first copy may have succeeded.

    Options considered

    Option What it fixes Cost or risk
    Never retry POST No duplicates The user presses again anyway
    One key per checkout attempt, reused on every retry Automatic retry is safe The server must store keys
    Server-side duplicate detection on cart contents No client change Legitimate identical orders may merge

    The decision

    Field Decision record
    Context Retries help on mobile networks, but the wrapper ignores what the method promised.
    Decision The wrapper retries only idempotent methods. Checkout creates one key per attempt, sends it on every try, and the API stores the answer under it.
    Consequences, good Retries stay on; a repeat cannot create a duplicate.
    Consequences, bad A new contract with the API, and a key lifetime to agree.
    Revisit when The API standardises its key header.

    Rollout and verification

    1. Write the contract down: which requests carry a key, and what a repeat returns.
    2. Test with a lost reply, as in the lab.
    3. Check in the network panel that every retry of one checkout carries the same key.
    4. Watch the duplicate-order count for a week.

    The 2 a.m. failure

    After a gateway update, shoppers see "Order placed" but find no order. The gateway now answers POST /orders with 301, the browser repeats it as a GET, and the page, which tests only response.ok, calls that success.

    First three checks: (1) in the network panel look for a 301 before the request and see what method the second request used; (2) compare the status the page treats as success with the 201 a create returns; (3) see which gateway rule changed. Mitigation: serve the path without the redirect, or answer 308, which keeps the method. Follow-up: require 201 and a Location for a create, with a test.

    Architect's notes

    • Learn the three properties by heart: most retry, prefetch and cache decisions follow from them.
    • Interpret statuses in one place and fall back on the class for any code you do not know.
    • Treat the redirect status as part of an API's contract.
    • Prefer a conditional write to hoping tabs do not collide.

    Common mistakes

    • Using GET for an action such as "delete", which crawlers and prefetchers will run.
    • Retrying a POST automatically with no key.
    • Treating fetch resolving as success. A 404 or 503 resolves.
    • Handling 401 and 403 alike, or looping on a 403 with the same credentials.
    • Redirecting an API with 301 or 302 and losing the method.

    Key takeaways

    • Safe, idempotent and cacheable decide what may be prefetched, cached or repeated.
    • An idempotent request may be repeated after a lost response; a POST only with something that makes a repeat harmless.
    • Know the class of every status code; treat an unknown one as the x00 of its class.
    • 301 and 302 may turn a POST into a GET; 307 and 308 keep the method.
    • 401 asks for credentials, 403 refuses, 409 is a conflict, 412 a failed precondition.
    • ETag with If-Match turns a silent overwrite into a 412.

    Practice exercises

    Exercise 1: Starter (30 min). Predict, then check in Node, what new Request(url, { method: "delete" }).method and { method: "options" } give.

    Exercise 2: Intermediate (2 hours). In the lab, add Retry-After to the 503 and 429 answers and make reactionFor return the wait, with tests for the seconds and the date form.

    Exercise 3: Advanced (half day). For your own data layer, list per status the reaction, whether it is transient and which layer owns it. Mark every row that depends on the method.

    In this course:

    Official documentation

    Ask about this article

    Get answers grounded in this post. AI-generated — based on this article, and may be imperfect.

    Scaled AI Weekly

    Enjoyed this? Get more like it every Monday.

    Real architecture decisions, LLMOps patterns that survive production, and engineering leadership advice — from 12+ years of building at enterprise scale. Free. No spam. Unsubscribe anytime.

    Get new articles by email

    Free. We email a confirmation link first, and you can unsubscribe from any email.

    Join engineers building production AI systems

    Comments