CoursesSystem design from first principles to design review
Puzzle 4 · 5 of 19
What if a successful reply disappears?
Recognize a retry after the database has committed.
All puzzles in this course
The problem
The database commits Ada's reservation. The connection drops before her phone sees the reply. How can the service distinguish a retry from a second trip?
See it
Scroll sideways to see the full diagram.
Ada taps once. Her phone saves intention 17 before sending the first request. Follow the number as it crosses the network.
Step 1 of 4The service commits grant 301 and the receipt connecting Ada’s intention 17 to that grant.
Step 2 of 4The reply is lost. Which part of this picture can Ada’s phone actually observe? It knows its intention but does not yet know the grant number.
Step 3 of 4The phone retries intention 17 with the same lot. The service finds the receipt and returns 301 again. A new intention number would describe another possible booking.
Step 4 of 4
The building block
The phone creates a request identity before the first attempt and retains it for retries of that intention. Bind it to the owner, the requested lot and the result. A unique constraint in the store arbitrates simultaneous first attempts. Recording the grant and its identity must survive a crash together.
Replaying the same successful request for the same lot returns its existing grant. Reusing the identity for a different lot is a conflict. The lab returns status 201 on creation and 200 on a successful replay. Both leave the same durable state. In this contract a full-lot refusal does not permanently consume the identity. State explicitly which results your own service retains and how long retries are supported.
Example calls against the local service:
PUT /sessions/ada-17 west → 201 west
PUT /sessions/ada-17 west → 200 west
PUT /sessions/ada-17 east → 409 id already used
GET /sessions/ada-17 → 200 west
Keep the identity across a restart
Ada taps once, the phone sends request 17 and the app closes before it receives the reply. On restart the phone still has the pending intention and its ID. Resending 17 lets the service return the original result. If the app invents 18 for that same tap, a lot with two spaces could grant both. A unique ID per network attempt breaks this contract.
Decide what a second deliberate tap means. If Ada wants a second space, it needs a new intention. If the service forgets completed identities after a day, a phone retrying a two-day-old request needs an explicit recovery rule. One option is to stop automatic retries after the supported window and look up the original reservation before offering a new purchase.
Store the first answer and challenge the replay
Suppose one driver can reserve different lots. A request number is unique only within that driver's account. Together the owner and the request number identify one intention. Run the SQL and predict what the last lookup returns when the incoming request says east.
CREATE TABLE receipt (
owner TEXT NOT NULL, intent TEXT NOT NULL, lot TEXT NOT NULL, grant_id INTEGER NOT NULL,
PRIMARY KEY (owner, intent)
);
INSERT INTO receipt VALUES ('Ada', '17', 'west', 301);
INSERT INTO receipt VALUES ('Ada', '17', 'east', 302)
ON CONFLICT (owner, intent) DO NOTHING
RETURNING grant_id;
SELECT grant_id, lot,
CASE WHEN lot = 'east' THEN 'replay'
ELSE 'conflicting request' END AS decision
FROM receipt WHERE owner = 'Ada' AND intent = '17';
The second insert returns no row. That alone cannot tell the handler how to reply. Reading the saved row gives grant 301 for west, so the changed lot is a conflict. Change east to west in both places to model a genuine retry. The handler can return 301 even though the attempted insert proposed 302.
The table isolates the identity check. In a service, derive the owner from the signed-in account and include every field that changes the operation's meaning in the comparison. Commit the receipt and the real inventory change together. Otherwise a receipt can outlive a failed reservation.
Try it
Compare a disconnect immediately before commit with one immediately after. Then compare retry identity with an edit version. One asks whether this intention was already carried out. The other asks whether the record is still the version the editor saw.
Check yourself
Sources
- AWS · Making retries safe — “the service first checks to see if it has seen this identifier before”
- SQLite · UPSERT
What you now know
A stable identity ties a replay to the original intention and committed grant.
Answer every question above correctly to unlock the next puzzle.
The driver can retrieve one receipt. How can the app page through many receipts while new ones arrive?
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.