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

What if the service stops between two writes? Save Queue row Commit Deliver

Scroll sideways to see the full diagram.

  1. The route stages the bookmark.

    Step 1 of 4
  2. The same transaction stages a pending event.

    Step 2 of 4
  3. Commit makes both rows durable together.

    Step 3 of 4
  4. A 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

Run the browser experiment

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.

Open the local exercise files

Check yourself

The worker loses the acknowledgement. Which value should a second delivery reuse?

Not quite. The consumer needs a stable identity to recognize redelivery. Re-read that part ↑

Right. A transaction can commit both the bookmark and a pending event, or roll back both.

Which database state contradicts the promise that every saved bookmark schedules delivery?

Not quite. A committed bookmark must have scheduled its event. Re-read that part ↑

Right. A committed bookmark must have scheduled its event.

Where must a consumer record an event identity to guard a local balance update against repeats?

Right. The deduplication decision and the protected effect must survive a crash together.

Not quite. The deduplication decision and the protected effect must survive a crash together. Re-read that part ↑

Sources

What you now know

A transaction can commit both the bookmark and a pending event, or roll back both.