DISTRIBUTED SYSTEMS & ARCHITECTURE · SUBTOPIC
Saga & Transactional Outbox Patterns
Keep multi-service workflows consistent without fragile Two-Phase Commit (2PC).
AdvancedPart of Microservices & Service Mesh
Subtopics in Microservices & Service Mesh (2)
In plain words
If booking a trip involves Flight + Hotel + Car across three companies, you don’t freeze all three companies while paying (2PC). Instead, book step-by-step, and if Car fails, automatically run compensating cancellations on Hotel and Flight (Saga).
Think of it like…
Booking Flight, then Hotel, then Car - and canceling Flight/Hotel if Car sells out.
Loading interactive diagram
Loading interactive exercise
Theory & how it works
Why the Transactional Outbox solves "Dual Writes"
If your code saves an order to Postgres and THEN tries to publish an OrderCreated message to Kafka, a crash right between those two lines leaves the order saved with no event ever published! The Outbox Pattern writes the order AND an outbox_events row inside the same SQL transaction, and a worker/CDC relays outbox rows reliably to Kafka.
Key points & practical guide
Summary checklist
- Avoid 2PC across independent microservices
- Saga = sequence of local transactions + compensating actions on failure
- Outbox pattern makes DB commit + Kafka publish atomic
When to use it
- E-commerce checkout, travel booking, multi-service workflows
Watch out for
- Forgetting idempotency on compensating actions (what if refund runs twice?)
Quick check
Loading quiz