These days I am researching the Microservice inter-service communication patterns. So during my research, I found that there are two patterns called SAGA and event sourcing. But I couldn't find a resource on the internet to learn the difference between the two patterns. I mean I know event sourcing will capture the history of the event with the aid of an event store. So as per my understandings, I feel that event sourcing is like an extended version of choreography-based SAGA pattern. So I need to clarify is my argument acceptable or not. I will attach sample diagrams of the two patterns which I found on the internet below. Please use those diagrams during any of your explanations.
References
Solution. The Event Sourcing pattern defines an approach to handling operations on data that's driven by a sequence of events, each of which is recorded in an append-only store.
The saga pattern is a failure management pattern that helps establish consistency in distributed applications, and coordinates transactions between multiple microservices to maintain data consistency.
The Saga design pattern is a way to manage data consistency across microservices in distributed transaction scenarios. A saga is a sequence of transactions that updates each service and publishes a message or event to trigger the next transaction step.
There are two ways of coordination sagas: Choreography - each local transaction publishes domain events that trigger local transactions in other services. Orchestration - an orchestrator (object) tells the participants what local transactions to execute.
A choreography saga pattern is a method to organise sagas where participants swap events without a centralized control point. On the other hand, an orchestration saga pattern is a method to coordinate sagas through a centralized controller that informs the saga participants what local transactions should be executed.
In Choreograph-based saga approach, each local transaction emits domain events. This events then triggers local transaction in other services subscribed to these events. So assuming there are three services A, B and C. When a local transaction occurs in A, it publishes an event that triggers a local transaction in B.
Event Choreography Choreography is driven by events from the various services within a system. Each service consumes events, performs an action, and publishes events. There is no centralized coordination or logic.
How Choreography-based Saga Works in Microservices - Microservices . In this article we would explain how a Choreography-based Saga works in microservices. Then present a simple algorithm. In Choreograph-based saga approach, each local transaction emits domain events.
The two are compatible patterns that address different problems, Sagas handle workflow processes where as event sourcing addresses how state is stored. Sagas provide a mechanism for handling a multi-step process and rolling back should steps fail (like a workflow). Where as Event Sourcing is the process of encoding the state of an entity by recording all its past changes.
Lets say we are booking a holiday, we need to book flights, a hotel and hire a car. each of these processes is handled by a different microservice.
We could create a microservice named BookingSaga which would be responsible for keeping track of the state of each booking. When we make a booking the BookingSaga service would
these can reply in any order, but if any one fails the BookingSaga would then begin a rollback and cancel any that had already been booked.
https://microservices.io/patterns/data/saga.html
Event sourcing keeps track of the state of some entity by recording the changes that have happened to it.
So we can see that Object A has a name of "sue" and an Age of 3 at the end of all the events. https://microservices.io/patterns/data/event-sourcing.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With