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?
answer
- scope a call to what it serves
- reads die with the screen
- the transfer belongs to the intent
- cancelling cannot un-send a request
- unknown outcome shows as pending
basics
~20 sCancel 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.
solid answer
~50 sEach call belongs to whatever it serves. Reads launched for the Send screen (contact search, avatars, a fee quote) run in the screen's cancellation scope and are cancelled when it goes away, which frees the radio and the connection pool. The transfer is the user's intent, not the screen's, so it runs in a scope owned by something app-lifetime, such as the payments repository. Cancelling it would not help anyway: once the bytes are sent, the server may apply the transfer, and a client-side cancel only throws away the answer. If the outcome is still unknown when the app comes back to it, for example after the OS suspended or killed the process, the app shows the transfer as *pending* and resolves it by retrying with the same idempotency key or asking for its status. A user-facing *Cancel* after sending is a separate server operation, not an abort.
go deeper
Recall that leaving a screen should stop the loads that only served it, while a payment the user confirmed must keep going.
Explain cancellation scopes, why screen-scoped reads are cancelled, and why the transfer is owned by an app-lifetime component like a repository.
Show that cancellation is local, that a killed process leaves an unknown outcome, and how the same key and a pending state let the app reconcile safely.
Set the rule for which owners may start writes and how unknown outcomes are reconciled, so no feature ever reports a sent payment as failed.
## Two kinds of in-flight work When a user backs out of the Send screen, the networking layer typically has several calls open: - a **recipient search** as the user typed a name; - **avatar images** for the contact list; - a **fee quote** for the amount on screen; - the **transfer** itself, sent a second ago when the user tapped *Confirm*. The first three exist only to draw this screen. The fourth exists because the user decided to move money. Treating them the same way, cancelling everything or nothing, is the mistake the interviewer is fishing for. ## Scope each call to what it serves A **cancellation scope** is a container for work that is cancelled together. The useful rule is to launch each call in the scope of the thing it serves: | Call | Serves | Scope | On leaving the screen | |---|---|---|---| | Recipient search | this screen's UI | screen | cancel | | Avatar images | this screen's UI | screen | cancel | | Fee quote | this screen's UI | screen | cancel | | Transfer | the user's intent | app-lifetime owner, e.g. the payments repository | keep running | Cancelling screen-scoped reads matters on a phone. The radio, the connection pool and the client's concurrency limit are shared, and abandoned reads compete with whatever the user opens next. Cancelling them is cheap and safe, because a read has no side effect to lose. ## Why the transfer must not be cancelled The key fact is that **cancellation is local**. Cancelling a request stops the client waiting; it does not reach back and undo what the server is doing. Even protocols with an explicit cancel signal leave stopping the work to the server application, and an HTTP request that has fully left the device may already be committed. So "cancel the transfer because the screen closed" does not prevent the payment. It only guarantees the app no longer knows whether it happened. The transfer therefore runs somewhere that outlives the screen: 1. the screen asks the payments repository to send the transfer; 2. the repository owns the request, its idempotency key and its outcome; 3. the screen observes the outcome while it is visible and stops observing when it goes away; 4. the transaction list or a notification shows the outcome wherever the user is. ## When the outcome is unknown On both native platforms the system may stop a backgrounded app from running, or end its process to reclaim memory, at a moment the app does not choose, so a transfer that was in flight may never deliver its response to the app. The design must assume this: - Record the transfer as **pending** before sending, with its idempotency key. - On return, **reconcile**: retry with the same key, which returns the server's stored result instead of paying again, or query the transfer's status. - Never display *failed* for a transfer whose outcome is unknown; users who see *failed* send the money again by hand. Making that pending record survive a full relaunch is the job of a durable queue of pending writes; this pipeline's part is not to throw the attempt away and to reuse its key. ## A user's Cancel is not a client cancel Some wallets offer *Cancel* on a pending transfer. That is a **new server operation** ("cancel transfer 812") with its own rules: it may succeed if the transfer has not settled, or fail if it already has. It is never implemented by aborting the original HTTP request. ## Two refinements - **Not yet sent means safe to drop.** If the transfer is still queued behind other calls and has not left the device, cancelling it is honest, provided the UI says so. - **Shared reads need reference counts.** If two screens share one in-flight read, it is cancelled only when the last one leaves. - **Cancellation must be observable.** A cancelled read should end quietly, not surface as an error toast on the screen the user just opened, so the pipeline reports cancellation as its own outcome rather than as a network failure. The interviewer's real question is whether the candidate separates *what the user asked for* from *what the screen needed to draw itself*. A design that names an owner for every call, and says which owners outlive the screen, answers it before the edge cases are even raised.
- In a mobile payments app, should a transfer still queued on the device, not yet sent, be cancelled when the screen closes?It can be, honestly, because nothing has reached the server. But the product must decide whether leaving the screen means *abandon*: many wallets keep the intent and send it anyway. If the app drops it, the UI must say the transfer was not sent; silently discarding a confirmed payment is worse than either choice.
- The OS killed a mobile wallet app while a transfer was in flight. What does the app do when the user opens it again?It finds the transfer recorded as pending with its idempotency key and reconciles before showing a result: it retries with the same key, which returns the server's stored outcome, or asks for the transfer's status. Until that answer arrives the transfer stays *pending*; showing *failed* invites a manual second payment.
- Cancelling the HTTP request stops the server from applying the transfer.
- Every call should die with the screen that launched it, writes included.
- If the response never arrived, mark the transfer as failed.
- Implement the user's Cancel button by aborting the in-flight transfer request.
- There is no point cancelling reads; the response will just be ignored.