SAGA Microservice Design Pattern Tutorial with Examples for Software Programmers

SAGA Microservice Design Pattern Tutorial with Examples for Software Programmers

In this video we will learn about SAGA (Segregated Access of Global Atomicity) design pattern for microservices. This is the 5th design principle in Database design patterns category for microservices.  

Saga design pattern is a way to manage data consistency across microservices in distributed transaction scenarios. Saga is a sequence of transactions that updates each service and publishes a message or event to trigger the next transaction step. If a step fails, the saga executes compensating transactions that counteract the preceding transaction.

The term saga refers to Long Lived Transactions (LLT) and abbreviated as Segregated Access of Global Atomicity.

Saga pattern is a failure management pattern that helps establish consistency in distributed applications, and coordinates transactions between multiple microservices to maintain data consistency. Microservice publishes an event for every transaction, and the next transaction is initiated based on the event's outcome. It can take two different paths, depending on the success or failure of the transactions.

Transaction is a single unit of logic or work, sometimes made up of multiple operations. Within a transaction, an event is a state change that occurs to an entity, and a command encapsulates all information needed to perform an action or trigger a later event. Transactions must be atomic, consistent, isolated, and durable (ACID). Transactions within a single service are ACID, but cross-service data consistency requires a cross-service transaction management strategy.

** ACID Operation **

1.    Atomicity is an indivisible and irreducible set of operations that must all occur or none occur.

2.    Consistency means the transaction brings the data only from one valid state to another valid state.

3.    Isolation guarantees that concurrent transactions produce the same data state that sequentially executed transactions would have produced.

4.    Durability ensures that committed transactions remain committed even in case of system failure or power outage.

** Type of SAGA – Choreography and Orchestration **

There are two common saga implementation approaches, choreography and orchestration. Each approach has its own set of challenges and technologies to coordinate the workflow.

1.    Choreography - Choreography is a way to coordinate sagas where participants exchange events without a centralized point of control. With choreography, each microservices run its own local transaction and publishes events to message broker system and that trigger local transactions in other microservices.

a.    Good for simple workflows that require few participants and don't need a coordination logic.

b.    Doesn't require additional service implementation and maintenance.

c.    Doesn't introduce a single point of failure, since the responsibilities are distributed across the saga participants.

2.    Orchestration is a way to coordinate sagas where a centralized controller tells the saga participants what local transactions to execute. The saga orchestrator handles all the transactions and tells the participants which operation to perform based on events. The orchestrator executes saga requests, stores and interprets the states of each task, and handles failure recovery with compensating transactions. This centralized controller microservice, orchestrate the saga workflow and invoke to execute local microservices transactions in sequentially. The orchestrator microservices execute saga transaction and manage them in centralized way and if one of the steps is failed, then executes rollback steps with compensating transactions.

a.    Good for complex workflows involving many participants or new participants added over time.

b.    Suitable when there is control over every participant in the process, and control over the flow of activities.

c.    Doesn't introduce cyclical dependencies, because the orchestrator unilaterally depends on the saga participants.

d.    Saga participants don't need to know about commands for other participants. Clear separation of concerns simplifies business logic.

** Two Phase Commit Or 2PC or 2 Phase Commit **

The 2PC could be alternative to SAGA pattern. The Two-Phase Commit protocol (2PC) is a widely used pattern to implement distributed transactions in microservices. In a two-phase commit protocol, there is a coordinator component that is responsible for controlling the transaction and contains the logic to manage the transaction. The other component is the participating nodes (e.g., the microservices) that run their local transactions. As the name indicates, the two-phase commit protocol runs a distributed transaction in two phases:

1.    Prepare Phase – The coordinator asks the participating nodes whether they are ready to commit the transaction. The participants returned with a yes or no.

2.    Commit Phase – If all the participating nodes respond affirmatively in phase 1, the coordinator asks all of them to commit. If at least one node returns negative, the coordinator asks all participants to roll back their local transactions.

Although 2PC is useful to implement a distributed transaction but it also has the following drawbacks.

1.    Coordinator node can become the single point of failure.

2.    All other services need to wait until the slowest service finishes its confirmation. So, the overall performance of the transaction is bound by the slowest service.

3.    The two-phase commit protocol is slow by design due to the chattiness and dependency on the coordinator. So, it can lead to scalability and performance issues in a microservice-based architecture involving multiple services.

4.    Two-phase commit protocol is not supported in NoSQL databases. Therefore, in a microservice architecture where one or more services use NoSQL databases, we can’t apply a two-phase commit. (and this is major fallback of 2PC)



** Difference between SAGA design pattern and 2PC design pattern **

2PC works as a single commit and aims to perform ACID transactions on distributed systems. It is used wherever strong consistency is important. On the other hand, SAGA works sequentially, not as a single commit. Each operation gets committed before the subsequent one, and this makes the data eventually consistent.


** Usage of this Design Pattern **

