Sagas: Distributed Transactions¶
When dealing with distributed systems, we will quickly realize that we need to execute an atomic transaction between multiple services. For the Suite, we have decided to use Sagas to solve this issue.
A Saga is a possibly long lived operation that is used to coordinate different backend services solve a single common goal.
Each service will perform a local atomic transaction, and the Saga is in charge of calling a service, waiting for it to finish doing it thing (using async communication) and then call the next service in the process, and so on.
When the operation of a service fails, the Saga will execute a series of compensating transactions to reverse the changes previously made. In practice, this probably means calling the services that the Saga has already called for them to execute other operation that reverses the changes.
This is in practice an orchestration approach, where we have a service (the director) directing what the Saga participants need to do. However, this is done through events, hence what is actually happening implementation wise is that events are published/consumed through the event broker, decreasing coupling. However, we must recognize that when we are using a Saga we are generating some coupling on purpose, most likely because the business logic requires us to do so.
Note
Bear in mind that Sagas that couple services are to be used in Services of the Application kind only.
Sagas are composed two pieces, a State Machine and its State Instance. The State Machine consumes events and triggers Activities, which can be used to modify the State Instance and transition to different States.
The State instance is usually persisted in a durable database, since this is an asynchronous operation that may be long lived due to the nature of the distributed system.
A Saga will continue to run even if some of its participants are down: whenever they receive the message from the broker and reply back to the Saga, the operation will continue.
The possibility of a usually short lived Saga to become long lived is determined by the reliability of its participants.