Video in progress

Besides the outbox, what other ways are there to solve the dual write problem, and what does each cost?

The options: outbox, "listen to yourself" (first an event, then you update your own DB from that event), event sourcing (the event itself is the source of truth) and CDC. Each one distributes consistency, latency and complexity differently.

📖 Answer

A dual write means writing to the DB and sending to the broker are two separate operations. If either one fails, the system is left in an inconsistent state. 2PC/XA is almost never used in practice: Kafka doesn't support XA, and elsewhere it is slow and brittle.

Outbox. The event goes into a table in the same local transaction as the business record, then a relay sends it. The most recommended and easy to understand. The cost: a table, a relay, cleanup, latency.

Listen to yourself. The service first writes the event to Kafka, then its own consumer reads that event and updates the DB. Now the event is the single write. The problem: when the response goes back to the user the DB may not be updated yet (read-your-writes breaks), and validation and unique checks (for example, "email is taken") get harder, because the time of the check and the time of the write are separated.

Event sourcing. The event store itself is the source of truth, and state is built from events. There is no dual write, and audit comes naturally. But it means redesigning the whole domain: projections, snapshots, event versioning, training the team. Moving to event sourcing just for the dual write problem is an overly heavy solution.

CDC straight from the table. Debezium watches the business table and turns changes into events. The code doesn't change, but the events are at the "a table row changed" level, with no domain meaning, and the internal schema becomes an external contract.

Its counterpart on the consumer side is a transactional inbox: an incoming message is first written to an inbox table (with dedup), then processed.

My default choice is outbox + Debezium or polling, with an inbox/dedup in the consumer. I suggest event sourcing only when the domain itself thinks in terms of events (accounting, ledger, processes that require audit).