Design Patterns (GoF) (50 Qs)•Part 2: Behavioral Patterns & Communication (11 - 24)
What is the Outbox Pattern and how does it guarantee Dual-Write consistency?
junior LevelCode ExampleBehavioral
In Simple Words: When an app must update a database AND publish an event to a message broker (Kafka/RabbitMQ):
- The Dual-Write Problem: If the database commit succeeds but the network fails before publishing to Kafka, data is out-of-sync!
- Transactional Outbox Pattern:
- App writes entity update AND inserts an event record into an
outboxtable in the SAME database transaction. - A Change Data Capture (CDC) tool (e.g. Debezium) tails the database transaction log and guarantees publishing the outbox events to Kafka with at-least-once reliability!
- App writes entity update AND inserts an event record into an
text
Transactional Outbox Pattern:
[Application Transaction]
├─► UPDATE orders SET status = 'PAID'
└─► INSERT INTO outbox_table (event_type, payload)
│ (Single Atomic SQL Transaction!)
▼
[Debezium CDC / Poller] ──► Publishes to [Apache Kafka]