The 30 second answer
I never write to the database and to Kafka in two separate steps, because one can succeed after the other fails. I write the business row and an outbox row in the same database transaction, and a relay, either polling or change data capture, publishes the outbox to Kafka. Delivery is still at-least-once, so consumers deduplicate by event id. The idempotent producer stops a retried send from creating a second log entry.
This is the dual-write problem. A database commit and a Kafka send() are not one atomic operation. If you write the order row and then the process crashes before send() completes, the event never leaves. If you send first and then the database rolls back, consumers act on an order that does not exist.
The transactional outbox solves it by making the event part of the same database transaction as the business write. The outbox table holds an id, the aggregate id, a type, a payload, and a published flag or timestamp. A relay then publishes unpublished rows to Kafka. Two common relays:
- A worker that
SELECT ... FOR UPDATE SKIP LOCKEDa batch, produces the records, and marks them published. - Change data capture on the outbox table, for example Debezium's outbox event router, so the publication is driven from the write-ahead log rather than from polling.
Delivery from the relay is still at-least-once: a crash after a successful produce and before marking the row published will send the event again. Consumers deduplicate by event id. The idempotent producer (enable.idempotence=true) stops the relay's own retries from appending a second copy of the same produce request.
Kafka transactions do not replace the outbox when the source of truth is the database. They atomicise writes to Kafka, including the consumer offset, not a write to Postgres plus a write to Kafka.
Alternate interview scenarios
"The relay sent to Kafka, then crashed before marking the row published. What happens, and in which order should those two steps go?"
On restart published_at is still null, so the relay sends the same stored payload again with the same event id. That duplicate is the price of the safe order. If you mark the row published first and crash before Kafka acknowledges, the row looks done and the event is lost for good. So: wait for the producer acknowledgement, then mark the row in a separate transaction, and accept at-least-once delivery. Never hold the database transaction open across the network send.
"You run three instances of the service, and each runs the polling relay. What breaks?"
All three can select the same unpublished rows and publish them three times. A local guard such as Quarkus @Scheduled(concurrentExecution = SKIP) only stops overlapping runs inside one JVM. Across instances you need a shared claim: SELECT ... FOR UPDATE SKIP LOCKED so each instance takes a different batch, or a lease column (claimed_by, claimed_until) when you do not want to hold a row lock during the Kafka send. Consumers still deduplicate, because claims can expire. Keep the polling query cheap with a partial index on unpublished rows (... WHERE published_at IS NULL), so it does not scan the growing history of published events.
"How would you move from a polling relay to Debezium, and what changes?"
The business transaction and the outbox table stay the same. Debezium reads committed inserts from the PostgreSQL write-ahead log (change data capture), and the Outbox Event Router builds records from the aggregatetype, aggregateid, type and payload columns. So naming the columns that way from day one makes the switch cheap. With the default routing, the topic name is derived from aggregatetype, for example outbox.event.Order, so align the router configuration or the consumer's topic. If you embed the Debezium engine in the application instead of running Kafka Connect, configure durable offset storage, such as a file or Kafka-backed store. An in-memory store forgets the capture position on restart.
"The client retried the checkout request after a timeout. Does the outbox stop a duplicate order?"
No. The outbox makes sure each committed order publishes its event. It does not know that two HTTP requests mean one purchase. If the second request is not recognised as a retry, it creates a second order with a new event id, and consumer deduplication by event id cannot catch it, because the ids differ. HTTP idempotency keys on the checkout endpoint and event-id deduplication in consumers solve two different duplicate problems, and you need both.
"How do you test that the outbox actually survives a crash?"
Runtime.getRuntime().halt(1) rather than System.exit, because halt skips shutdown hooks, so the test sees what a real crash leaves behind, not a graceful shutdown. After restart, assert that the order exists, the event reached Kafka at least once, and the consumer wrote exactly one fulfillment row.The same pattern, aimed at a microservices audience, is also answered in How do you publish events reliably when you update the DB and send to Kafka?.
Sources (9)
- The Main Thread: transactional outbox checkout with Quarkus, PostgreSQL and Kafkathe-main-thread.com
- Quarkus scheduler reference: concurrent executionquarkus.io
- PostgreSQL 17: Partial indexespostgresql.org
- Debezium: Embedded engine and offset storagedebezium.io
- Java 21: Runtime.haltdocs.oracle.com
- microservices.io: Transactional outboxmicroservices.io
- Debezium: Outbox Event Routerdebezium.io
- Kafka 4.4 design: Message delivery semanticskafka.apache.org
- PostgreSQL 17: SELECT ... FOR UPDATE and SKIP LOCKEDpostgresql.org