skip to content

Request Pipeline

The client's networking layer as interceptors that attach auth, refresh an expired token once for every waiting call, retry safely and cancel with the screen. Interviewers probe its edge cases.

on this pageshow

explore

questions

6

In a mobile payments app, how do you make the Send money call safe to retry on a flaky network?

level: middleimportance: must knowfreq 55%

answer

  1. a timeout is not a failure
  2. POST is not idempotent by itself
  3. the key is born when the user confirms
  4. one key per intent, reused on every retry
  5. 409 means still processing; 422 means misuse

basics

~20 s

Create one idempotency key on the device when the user confirms the transfer, send it in the Idempotency-Key header, and reuse that same key on every retry of that intent, automatic or manual. A new key per attempt can pay twice.

solid answer

~50 s

A transfer is a `POST`, which is not idempotent, and a timeout on a phone does not tell you whether the server applied it. So the client makes the intent recognisable: when the user taps *Confirm*, it generates a random key (a UUID is the usual choice) and stores it with that pending transfer. Every attempt, the pipeline's own retries and the user's *Try again*, sends the same key in an `Idempotency-Key` header with the same body; the server returns the first outcome instead of moving money again. Changing the amount or recipient is a new intent and gets a new key. The header comes from an IETF Internet-Draft, so the server documents its own rules: commonly `409` while the first attempt is still running (wait, then retry with the same key) and `422` if the key is reused with a different body, which is a client bug.

code

http · 7 lines
http
POST /v1/transfers HTTP/1.1
Host: api.example.com
Authorization: Bearer <access token>
Idempotency-Key: "8e03978e-40d5-43e8-bc93-6894a57f9324"
Content-Type: application/json

{"to": "contact-2817", "amount": "25.00", "currency": "EUR"}

go deeper

for a junior

Recall that a timeout leaves the outcome unknown and that a transfer can only be retried safely when every attempt carries the same idempotency key.

for a middle

Explain where the key is created and stored, which responses the transfer retries, and what 409 and 422 mean under the Idempotency-Key draft.

for a senior

Show the traps: keys minted per attempt, manual retries that build new requests, and edited amounts reusing an old key while the first attempt's outcome is still unknown.

for a principal

Set a team-wide rule that every non-idempotent call carries a caller-supplied intent key, and agree its expiry window with the server so client retries never outlive it.