1.    Use this pattern when application needs to maintain data consistency across multiple microservices without tight coupling.

2.    2. Use this pattern when there are long-lived transactions and you don’t want other microservices to be blocked if one microservice runs for a long time.

3.    3. Use this pattern when You need to be able to roll back if an operation fails in the sequence.

4.    4. use this pattern to Ensure data consistency in a distributed system without tight coupling.

5.    5. use this pattern to Roll back or compensate if one of the operations in the sequence fails.


** Advantages of this Design Pattern **


1.    Best way to handle distributed transactions accross the microservices.

2.    2. Makes transaction management in a loosely coupled, message-driven.

3.    3. Well-suited to transactions that have a small number of steps (4 -5).

** Topics Covered **

1.    Introduction to Saga design pattern

2.    Agenda of this video

3.    Real world examples of Saga design pattern

4.    Types of Saga – Choreography and Orchestrator

5.    Saga Vs Two Phase Commit 2PC design pattern

6.    Difference between Saga and 2 phase commit patterns

7.    Usage of Saga design pattern

8.    Advantages of Saga design pattern

9.    Summary of Saga design pattern

10. Next video on Log Aggregator design pattern


** Chapter Timestamps **

0:00 Welcome to Saga design pattern

1:03 Agenda of tutorial

2:20 Introduction of Saga design pattern

5:05 Real world examples of Saga design pattern

7:14 Types of Saga Approach – Choreography

8:44 Types of Saga Approach – Orchestrator

10:43 Saga Vs Two Phase Commit 2PC design pattern

13:20 Difference between Saga and 2 phase commit patterns

13:56 Usage of Saga design pattern

14:48 Advantages of Saga design pattern

15:55 Summary of Saga design pattern

16:44 Next video on SAGA design pattern for microservices


#saga #choreography #orchestrator

 

** CHECK OUT OUR OTHER VIDEOS **

Shared Database per Service Design Pattern https://youtu.be/GI8kc0VnmWc

Difference between Monolithic and Microservice Architecture https://youtu.be/AkLxyMUyGg0

Spring boot project setup: https://youtu.be/bsgA20eJKxs

Spring Boot Microservice with postgres database Project:  https://youtu.be/iw4wO9gEb50

Prepare Docker file, Container and Build Image: https://youtu.be/g_pdTzjnuso

Deploy Docker Image AWS Elastic Container Service: https://youtu.be/ZlR5onuwZzw

Solid Principle Tutorial https://youtu.be/7d4ZrBfXweE

 

** CHECK OUR PLAYLISTS **

Microservice Architecture and Microservice Design Patterns Tutorial  https://youtube.com/playlist?list=PL2NZAYdLkYvgY74JFZMuluTJy-J_A_8NA

Spring Boot Complete Tutorial https://youtube.com/playlist?list=PL2NZAYdLkYvg_VlNmszrb-Um0wRx5yGDF

Docker Containers Complete Tutorial

https://youtube.com/playlist?list=PL2NZAYdLkYvhZQo2VTVCSug_zVjekNodi

Solid Principles Tutorial https://youtube.com/playlist?list=PL2NZAYdLkYvgB_35bYUnqP1p6v6P2Yb4-

Java Design Pattern Complete Tutorial with Examples  https://youtube.com/playlist?list=PL2NZAYdLkYvglL0xl-4tgBAribrcjeuNH



** ABOUT OUR CHANNEL **

CodeOneDigest is a youtube channel that produces videos on programming languages, cloud and container technologies, Software design principles, Java frameworks in English and Hindi languages.

Dosto, CodeOneDigest youtube channel pe aapko programming languages, container technology, cloud computing, software engineering se related videos milenge.


Check out our channel here:

https://www.youtube.com/channel/UC9V0QYsWKz_OD2uooCtEtRg

Don’t forget to subscribe!


** OUR WEBSITE **

https://codeonedigest.wordpress.com/


** GET IN TOUCH **

Email us on codeonedigest@gmail.com


FOLLOW US ON SOCIAL - LIKE, SHARE & SUBSCRIBE

Get updates or reach out to Get updates on our Social Media Profiles!

Subscribe: https://bit.ly/3NeWQ8U

Youtube: https://www.youtube.com/channel/UC9V0QYsWKz_OD2uooCtEtRg

Twitter: https://twitter.com/codeonedigest

Facebook: https://www.facebook.com/codeonedigest

Instagram: https://www.instagram.com/codeonedigest/

Linkedin: https://www.garudax.id/in/codeone-digest-10b418255/

Reddit: https://www.reddit.com/user/codeonedigest

Github: https://github.com/codeonedigest

Website: https://codeonedigest.wordpress.com/

Tumblr: https://www.tumblr.com/codeonedigest

Pinterest: https://in.pinterest.com/codeonedigest/

To view or add a comment, sign in

More articles by codeonedigest cod

Others also viewed

Explore content categories