What is the Transactional Outbox pattern?

Answer

The Transactional Outbox pattern solves the dual-write problem: how to atomically update a database AND publish an event to a message broker. If you write to the database and then publish to Kafka, the two are not atomic — a crash between them leaves you with either missing events or orphaned events. The solution: write the event to an outbox table in the same local database transaction as the business data update. A separate Message Relay process (e.g., using Debezium CDC — Change Data Capture) reads from the outbox table and publishes the events to the broker. This guarantees exactly-once semantics at the database level, even if the service crashes, because the event is committed atomically with the business data.