## Why the transfer is the hard call Most calls in a wallet app are reads, and reads are easy: a `GET` is **safe** and **idempotent**, so repeating it changes nothing. The HTTP semantics name `PUT`, `DELETE` and the safe methods as idempotent and say a client **should not** automatically retry a non-idempotent request unless it has some means to know the request is actually idempotent, or that the original was never applied. Sending money is a `POST`. On a phone the dangerous moment is common: the request leaves, the train enters a tunnel, the response never arrives. The client knows only that it does not know. Retrying blindly can send the money twice; giving up can tell the user "failed" about a transfer that went through. ## The idempotency key, from the client's seat An **idempotency key** is a unique value the client generates so the server can recognise later attempts as retries of the same request. The `Idempotency-Key` header that carries it is defined in an IETF **Internet-Draft**, not yet an RFC, which recommends a UUID or similar random value. What the server stores and for how long is the server's business; the client owns four decisions: 1. **When the key is born.** At the moment of the user's intent, when they tap *Confirm*, not inside the retry layer and not per attempt. 2. **Where it lives.** With the pending transfer the app holds for that intent, so the automatic retry, a manual *Try again* and a double tap all find the same key. 3. **What it covers.** Exactly one payload. If the user edits the amount or picks another recipient, that is a new intent with a new key. 4. **When it dies.** When the outcome is known: success, or a definite rejection the user must act on. | Approach | Timeout, then retry | Result | |---|---|---| | No key, retry the `POST` | server sees two transfers | possible double payment | | New key per attempt | server sees two distinct requests | possible double payment | | One key per intent, reused | server sees the same request twice | one transfer, same answer twice | | No key, never retry | user taps again manually | same risk, moved to the user | ## Which responses the client retries Retrying is the pipeline's policy; the key only makes it safe for this call. A sensible rule set for the transfer: - **No response at all (timeout, connection dropped):** retry with the same key and body. - **`409 Conflict` with an idempotency problem body:** the draft says a server should answer this when the first request with that key is still being processed. Nothing is wrong with the request; wait and try again with the same key. - **`422`:** the draft's answer when a key is reused with a different payload. This is a bug in the client's key handling; log it and do not retry. - **`429` or `503`:** the server is shedding load; honour `Retry-After` when it is present. - **Other `4xx` such as `400` or `403`:** a definite answer; retrying the same request gets the same answer. - **A cap on attempts:** a small fixed number per intent, after which the transfer shows as *pending* rather than *failed*, because its outcome may still be unknown. The spacing between attempts (backoff and jitter) is its own subject; what this leaf decides is *whether* a given failure is retried and *which key* goes with it. ## Two traps that look harmless - **Generating the key in the retry interceptor.** It runs per attempt, so every attempt gets a new key and the server cannot match them. The key must arrive at the pipeline already attached to the request. - **A manual retry that builds a new request.** If *Try again* creates a fresh request object with a fresh key, the user's own retry becomes the double payment. The button must resubmit the same intent. ## Where this leaf stops The key protects retries **within this session**. Keys expire on the server by its own published policy, and a transfer that must survive the app being killed and relaunched belongs in a durable queue of pending writes stored with the data. Server-side handling of keys (storage, concurrency, retention) is the server's design. The client's job is narrower and absolute: one key per user intent, reused on every attempt of that intent.

  • In a mobile payments app, the user changes the amount after a transfer attempt timed out. Same key or a new one?
    A new key, because it is a new intent. The draft says a key must not be reused with a different payload, and a server following it answers `422`. But first resolve the old attempt: it may already have gone through, so look up its outcome with the original key before offering an edited transfer, or the user may end up paying both amounts.
  • A mobile app's transfer retry gets 409 with an idempotency problem body. What does that tell the client?
    That the server is still processing the first request with that key. The request is not wrong, so the client does not change it: it waits and retries with the same key and body, or polls the transfer's status, until the stored outcome comes back. Treating `409` as a failure would show *failed* for a transfer that is about to succeed.

  • A POST is safe to retry as long as the server responded with 5xx.
  • Generate a fresh idempotency key for each retry attempt so attempts stay distinct.
  • A timeout means the transfer failed, so tell the user it did not go through.
  • The Idempotency-Key header is a finished RFC that every server implements the same way.
  • Reuse the old key after the user edits the amount, since it is the same transfer screen.
open as a page

In a mobile wallet app, five in-flight calls all get 401 when the access token expires; how does the client refresh once and replay them?

level: middleimportance: must knowfreq 60%

basics

~20 s

Route every 401 through one shared refresh gate: the first caller starts the refresh, the rest wait for its result, then each replays once with the new token. A refresh failing with invalid_grant fails them all and signs out.

open as a page

In a mobile wallet app, why should the request pipeline refresh the access token on a 401 but not on a 403?

level: juniorimportance: should knowfreq 45%

basics

~20 s

A 401 says the credentials themselves were refused, usually an expired access token, so one refresh and one replay can fix it. A 403 says valid credentials lack permission; a refreshed token carries the same rights, so refreshing cannot help.

open as a page

In a mobile wallet app's networking layer, in what order should the idempotency-key, retry, auth and logging interceptors run, and why?

level: middleimportance: should knowfreq 35%

basics

~20 s

Outermost the idempotency key, so every attempt shares it; then retry; then auth, so each attempt carries the current token and handles its own 401; innermost logging, so it records each real attempt with secrets redacted.

open as a page

In a mobile wallet app, why do the transfer call, the balance read and an avatar prefetch need different timeouts, priorities and deduplication?

level: seniorimportance: should knowfreq 30%

basics

~20 s

Their failure costs differ. The transfer gets a generous deadline and is never deduplicated; the balance read gets a short deadline and shares one in-flight call; the prefetch is short, lowest priority and dropped on failure, so it never delays the user.

open as a page

In a mobile payments app, the user leaves the Send screen while the transfer request is in flight; what should the pipeline cancel, and what must it not?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Cancel the calls that only served the screen, such as recipient search, avatars and the fee quote. Keep the transfer running in a scope that outlives the screen: a sent request cannot be recalled, so resolve its outcome with the same idempotency key.

open as a page