What are the Saga design pattern and types available?
The Saga Pattern is a microservices architectural pattern to implement a transaction that extends multiple services.
A saga is a sequence of local transactions. Each service in a saga performs its own transaction and publishes an event. The other services listen to that event and perform the next local transaction. If one transaction fails for some reason, the saga also executes compensating transactions to undo the impact of the preceding transactions.
Consider a Food delivery service flow, When a user places an order, below could be the flow.
The food ordering service creates an order. At this point, the order is in PENDING status.
With the Saga pattern, it contacts the restaurant via the restaurant service.
The restaurant service attempts to place an order with the selected restaurant, Once the order is received by Restaurant it sends back an ORDER_ACCEPTED (if the order is accepted) or ORDER_REJECTED (if the order cannot be accepted) status.
The saga received the status ORDER_ACCEPTED or ORDER_REJECTED status and process the response to the customer accordingly.
The food order service then changes the status of the order, if the order is accepted then it displays an order confirmation message if the order is rejected it display an apologies message and suggest alternative restaurants.
Types of Saga:
In the next article, we will see in detail about the Type of Saga design patterns.
CQRS stands for Command Query responsibility segregator, a design pattern that separates the read and update operations for data.
In traditional architecture, the same data mode is used to query and update the data in a database, This is how the CRUD operations work. With CQRS the read and update operations are separated into two parts, Command and Query.
The command part will handle all the CREATE, UPDATE, DELETE requests, while the Query part will handle all the READ (Materialize view) requests.
Advantages of CQRS:
It also prevents update commends from causing merge conflicts at the domain level
Consider we have Video Services, One for Uploading (Write Operation) and another for viewing (Read Operation).
Where Uploading will perform Create, Update, Delete operations. And the other service on viewing will perform a Select operation to read the videos and their metadata.
In the next article, we will see the Circuit breaker design pattern.
Before understanding what is Event sourcing design pattern is, let us see why & when do we use the Event sourcing design pattern.
All application uses data to maintain the current state of the data by updating it, In the traditional Create, Read, Update & Delete (CRUD) model a data is read, then make some modification to It, and update the current state of the data with the new values – often by using transactions to lock the data.
CRUD has its own limitations, below are a few such limitations.
CRUD performs update operations directly against the data, which slowdowns the performance and limits the scalability.
When multiple users read and updates the data, more likely conflict occurs since the update operations take place on single data.
Unless there is a separate auditing mechanism that records the state of data as history or the state of the data is lost.
Now, this is why & when we need to use an Event sourcing design pattern.
Event sourcing design pattern:
Event Sourcing gives us a new way of persisting application state as an ordered sequence of events. We can selectively query these events and reconstruct the state of the application at any point in time. Of course, to make this work, we need to reimage every change to the state of the application as events:
Event sourcing design pattern
Consider we have Order and Cart services, When the Client adds the item to the cart or remove the items from the cart, these are called events, These events must be stored in order to maintain the changes made to the cart.
The events are persisted and published into the Event queue. Where the other services will subscribe to those events to get the updated state of the cart.
Consider you add the item to cart from Mobile App, then you login to website via PC and make some changes to the cart, these are captured as events and maintaining the seamless experience when accessing the cart in different platform.
In the next article, we will see the Branch design pattern.