Save the change, then tell the others

Intermediate · 6 min · Animated story · 6 scenes

An order is saved to a database and then announced to a broker. The two systems share no commit, so a crash between the steps can leave them disagreeing.

Follow the requestScene 1 / 6

The order row commits first. The broker publish then fails, and nothing ties the two steps together.

Illustrative guided sequence, not a simulation or a benchmark.

01 / 06

Dual write: two steps, no shared commit

The order service writes the order row, which commits. It then publishes the event, and the broker is unavailable. The row exists, but billing never hears about the order. Whether the client learns of the failed publish depends on the code, and the committed row cannot be rolled back by the publish step.

What you will learn

  • Explain why a row write and a broker publish, done as two steps, can disagree after a failure.
  • Trace how an outbox row is written in the same local transaction as the business change.
  • Describe how a relay publishes stored rows and why it can publish one row twice.
  • Explain why consumers need idempotent handling when delivery can repeat.
  • State when the outbox adds more complexity than it removes.

Transcript

  1. Dual write: two steps, no shared commit. The order service writes the order row, which commits. It then publishes the event, and the broker is unavailable. The row exists, but billing never hears about the order. Whether the client learns of the failed publish depends on the code, and the committed row cannot be rolled back by the publish step.On screen: The order row commits first. The broker publish then fails, and nothing ties the two steps together.
  2. Publish first: the mirror failure. Reversing the order of the two steps does not fix the problem. The broker accepts the event, then the row write fails. Billing now acts on an order that does not exist. The mismatch moves to the other side, and the two systems still have no shared commit.On screen: Publishing first leaves an event for an order that never committed when the row write fails.
  3. The outbox row commits with the order. The order service writes the order row and a matching outbox row inside one database transaction. A commit saves both, and a rollback removes both. The broker is not involved in the request at all, so a broker outage cannot change what was saved.On screen: The order row and its outbox row are written in one local transaction, so they commit together or not at all.
  4. A relay publishes from the outbox. A relay process polls the outbox table for unsent rows. It publishes each row to the broker, waits for the acknowledgement, and then marks the row sent. Reading the database's change log, sometimes called transaction log tailing, can replace polling. Either way, the relay publishes the rows that transactions wrote to the outbox.On screen: The relay reads unsent rows, publishes each one, and marks it sent after the broker acknowledges it.
  5. A crash after publish sends a duplicate. The relay publishes an event, then fails before it marks the row sent. After restart the row is still unsent, so the relay publishes it again. The broker now holds two copies, and billing can receive both. Consumers must tolerate the repeat, which the next scene covers.On screen: If the relay crashes after publishing but before marking the row sent, it publishes the same event again.
  6. Billing stays correct on a repeat. Billing stores each event ID in the same transaction as its own update. When the same event arrives again, the ID is already recorded, so the handler skips the work and acknowledges the message. The check covers repeats from the relay as well as repeats from broker retries.On screen: A consumer that records processed event IDs can handle a repeated event without applying it twice.

Sources

Under the hood

Why writing a database row and publishing an event as two separate steps can disagree, and how an outbox table, a relay and idempotent consumers close most of the gap.

Trade-offs

  • The business change and its event are committed together, so a crash cannot save one without the other.
  • Events are published after a short delay, so downstream readers see the change a little later.
  • The relay and its duplicate handling add components to build, monitor and operate.
  • Consumers must track processed event IDs, which adds storage and a check on every event.

When not to use it

  • No other system needs the event, so a plain database transaction is simpler.
  • Consumers cannot tolerate any delay between the commit and the publication.
  • The team has no capacity to run a relay, monitor its lag and clean up the outbox table.

Limitations of this lesson

  • The failure cases are shown as single scripted examples. Failure rates and timings are not measured.
  • Ordering across many outbox rows is not modelled.
  • Change data capture is described in narration only and is not animated.
  • Broker acknowledgement behaviour differs between products and is not shown in detail.