Saga Pattern

richardson.2017.microservicespatternsaga (pg. 1)

Implement each business transaction that spans multiple services as a saga. A saga is a sequence of local transactions. Each local transaction updates the database and publishes a message or event to trigger the next local transaction in the saga. If a local transaction fails because it violates a business rule then the saga executes a series of compensating transactions that undo the changes that were made by the preceding local transactions.

google.2023.interservicecommunicationmicroservices (pg. 3)

Saga: The Saga pattern consists of running local transactions within each microservice that make up the distributed transaction. An event is triggered at the end of every successful or failed operation. All microservices involved in the distributed transaction subscribe to these events. If the following microservices receive a success event, they execute their operation. If there is a failure, the preceding microservices complete compensating actions to undo changes. Saga provides a consistent view of the system by guaranteeing that when all steps are complete, either all operations succeed or compensating actions undo all the work.

Distributed Transactions vs. Saga

ferreiraloff.etal.2023.antipodeenforcingcrossservice (pg. 13)

Distributed transactions can also be applied in this context, e.g., in the form of 2PC protocols [15]. Just as coordination-based approaches, distributed transactions suffer from low performance [61]. Faced with this problem, the community migrated towards an approach known as Sagas [37].

Recommendations by Google:

google.2023.interservicecommunicationmicroservices (pg. 3)

We recommend Saga for long-lived transactions. In a microservices-based application, you expect interservice calls and communication with third-party systems. Therefore, it's best to design for eventual consistency: retry for recoverable errors and expose compensating events that eventually amend non-recoverable errors.

google.2023.interservicecommunicationmicroservices (pg. 3)

There are various ways to implement a Sagaβ€”for example, you can use task and workflow engines such as Apache Airflow,, Apache Camel, or Conductor. You can also write your own event handlers using systems based on Kafka, RabbitMQ, or ActiveMQ.

google.2022.transactionalworkflowsmicroservices (pg. 7)

When you consider whether to implement a choreography-based saga or synchronous orchestration, the best choice for your organization is always the pattern which is most suitable for its needs. However, in general, because of the simplicity of its design, synchronous orchestration is often the first choice for many enterprises.

Challenges

richardson.2017.microservicespatternsaga (pg. 3)

In order to be reliable, a service must atomically update its database and publish a message/event. It cannot use the traditional mechanism of a distributed transaction that spans the database and the message broker.

ferreiraloff.etal.2023.antipodeenforcingcrossservice (pg. 14)

Although sagas gained acceptance within the community [22, 28], they fall short when compensating mechanisms are not possible or hard to achieve. Reversal is especially challenging when transactions trigger third-party side effects [4]. Furthermore, Sagas often still rely on an orchestrator-like entity that sequences the steps of a saga.

[22] Chris Richardson (2021): Microservices.io
[28] Eventuate (2021): Eventuate

How to implement?

Because of the complexity you should use a framework for it!

Which framework?

πŸ”— References

Notes:

Books:

Articles:

Papers: