The Saga Pattern is the solution to distributed transactions. It manages a long-running process that spans multiple microservices. If one step fails, the Saga is responsible for triggering "Compensating Transactions" to undo the work of the previous steps.
Services just publish events. Service A says "Order Created." Service B hears it and says "Payment Charged." Service C hears it and says "Stock Deducted." It is simple but hard to visualize as the system grows.
A central "Saga Manager" (Orchestrator) tells everyone what to do. It acts like a conductor in an orchestra. It knows the current state (e.g., 'AwaitingPayment') and tells the Payment service to charge.
If "Stock Deduction" fails because an item is out of stock, the Saga Orchestrator tells the Payment service: "REFUND THIS USER". This ensures the system eventually returns to a consistent state.
Q: "Why is the Saga pattern preferred over 2-Phase Commit (2PC)?"
Architect Answer: "2-Phase Commit is a 'Blocking' protocol. It requires all databases to be 'Locked' until everyone agrees to commit. If one network link is slow, the entire system grinds to a halt. The **Saga Pattern** is 'Non-Blocking.' Each service commits its work locally and immediately. The system is 'Eventually Consistent,' which allows it to scale to millions of users without the performance bottleneck of global locks."