The outbox pattern: what it actually guarantees
An outbox commits the business change and the intent to publish together, trading a window for lost events for the possibility of duplicate publication.
Commit the business change and the intent to publish in one database transaction. A transactional outbox gives you at-least-once publication, not exactly-once: if the relay recovers and can eventually publish, each pending event reaches Kafka, but it may reach Kafka more than once.
Question
If my service writes to a database and publishes to Kafka, how do I avoid losing the event? With two separate writes, a crash between them leaves one side ahead of the other. Commit the database first and a crash can leave a row with no event. Send to Kafka first and a crash can leave a ghost event: a record describing a change that never committed.
Mechanism
Write the business row and an outbox row with a stable event ID in the same transaction. The outbox row is a durable instruction to publish an event. If the transaction rolls back, neither row remains. If it commits, a separate process can read that instruction even if the request worker never comes back.
The relay reads pending rows, publishes to Kafka, then marks each row sent after Kafka acknowledges it. Marking it sent first would put the loss window back: a crash could leave a row that looks finished even though nothing was published. Waiting for the acknowledgement avoids that mistake, but a crash after the acknowledgement can leave the row pending. The next relay cannot assume Kafka missed the first send. It sends again with the same event ID.
What I tested
In the outbox crash finding, I killed the naive and outbox workers at the same point after PostgreSQL committed. The naive write left a committed row without an event; the outbox relay published one after it started. Killing the relay after Kafka acknowledged the send produced two records for the target ID at different offsets, while a second, untouched request produced one. A kill before the outbox transaction committed left neither row nor event.
The experiment results cover Kafka 4.2.1 and PostgreSQL 17.11, with one broker, one partition, and one active polling relay, on Windows and Ubuntu CI. The tests used chosen crash points. They did not measure how often a crash hits those points in production or how a downstream consumer handles duplicates.
The trap
Exactly-once publication and exactly-once effects are different questions. The outbox lets the relay retry a send; it does not stop that retry from creating a second Kafka record. Make the consumer idempotent by event ID: receiving the same ID again should not repeat the effect. For a database effect, one approach is to record the processed ID in the same transaction as the effect. A Kafka offset cannot stand in for that ID when two copies have different offsets.
Kafka transactions can group work inside Kafka, and an idempotent producer can suppress duplicates from its own retries. Neither makes a PostgreSQL commit and a Kafka send one atomic operation. A replacement relay making a fresh send of the same event is not the same as the original producer retrying its send.
Ordering and cleanup are still yours. If changes for the same entity must arrive in order, choose how you will identify that sequence, route related events, and handle retries without overtaking earlier work. Decide when sent rows can be removed and keep pending rows available for recovery. A polling relay queries the table for pending rows; change data capture (CDC), such as Debezium, can read committed changes instead. The study exercised one polling relay, not CDC, concurrent relays, or ordering across partitions.
How to check your system
- Kill at named boundaries. Stop the worker just before commit and again just after commit, before publication. Stop the relay after Kafka acknowledges a send but before it records completion. Use a checkpoint you can observe, not a sleep that only makes the window likely.
- Count by event ID. Before and after recovery, read the business row and outbox state and count Kafka records at distinct offsets for that ID. A committed request should eventually have a record; a rolled-back request should have none.
- Check recovery and consumer effects separately. Leave a second pending ID behind the interrupted one. Restart the relay and check that both drain. Feed the consumer two records with the same ID and count the durable effects, not just handler calls.
- Watch backlog and age. Track how many unsent rows you have and how old the oldest one is. A healthy request path does not tell you whether the relay is keeping up.