CoursesBackend engineering from first request to production
Puzzle 17 · 18 of 18
What does the second editor see?
Follow two clients editing the same saved title.
All puzzles in this course
The problem
Your released service stores a bookmark called River. Two phones open it. Ada changes the title to Lake and saves. Ben still sees River and taps Save on his edit, Forest. What should his phone receive?
See it
Scroll sideways to see the full diagram.
Ada and Ben read River at version seven.
Step 1 of 4Ada drafts Lake and Ben drafts Forest. Predict what Ben sees if Ada saves first.
Step 2 of 4Ada saves against version seven. The stored title becomes Lake at version eight.
Step 3 of 4Ben sends version seven. The service returns 412 and his draft stays on his phone.
Step 4 of 4
The building block
Optimistic locking lets both people edit immediately and checks at save time whether the version they read is still current. A header is a named field carrying information about a request or response. An ETag is a response field identifying the version of the representation sent. Here its value is the quoted version of the title. Each edit sends that tag back in the If-Match request header. Every change to this title advances the version.
Both phones read version seven. The service handles Ada's request by running the update below with Lake, the bookmark identity and seven. In one database statement it checks the identity and version, changes the title and advances the version. One row changes. After commit the service replies 204, meaning the save succeeded with an empty response body.
UPDATE bookmark
SET title = ?, version = version + 1
WHERE id = ? AND version = ?;
For Ben the same condition now matches zero rows. In this fixture the bookmark always exists, so the stale tag explains the zero. The service replies 412, meaning his request's condition failed. Ben's phone can keep his draft, read Lake and ask him which title to save. Silently resending Forest with the latest tag would overwrite Ada's decision.
The analogy
Two clerks copy edition seven of a filing card. The archive accepts a replacement only against the edition written on the clerk's slip. After the first replacement, the archive can show the second clerk edition eight while the clerk keeps the proposed text.
Distinguish replay from an edit conflict
A request identity answers whether a particular intention already took effect. A version answers whether the document still matches the copy a person edited. Suppose Ada's conditional save commits and its reply is lost. Retrying it with the old version can yield 412 even though Ada's first request succeeded. A new read reveals the current title and version. It cannot in general identify who wrote them. If the service promises to recover the precise outcome of each submitted edit, record a stable request identity and its outcome alongside the accepted edit.
Try it
Optional local lab
Run the following command in the bookmark service source files.
python3 edit_conflict.py
It starts a temporary local service and sends real reads and writes. Predict Ben's status, the stored title and the next tag before running. In a copy, remove the tag condition and its matching SQL parameter. Which assertion fails, and which title survived? Restore the condition and reverse the order of the two saves. Trace the losing client's next read.
Check yourself
Sources
- HTTP Semantics, section 13.1.1 — “to prevent accidental overwrites when multiple user agents might be acting in parallel on the same resource”
- SQLite UPDATE
What you now know
A version check inside the update lets the service report a stale edit while preserving the first writer's title.
Answer every question above correctly to unlock the next puzzle.
Apply what you learned
Continue with worked examples and practice cases. When you reach the final project, make your own decisions before comparing your evidence with the worked review.
Continue to Reading production evidence → Start the final project →
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.