In a mobile wallet app's networking layer, in what order should the idempotency-key, retry, auth and logging interceptors run, and why?
answer
- outer runs once, inner runs per attempt
- the key must sit outside the retry
- each attempt reads the current token
- 401 belongs to auth, not to retry
- log the wire, redact the secret
basics
~20 sOutermost 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.
solid answer
~40 sAn interceptor chain wraps the call in layers: an outer layer runs once per logical call, an inner layer once per attempt. So the idempotency-key layer goes outside retry: the key is attached once and every attempt reuses it. Retry sits next and decides which failures repeat. Auth goes inside retry, so each attempt reads the token current at that moment, and it owns the `401`-refresh-replay itself; if retry also treated `401` as retryable, refreshes would multiply. Auth also attaches the token only to the app's own API hosts, never to a third-party image or map host. Logging goes innermost, where it sees the exact headers of each attempt, and must redact `Authorization`. A whole-call deadline, if the app has one, wraps the retry layer so the attempts share one time budget.
code
pseudocode · 10 lineschain = [
callDeadline(budget = classOf(request).deadline), # once per call
requireIdempotencyKey(forWrites = true), # once per call
retry(maxAttempts = 3, retryOn = [NO_RESPONSE, 429, 503, 409_IDEMPOTENCY]),
auth(attachTo = OWN_API_HOSTS, onUnauthorized = singleFlightRefreshThenReplayOnce),
wireLog(redact = ["Authorization", "refresh_token"]), # per attempt
]
function execute(request):
return chain.proceed(request) # outer layers run first on the way out, last on the way backgo deeper
Recall that interceptors wrap a call in layers and that a layer outside the retry runs once, while a layer inside it runs on each attempt.
Explain the order key, retry, auth, logging, and what breaks when the key or the token sits on the wrong side of the retry layer.
Show the production details: 401 owned by auth not retry, tokens attached only to your own hosts, redacted per-attempt logs, and a shared deadline over all attempts.
Treat chain order as a contract of the shared networking module, with one assembly point and a test, so teams adding layers cannot reorder it by accident.
## What an interceptor chain is Most HTTP clients let an app wrap every call in a chain of **interceptors**: small layers that can read or change the outgoing request, pass it inward, and read or change the response on its way back. The order matters because of one simple property: **a layer outside the retry layer runs once per logical call; a layer inside it runs once per attempt.** Every placement decision below follows from asking "should this happen once, or on every attempt?" For a wallet app the chain usually holds at least these concerns: stamping the **idempotency key** on writes, **retrying** failed attempts, attaching the **access token** and handling `401`, and **logging** for diagnostics. ## The order, outside to inside 1. **Call deadline (optional).** A whole-call time budget wraps everything, so three slow attempts cannot add up to a minute of spinner. 2. **Idempotency key.** Ensures a non-idempotent write carries the key created for the user's intent. Outside retry, so every attempt sends the same key. 3. **Retry.** Decides whether a failed attempt is repeated: idempotent methods, writes that carry a key, transient failures only, a capped number of attempts, `Retry-After` honoured. 4. **Auth.** Reads the current access token for *this* attempt, attaches it, and on `401` runs the shared refresh and one replay. 5. **Logging.** Records each attempt as it actually went on the wire, with credentials redacted. 6. **Transport.** The connection itself. | Layer | Runs | What it may change | Why here | |---|---|---|---| | Idempotency key | once per call | adds one header | a key minted per attempt breaks deduplication | | Retry | once per call, loops inner layers | nothing on the request | must see each attempt's outcome | | Auth | per attempt | the `Authorization` header | a refresh between attempts must be picked up | | Logging | per attempt | nothing | must see real headers and real failures | ## The placements that go wrong - **Key inside retry.** If the key layer generates a value whenever it sees a write, an inner placement mints a new key on every attempt and the server cannot recognise retries. The safest design has the feature create the key at the user's confirm and the layer only check that a write carries one. - **Auth outside retry.** The token is attached once; after a refresh, the remaining attempts still carry the expired one and fail. - **Retry handling 401.** Retry and auth would both react: retry repeats the call, auth refreshes again, and the attempt count multiplies. Retry should treat `401` as auth's job, and `403` and most other `4xx` as final. - **Logging outermost only.** It sees one call and its final result, and misses that the transfer took three attempts and one refresh, exactly what a support engineer needs. ## Attaching credentials to each call The auth layer is also where the app decides *which* calls get a token at all. A wallet app talks to its own API, but also to an image host for contact avatars, a map tile service, a third-party analytics endpoint. Attaching the bearer token to every outgoing request leaks a live credential to hosts that have no business holding it. The layer should attach it only to an allowlist of the app's own API origins, and the token endpoint call must skip the auth layer entirely so a refresh never tries to refresh itself. ## Logging without leaking Per-attempt logging is useful precisely because it is close to the wire, which is also why it is dangerous: - redact the `Authorization` header and any refresh token in bodies; - avoid full payment bodies; log the transfer id and the idempotency key instead, which are enough to correlate with server logs; - keep verbose wire logging out of release builds, where logs can end up on shared devices or in bug reports. ## Ordering as a team rule Interceptor order is easy to break silently: a new team adds a header-signing or analytics layer at the wrong place and nothing fails until a refresh happens mid-retry. Treat the order as part of the networking module's contract: one place that assembles the chain, a test that asserts the order, and a short note per layer saying whether it runs per call or per attempt.
- In a mobile app's interceptor chain, where should a timing metric go if you want to know what the user waited?Outside the retry layer. There it measures the whole logical call, including every attempt and any token refresh, which is what the user experienced. A timer inside retry measures single attempts, which is useful for the network's health but hides that the user waited for three of them.
- Why should a mobile app's auth interceptor attach the access token only to certain hosts?Because the same client usually calls image hosts, map tiles or third-party endpoints too, and a bearer token works for whoever holds it. Attaching it everywhere hands a live credential to services outside your trust boundary. An allowlist of the app's own API origins keeps the token where it belongs.
- Interceptor order is cosmetic; every layer sees the same request anyway.
- Generate the idempotency key in an interceptor that runs on every attempt.
- Let the retry layer retry 401 responses like any other transient failure.
- Attach the bearer token to every outgoing request, whatever the host.
- Log full request headers in release builds to make support easier.