CoursesBackend engineering from first request to production
Puzzle 7 · 8 of 18
What if the service stops between two writes?
Save the bookmark and the work it creates together.
All puzzles in this course
The problem
Your save route writes river. A second write records that a worker should announce it. The process stops between those writes. What should the next process find?
See it
Scroll sideways to see the full diagram.
The route stages the bookmark.
Step 1 of 4The same transaction stages a pending event.
Step 2 of 4Commit makes both rows durable together.
Step 3 of 4A worker may attempt delivery more than once.
Step 4 of 4
The building block
A transaction groups database changes behind one commit. Put the saved row and the pending event in the same database transaction. A failure before commit leaves neither change. A successful commit makes both available. A worker can deliver the pending event afterward. If it sends successfully but stops before recording completion, it may send again. Give the event a stable identity. The consumer can record that identity and a database effect together to avoid applying that effect twice. Sending an email introduces another system and needs its own delivery contract.
Move the crash one statement at a time
Think of the event as a durable to-do note named E17. With separate commits, a crash after saving river can leave a bookmark and no E17. The worker can never discover the missing note by polling its event table. In one transaction, a crash before commit discards both writes; after commit, the worker can find E17 again after restart. This is the transactional outbox pattern. A later crash after sending but before marking E17 delivered creates a different problem: the worker knows it owes delivery, but cannot tell whether the receiver already acted.
Try it
Optional local lab
Run the data drills. Move the pending insert to a second transaction in a disposable copy. Stop between commits and inspect both tables.
Check yourself
Sources
What you now know
A transaction can commit both the bookmark and a pending event, or roll back both.
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.