How does the transactional outbox solve the dual-write problem?
Sketch your own answer first, then compare it with the one below.
A strong answer
The service writes the business row and an event row in the same local database transaction. They commit together or not at all. A separate relay reads unsent rows from the outbox table and publishes them to the broker, then marks them sent.
The event can no longer be lost while the database survives, and it cannot describe a write that rolled back. The cost is delay, since the event goes out after the relay polls, and an extra table to prune.
The relay can crash after publishing and before marking, so the same event goes out again. The outbox gives at-least-once publication, and not more.
Sources
- Chris Richardson (2018). Pattern: Transactional outbox.
- Debezium contributors (2023). Outbox Event Router.