In this lesson · topic 5 of 7
Topic 5
Two writes and a crash between them
5 min read
The gap
The service commits the order row, then publishes the event. Between those two calls there is a gap, and a crash in it loses the event. Reversing the order is no better. If the service publishes first and then fails to commit, the event announces an order that does not exist.
You cannot close the gap with a retry. After a crash the service does not know which calls ran. You cannot close it with a transaction either, because the database and the broker do not share one. In the lab, the dual write lost 55 eventsmeasured. Nothing in the system noticed.
The outbox
Write the event into a table in the same database, in the same transaction as the order. The two commit together or not at all. Now no event is lost while the database survives. A relay process reads the rows that are not yet sent, publishes each one to the broker, and marks it sent.
With the outbox the lab lost 0 eventsmeasured.
What it costs
The event is not published at once. It waits for the relay, so downstream sees it a little later. The outbox table needs pruning. The relay is another process to run and watch. Some teams read the database’s change log instead of polling the table. It works the same way and needs less polling.
Predict first