The Saga Pattern
Hi there!
Today (Jun 13th) I will speak about Implementing Saga Pattern using Temporal on The Developer’s Conference - Florianópolis. It’s a hybrid event, but only in Portuguese, this is why I didn’t mention here before.
Do you know what is a Saga? A Saga Pattern?
Saga was first a database concept. You can read this paper named Sagas from Hector Garcia-Molina and Keneth Salem. They define Saga as (LLT) that can be broken down into a collection of subtransactions that must be executed as a whole, or must be compensated for.
Now, we use it for distributed systems, so we can think a saga will happen between (micro)services.
Let’s use the Baeldung’s definition, and now we can say that the Saga Pattern is an architectural pattern for implementing a sequence of local transactions that helps maintain data consistency across different microservices.
Nice?
You have two options to implement a Saga Pattern: coreography vs orchestraded
On coreography, each service knows the next step and what call in case of compensation. This is a picture from my presentation in PT-BR, but Reservar Hotel == Book a Hotel ( rest is very easy to understand).
Benefits:
Easy service maintenance
No single point of failure
Low coupling
Risks:
Hard to maintain (reserve an extra attraction between car and flight)
Hard to monitor and debug (there is no single source of truth). What is the status of reservation X?
Each service needs to implement retry in case of failure, and if compensation is needed, it must call the appropriate service. And what if it fails?
For a orchestration system, we can see one microservice became the orchestrator and it’s responsible to make all the calls and compensations:
Benefits:
Easy service maintenance
Easy to monitor and debug (everything is on orchestrator)
Risks:
Scalability depends on the power of your orchestrator
Need implement retries
Orchestrator is a single point of failure
There is no right question in what is the best option, it will depend your context. In my case and presentation, I chose the orchestration, and to avoid risks, I’m using Temporal. Here is the github project with a Saga using Temporal. Hope you like it.
Please let me know if you have any doubt with the project, or want that I write more about Temporal.
Happy coding!