{"id":1602,"date":"2022-07-04T13:14:22","date_gmt":"2022-07-04T07:44:22","guid":{"rendered":"https:\/\/ontoborn.com\/blog\/?p=1602"},"modified":"2022-07-12T23:50:06","modified_gmt":"2022-07-12T18:20:06","slug":"distributed-transaction-management-in-microservices-part-2-saga-pattern","status":"publish","type":"post","link":"https:\/\/ontoborn.com\/blog\/distributed-transaction-management-in-microservices-part-2-saga-pattern\/","title":{"rendered":"Distributed Transaction Management in Microservices \u2014 Part 2 \u2014 Saga Pattern"},"content":{"rendered":"\n

In this article, we are going to see about the Saga pattern which is an asynchronous pattern<\/strong> that performs a sequence of transactions in each microservice and publishes messages or events to proceed to the next step. If any step fails in between, compensating steps will be executed by the Saga pattern to reverse the transaction<\/p>\n\n\n\n

\"\"<\/a><\/figure>\n\n\n\n

We can see from the above diagram that the Saga patterns perform a sequence of local transactions in each service. Each service updates its database and then publishes a message or event which will trigger the next local transaction.<\/p>\n\n\n\n

\"\"<\/a><\/figure>\n\n\n\n

So we have to write the logic to commit the transaction and also have a mechanism to reverse the flow when something goes wrong anywhere in the transaction. All the transactions and compensating transactions will happen through the listeners that listen to an event or a message from the queue<\/p>\n\n\n\n

The compensating transaction must be idempotent<\/strong> and should be able to retry if the previous try fails<\/p>\n\n\n\n

The saga pattern can be implemented using 2 ways namely<\/p>\n\n\n\n