SAGA Design Pattern for Microservices Tutorial with Examples for Softwar...

SAGA Design Pattern for Microservices 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. ** 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. 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. ** 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. ** 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 ** 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. ** Advantages of this Design Pattern ** 1. Best way to handle distributed transactions across the microservices. 2. Makes transaction management in a loosely coupled, message-driven.

Comments

Popular posts from this blog

Ultimate Guide to Shopify Custom App & OAuth Flow | Create Shopify Custo...

Shopify Inventory Management | Manage Shopify Location & Product Invento...

Spring Boot Microservice with Postgres Database Running in Docker Contai...