CoursesBackend engineering from first request to production
Puzzle 1 · 2 of 18
Did the first write happen?
Make a lost reply safe to retry.
All puzzles in this course
The problem
The save route accepts a bookmark name. Your app times out after tapping Save. The phone cannot tell whether the server wrote the row just before the reply disappeared. What must stay the same on retry?
See it
Scroll sideways to see the full diagram.
The phone chooses one stable name.
Step 1 of 4The database commits a single row.
Step 2 of 4The phone never sees the reply.
Step 3 of 4The second write finds the same key.
Step 4 of 4
The building block
The database stores information in a table. Think of each row as one bookmark and each column as one property. SQL is a language for reading and changing these tables. A unique key lets the database enforce one row per name, even when two callers write together. Use river as the identity of the desired bookmark. The database accepts at most one row for that name. The insert asks it to ignore a conflicting key. The handler waits for the transaction to commit before replying. The first insertion returns 201. Repeating the same write returns 200 and leaves one row. Checking for the row in application code and inserting later leaves a gap where two callers can both see it missing.
Lose the reply after a successful save
Ada sends a write request for river. The database commits one row, but the connection closes before Ada sees the reply. Her phone knows only that the outcome is unknown. On the second attempt the database checks the same key and finds river already there. The reply changes from a lost 201 to a visible 200, yet the durable result is still one bookmark. This property is called idempotence: repeating the intended operation preserves its intended effect. Starting a new operation called hill is a different intention.
Try it
Optional local lab
Run both writes. Predict the list after a third write with a new name. Check it and explain which component enforces uniqueness.
Check yourself
Sources
What you now know
A stable bookmark name and a unique database key let repeated writes converge on one row.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.