SteelReed

Search

Search posts, questions…

Messaging & KafkaQuestion 19 / 21

Middle

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.

poller or CDCService saves orderOne DB transactionorders tableoutbox tableKafka topicIdempotent consumer

3 points to say:

  1. A dual write can diverge: order saved but event lost, or event sent but the DB rolled back.
  2. Outbox: the event shares the transaction with the data; a poller is simple, CDC is near real time and follows commit order.
  3. 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.