- A sequence of asynchronous local transactions.
- Each local transaction updates data within a single service.
- A service publishes an asynchronous message when a local transaction completes. The message triggers the local transaction in the next service.
- Use compensating transactions to rollback changes
- Each local transaction has a compensating transaction to undo the change.
- If one transaction fails, the saga needs to execute the compensating transaction of preceding transactions in reverse order.
- Choreography
- Concepts
- Saga participants coordinate a saga operation by themselve.
- A saga participant sends a message to the next saga participant triggering its local transaction.
- Pros
- Easy to implement.
- Avoid the single point of failure of the saga orchestrator in the orchestration saga.
- Cons
- Risk of cyclic dependencies.
- More difficult to understand (There is no a single place in the code that defines the saga).
- Concepts
- Orchestration
- Concepts
- A saga orchestrator coordinates a saga operation with saga participants.
- A saga orchestrator sends messages to saga participants telling them which transactions to perform.
- Pros
- Easy to understand (The code in orchestrator defines the saga).
- Avoid the risk of cyclic dependencies.
- Improves separation of the saga coordination logic (in the saga orchestrator) and the business logic (in saga participants).
- Cons
- Saga orchestrator has risk to become a single point of failure.
- The risk of centralizing too much business logic in the orchestrator.
- Concepts
- Maintains the data consistency of an application across multiple services.
- Supports for long-lived transactions (Other microservices are not blocked if a microservice is running for a long time).
- Increases the complexity of an application (difficult to design, debug, etc.).
- Lack of isolation between transactions.
Topic | Consideration | Possible Solution Options |
---|---|---|
Concurrency | Multiple Sagas can run concurrently and cause the following problems: |
- Book: Chris R.(2018). Chapter 4. Managing transactions with sagas, Microservices Patterns (pp. 110-145). Manning Publications
- Web Article: Pattern: Saga | https://microservices.io/patterns/data/saga.html
- Web Article: 分布式事务基本原理 | https://github.com/dunwu/blog/blob/master/source/_posts/distributed/distributed-transaction.md
- Web Article: Patterns for distributed transactions within a microservices architecture | https://developers.redhat.com/blog/2018/10/01/patterns-for-distributed-transactions-within-a-microservices-architecture/