LESSON 02 · Transactional outbox

Stop writing to two places

Writing an event to a table in the same transaction as the business change replaces two unrelated writes with one atomic commit.

SYSTEM VIEW

The failure path

Diagram view
Unsent rows—
5041 published—
812 billed—

Watch an order and its event part ways under a dual write, then commit together through an outbox.

  1. The Order service saves an order and publishes an OrderPlaced event: two writes with no transaction across them. Order 810 commits, then the service crashes before publishing. The order exists, but Billing never hears about it.

    Order 810
    saved
    Its event
    never sent

    What changed

    • Order 810 committed
    • Order service crashes
    • OrderPlaced 810 never sent
  2. Publish first instead. Billing charges for order 811, then the commit fails, so order 811 doesn’t exist. A try/catch doesn’t help: a crash skips the catch, and a publish timeout doesn’t say whether the broker got the message.

    Billing
    charged 811
    Order 811
    doesn’t exist

    What changed

    • Order service back up
    • OrderPlaced 811 published
    • Order 811 commit fails
  3. In one transaction, the Order service inserts order 812 and an outbox row holding event ID 5041, the order ID and a payload. Either both commit or neither does. The direct publish is gone: a separate Relay publishes unsent rows.

    Event ID
    5041
    Row holds
    event ID, order, payload

    What changed

    • Outbox table
    • Order 812 and row 5041, one commit
    • Relay
    • Direct publish
  4. The Relay reads unsent row 5041 and publishes OrderPlaced for order 812. The broker acknowledges, and Billing charges 812. Then the Relay dies before marking the row sent, so 5041 is still unsent.

    What changed

    • OrderPlaced 812 published
    • Billing charges 812
    • Relay dies before marking 5041
  5. The Relay restarts, finds row 5041 still unsent and publishes it again. Billing has already seen event 5041 and skips it, so order 812 is charged once. The Relay marks 5041 sent. Delivery is at least once, and nothing committed is lost.

    Delivery
    at least once
    Billing
    dedupes by event ID

    What changed

    • Relay back up
    • 5041 published again
    • Billing skips 5041
    • Row 5041 marked sent
  6. The costs: a Relay to run, polling that loads the Database, and a delay of up to one polling interval. Sent rows pile up, so the table needs pruning. Publishing with the order ID as the key keeps each order’s events in sequence.

    Delay
    up to one poll
    Prune
    sent rows
    Broker key
    order ID

    What changed

    • Polling the Database
    • Sent row 5041 pruned
    • Events keyed by order ID
  7. IDs are assigned before commit. Order 813 takes 5042 but commits after 814’s row, 5043. A Relay that polls only for IDs above the highest it has seen publishes 5043, moves past it and never sees 5042. Select by unsent status instead.

    Poll by
    unsent status
    Not by
    highest ID seen

    What changed

    • 5042 for 813, still in its transaction
    • 5043 for 814, published
    • 5042 commits late and is skipped

Space plays · ← → step · Home resets

THE IDEA

How it works

A service that saves an order and publishes an OrderPlaced event is making a dual write: two writes with no transaction across them. Commit first, then crash before publishing, and the order exists but billing never hears about it. Publish first, then have the commit fail, and billing charges for an order that does not exist. A try/catch does not help, because a crash skips the catch and a publish timeout does not say whether the broker got the message.

The transactional outbox takes the second write out of the request path. In one transaction, the service inserts the order and a row in an outbox table holding an event ID, order ID, and payload. Either both commit or neither does. A separate relay then reads unsent rows, publishes each one, and marks it sent.

Order 812 commits with its outbox row. The relay publishes the event, the broker acknowledges, and the relay dies before marking the row. On restart it publishes again. So the outbox gives at-least-once delivery, and consumers must deduplicate by event ID. In exchange, nothing committed is lost.

The costs are a relay to run, polling load on the database, delay up to the polling interval, and a table to prune. Publishing with the order ID as key keeps each order’s events in sequence. IDs are assigned before commit, so a poller that tracks only the highest ID seen can skip a row that commits late; select by unsent status instead. Notification System uses this handoff for receipts and News Feed for fanout.

DECISION POINT

A teammate proposes keeping the direct Kafka publish but deleting the order row whenever the publish throws an error. They say this keeps the code simple and avoids running a relay. How do you respond?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • A dual write has no atomic boundary, so a crash between the writes leaves the systems disagreeing.
  • An outbox row commits with the business change, so its event is neither lost nor invented.
  • The relay can publish twice, so consumers deduplicate by event ID.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

A service publishes to the broker and then commits the order. Billing occasionally charges for orders that never appear in the orders table. What is the cause?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.