Do you need an outbox?
Use an outbox when a committed change without its event is a correctness problem and the database must stay authoritative.
What is a lost event worth, and who must notice? Use an outbox when a committed change without its event is a correctness problem and the database must stay authoritative.
Context
Start with what the missing event prevents someone from doing. If another service depends on it to update a state it cannot reconstruct, a successful database commit is not enough. If the event only nudges a view that can be rebuilt from the database, delayed repair might be acceptable. Name who notices the gap and how long they can wait. If the answer is “a customer will tell us,” the team has delegated detection, not solved it.
Options
A naive dual write commits to the database and sends to Kafka separately. A polling outbox stores the intent to publish in the database transaction and has a separate process, the relay, check the table for pending events. The table compares design trade-offs, not measured performance.
| Option | What you get | What you pay |
|---|---|---|
| Naive dual write, with or without retries | A direct path with no relay; retries can recover some send failures | Without durable intent, in-process retries can't recover a send that was never attempted before a crash |
| Transactional outbox with polling | A durable record of what still needs publishing | An outbox table, a relay to operate, cleanup, and possible duplicates |
| Outbox read by change data capture (CDC), such as Debezium — untested here | The same intent recorded at commit, read from database changes rather than by polling | A CDC pipeline to operate and monitor, with recovery and delivery behavior to verify |
| Kafka is authoritative; the database is a projection — untested here | One write in the request path: Kafka holds the event and a consumer derives the database state | The derived view can lag; consumers need a way to catch up or rebuild it |
| Accept loss and reconcile periodically — untested here | A way to repair detectable gaps without forwarding every event reliably | Gaps can persist until the next check; repair needs an owner and a source to compare against |
What I chose and why
For a database-owned change whose event must not disappear, I'd start with a polling outbox. In the crash finding, I killed both workers just after the database commit. The naive worker left a row with no event. The outbox left an unsent entry, and the relay published it after starting. When I killed the relay after Kafka acknowledged the send, the replacement published the same event again and still drained the second, untouched request.
That result is why I'd pay for durable intent, not why I'd claim exactly-once delivery. The deep dive explains where the second publish comes from. The experiment used one active relay and one Kafka partition at chosen crash points. It did not measure production loss rates or compare naive retry policies. The other designs in the table are options, not study results.
What it cost
Someone has to own the table and decide when sent rows can be removed. Someone has to monitor the relay, including how many rows are pending and how long the oldest one has waited. A quiet Kafka topic does not tell you whether there was nothing to send or the relay fell behind. Downstream, use a stable event ID and record it in the same transaction as the consumer's update, or pass it as an idempotency key to a destination that enforces it. The outbox alone does not prevent duplicate effects.
If consumers depend on per-entity order, concurrent relays can publish an entity's later change first. With key-based partitioning, a shared Kafka key routes the changes to the same partition, which preserves append order but cannot repair a reversal that happened before publication. The single-relay experiment did not test this.
When I'd decide differently
If Kafka is authoritative and the database is a projection that can catch up later, I'd write the event once and derive the database state rather than add an outbox to that database. If the database owns the change and the team already runs CDC, I'd consider using it to read the outbox rather than operate a second relay, but I'd test its restart and replay behavior before relying on it.
For a best-effort refresh hint, I'd accept a missed event if the view can be rebuilt, someone owns reconciliation, and the business accepts stale data until the job runs. If there is no way to detect or repair the gap and it can affect correctness, I would not choose a naive dual write just because the code is shorter.
What to do
- Write down what breaks when an event is lost, who notices, and the longest acceptable delay.
- Name the authoritative store and check whether the other can be derived without a second write in the request.
- If you choose an outbox, kill the worker after commit and the relay after acknowledgement. Count Kafka records and downstream effects by event ID, then check that the pending queue drains.