How do microservices handle distributed transactions (Saga pattern)? माइक्रोसर्विसेज़ डिस्ट्रिब्यूटेड ट्रांज़ैक्शन्स को कैसे हैंडल करते हैं (Saga पैटर्न)?
Because each microservice owns its own database, a traditional ACID transaction spanning multiple services isn't possible — there's no single database to commit or roll back atomically. The Saga pattern solves this by breaking a business transaction into a sequence of local transactions, each executed by a different service, with each step publishing an event or message that triggers the next step.
If a step fails partway through, previously completed steps are undone using compensating transactions (e.g. if payment succeeds but shipping fails, a compensating "refund payment" transaction runs) rather than a database-level rollback. Sagas are commonly implemented as either choreography (services react to each other's events independently, no central coordinator) or orchestration (a central saga orchestrator explicitly directs each step), with orchestration being easier to reason about as complexity grows.
// Choreography: OrderCreated -> InventoryReserved -> PaymentProcessed
// If PaymentFailed, a compensating InventoryReleased event followsचूंकि हर माइक्रोसर्विस अपना खुद का डेटाबेस रखती है, कई सर्विसेज़ में फैला एक पारंपरिक ACID ट्रांज़ैक्शन संभव नहीं है — एटॉमिक रूप से कमिट या रोलबैक करने के लिए कोई एक डेटाबेस नहीं होता। Saga पैटर्न किसी बिज़नेस ट्रांज़ैक्शन को स्थानीय ट्रांज़ैक्शन्स के अनुक्रम में तोड़कर इसे हल करता है, जहाँ हर चरण एक अलग सर्विस द्वारा चलाया जाता है।
यदि कोई चरण बीच में विफल हो जाता है, तो पहले पूरे हुए चरणों को कंपनसेटिंग ट्रांज़ैक्शन्स से वापस किया जाता है (जैसे यदि पेमेंट सफल हो लेकिन शिपिंग विफल हो, तो एक कंपनसेटिंग "रिफंड पेमेंट" ट्रांज़ैक्शन चलता है)। Sagas को आमतौर पर कोरियोग्राफी या ऑर्केस्ट्रेशन के रूप में इम्प्लीमेंट किया जाता है।
// कोरियोग्राफी: OrderCreated -> InventoryReserved -> PaymentProcessedWas this answer clear?