Design Patterns (GoF) (50 Qs)•Part 3: Architectural Patterns & Enterprise Systems (25 - 35)
Saga Pattern (Choreography vs Orchestration): Distributed transactions.
senior LevelState ManagementSystem DesignDatabases & SQL
In Simple Words: In a microservices architecture, you cannot use distributed ACID database locks (2PC) across services without catastrophic latency. A Saga is a sequence of local transactions:
- If Step 3 (Payment) fails, the Saga executes Compensating Transactions in reverse order (e.g. cancel reserved inventory, cancel pending order).
- Choreography (Event-Driven): Services publish and listen to Kafka events without a central coordinator. Fast, decentralized; can become difficult to track across complex workflows.
- Orchestration: A dedicated orchestrator service (e.g. Temporal, AWS Step Functions) tells each participant service what step to execute next and triggers compensating rollbacks on failure.