Distributed Transactions
Coordinating atomic operations across multiple services or databases.
1The Wedding Ceremony Analogy
A distributed transaction spans multiple nodes/services. The challenge: ensuring all participants commit or all abort-atomicity across network boundaries.
2Two-Phase Commit (2PC)
The Classic Approach
Pros
- ✓ Strong consistency (ACID)
- ✓ All-or-nothing guarantee
Cons
- ✗ Blocking-holds locks during prepare
- ✗ Coordinator is single point of failure
- ✗ Doesn't scale well
3Real-World Dry Run: Order + Payment + Inventory
Scenario: E-commerce checkout with 2PC
Problem: If coordinator crashes between PREPARE and COMMIT, participants are stuck waiting with locks held.
4Alternatives to 2PC
Saga Pattern
Sequence of local transactions with compensating actions. No global locks.
Use case: Long-running business processes
Learn more →TCC (Try-Confirm-Cancel)
Try reserves, Confirm commits, Cancel releases. Business-level locking.
Use case: Reservation systems
Outbox Pattern
Write to DB + outbox in one transaction. Separate process publishes events.
Use case: Event-driven microservices
Event Sourcing
Store events, derive state. Natural audit trail and replay.
Use case: Financial systems
Learn more →5Key Takeaways
?Quiz
1. 2PC coordinator crashes after PREPARE. What happens to participants?
2. Microservices checkout: order, payment, shipping. Best approach?