Microservice architecture design with SAGA pattern
microservices are known as a good way to scale and maintain programs as easily as possible.?However, this method also has its own positive and negative aspects.?Integrated applications typically use database transactions with ACID features that facilitate data consistency.?However, if there are multiple microservices spread across our database transaction, how can we ensure data consistency??The answer is simple: with the saga pattern.?In the following, we discuss the design of microservice architecture with the saga pattern.
Getting to know microservice architecture design with saga pattern
The SAGA application pattern can be considered an old architectural concept that is still very useful for today's microservices.?Using epic patterns can be beneficial for different systems and applications and bring many benefits.?SAGA is a sequence of transactions local to each of the participating microservices.?To implement SAGA, there are steps that must be executed, and when they are completed, there is logic that decides what to do next.
SAGA must ensure that all steps are completed successfully;?Otherwise, he should make the necessary arrangements for his return.?We may also encounter exceptions to infrastructure law or business logic when submitting an application.?Of course, we need to manage all these exceptions, and if we have an exception in a certain step, cancel all the changes in the previous steps.?In some cases, we need to make additional requests to microservices to perform a full rollback.
These additional requests are known as compensating transactions, and all of these can mean that using saga patterns can complicate applications.
SAGA implementation solutions
Undoubtedly, if you plan to use saga in your microservices design, you need to get familiar with its implementation methods.?There are two ways to implement the saga pattern, each with a different approach to coordinating workflows.?These two methods will be as follows:
?Using the Orchestration method (focused)
Using the Choreography method (distributed)
领英推荐
?Using the Orchestration method (focused):
Orchestration is used when we need a centralized coordinator that manages all the logic and knows when to communicate with other microservices, what step to take, or how to roll back.?The orchestration method in the saga pattern will work best when the logic of the saga is monitored and reviewed by one or two teams.?Otherwise, the program codes can look very complex and difficult.
On the other hand, the orchestration method also has benefits for understanding complex workflows.?One of the most important of these benefits is having a more regular architecture;?Because, microservices will not be connected to each other.?Of course, the coordinator must know how to return to the previous stage in case of failure.?Therefore, the coordinator must store a log of events for each flow and perform compensating transactions on each corresponding microservice when performing a rollback.
you can find a sample in my github account :
?In this project, I walked through implementing the Saga Orchestration pattern for managing distributed transactions in ASP.NET Core microservices.
Java Developer
1 年Thanks for the useful and challenging explanation of the distribute Transactional. Is it possible to implement all the implementation notes that you mentioned in the article?