Designing a Saga pattern that I plan to implement in a worldwide microservices training class for Microsoft.
In a distributed microservices architecture, the complexity of implementing a transaction across several microservices cannot be understated. The lack of a distributed transaction mechanism means that you must manage distributed transactions programmatically. A popular pattern for adding distributed transactional support is the Saga pattern. It’s implemented by grouping local transactions together programmatically and sequentially invoking each one. If any of the local transactions fail, the Saga aborts the operation and invokes a set of compensating transactions. The compensating transactions undo the changes made by the preceding local transactions and restore data consistency.
Here is the design thus far. We have a simple eCommerce store checkout process that creates an order, validates payment, ships, and notifies the customer. The transaction spans 5 microservices. I’m looking to use a choreography (event-driven) approach. The compensating logic is colored red. Right now, we use Azure Service Bus topics for our message brokering. Keep in mind this is still a work-in-progress:
Would appreciate any input. Especially interested hearing about frameworks that you have used that facilitate this kind of operation.
To read more about managing distributed data in microservice applications, be sure to checkout the Distributed Data chapter in new Architecting Cloud-Native .NET Apps for Azure which is available for free from the Microsoft architecture site. As well, Chris Richardson always does a fantastic job of providing guidance for distributed, microservice applications. Check out his new virtual bootcamp, Distributed Data Patterns in a Microservice Architecture.