DEV Community

Cover image for The Saga Pattern Explained: Managing Distributed Transactions in Microservices
Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

The Saga Pattern Explained: Managing Distributed Transactions in Microservices

This article was originally published on bmf-tech.com.

Overview

Notes on what I researched about the Saga pattern.

What is the Saga Pattern

  • In microservices, distributed transactions (such as 2phase commit) are not recommended
    • The Saga pattern is used to ensure consistency while avoiding distributed transactions
  • Avoids long-term locks and uses eventual consistency
  • Compensating transactions
    • Operations that cancel a series of transactions
    • The Saga pattern prohibits simple rollbacks
  • Not a pattern unique to microservices, it was also used in SOA
  • Implementation patterns
    • Choreography
    • Each service progresses the transaction with its own responsibility
    • Avoids SPOF, but makes overall testing difficult
    • Orchestration
    • A central service is prepared to instruct each service to progress the transaction
    • Prone to SPOF, but makes overall testing easier
  • Software Architecture: The Hard Parts introduces eight types of Saga patterns

Other Patterns

Another pattern to maintain consistency in microservices, similar to the Saga pattern, is the TCC (Try-Confirm/Cancel) pattern, which also uses eventual consistency.

The TCC pattern is similar to 2phase commit, but in the TCC pattern, each service has three steps: preparation, confirmation, and cancellation of the transaction.

The TCC pattern does not perform rollbacks like compensating transactions, but ensures consistency by not performing processes that cause inconsistencies.

References

Top comments (0)