NOTE

2.5 Distributed Transaction Solution: Saga

Saga gives each participant a forward operation and a compensating operation. If a step fails, compensation handles operations already completed.

Distributed SystemsCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What Is Saga?

A distributed transaction solution that guarantees eventual consistency.

2. Saga Process

  • There are multiple transaction participants, and each participant has two operations: a forward operation and a compensating operation.
  • Divide the transaction into two phases:
    • In the first phase, each participant executes the forward operation.
    • The second phase depends on the result of the first phase.
      • If all forward operations execute successfully, the distributed transaction commits.
      • If any forward operation fails, execute compensating operations for the preceding participants.

3. TCC vs Saga

  • Similarities:
    • Both have two phases.
    • Both need to implement compensating operations.
  • Differences:
    • TCC is synchronous, while Saga is asynchronous (event-driven).
    • TCC needs to reserve resources in the first phase, while Saga does not.

4. Use Cases for Saga

  • Multiple services use multiple data sources, and each data source can be different.
  • Participants include services from other companies or legacy systems that cannot provide the three interfaces required by the TCC model.
  • Business processes are long and have many steps.

5. Problems with Saga

Does not guarantee isolation.

6. Saga Implementation

6.1. Seata Saga Mode

7. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub