What is the dual write problem, and how do you publish an event after a DB write without divergence (transactional outbox, CDC)?
Short answer
A dual write writes to the DB and Kafka with no shared transaction, so one can succeed while the other fails. The standard fix is the transactional outbox: write the data and an event row into an outbox table in the same transaction, then a poller or CDC (Debezium) publishes it to Kafka.
3 points to say:
- A dual write can diverge: order saved but event lost, or event sent but the DB rolled back.
- Outbox: the event shares the transaction with the data; a poller is simple, CDC is near real time and follows commit order.
- Event key = aggregate id; the outbox is at-least-once, so consumers must be idempotent.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.