The dual-write problem is one of the most common and dangerous bugs in event-driven architectures. It occurs when a system tries to update a database and publish a message to a message broker (like Apache Kafka) in a non-atomic way. If one operation succeeds and the other fails, data becomes inconsistent — events can be lost or duplicated.
Here's a typical scenario: a service saves a new order to its database and then tries to publish an "order created" event. The database write succeeds, but the message publish fails due to a network issue. Now the order exists in the database but other services never learn about it. Conversely, if the message is published first and the database write fails, other services might act on an event that doesn't correspond to real data.
The Transactional Outbox Pattern is the industry-standard solution to this problem. It ensures that database state and events remain perfectly consistent without requiring distributed transactions.
How it works
-
Outbox Table: Instead of directly publishing an event, the service writes the event into a special "outbox" table within the same local database transaction that performs the business operation (e.g., creating an order).
-
Background Process: A separate process (often called a "publisher" or "relay") reads new rows from the outbox table and publishes them to the message broker. Once published, it marks the row as sent or deletes it.
Benefits
- No lost events: The event is persisted atomically with the business data. Even if the message broker is temporarily unavailable, the event remains in the outbox.
- No phantom messages: An event is never published unless the corresponding business transaction has committed.
- Strong consistency without distributed transactions: The pattern avoids the complexity and performance cost of two-phase commit protocols.
We'll likely discuss implementation details — like how to handle ordering, idempotency, and failure scenarios — in the full video. For now, understand that the Transactional Outbox Pattern is a fundamental building block for reliable messaging in microservices.
⚠️ One database write succeeds. One message publish fails. Your system is now corrupted. This is the infamous dual-write problem — one of the most common and dangerous bugs in event-driven systems.
If you work with microservices, event-driven systems, or prepare for system design interviews, this pattern is mandatory knowledge.