Design Patterns for Microservice Architecture – Saga

If you use Microservice Architecture with Database per Microservice, then managing consistency via distributed transactions is challenging. You cannot use the traditional Two-phase commit protocol as it either does not scale (SQL Databases) or is not supported (many NoSQL Databases).

You can use the Saga pattern for distributed transactions in Microservice Architecture. Saga is an old pattern developed in 1987 as a conceptual alternative for long-running database transactions in SQL databases. But a modern variation of this pattern works amazingly for the distributed transaction as well. Saga pattern is a local transaction sequence where each transaction updates data in the Data Store within a single Microservice and publishes an Event or Message. The first transaction in a saga is initiated by an external request (Event or Action). Once the local transaction is complete (data is stored in Data Store, and message or event is published), the published message/event triggers the next local transaction in the Saga.

If the local transaction fails, Saga executes a series of compensating transactions that undo the preceding local transactions’ changes.

There are mainly two variations of Saga transactions co-ordinations:

  • Choreography: Decentralised co-ordinations where each Microservice produces and listen to other Microservice’s events/messages and decides if an action should be taken or not.
  • Orchestration: Centralised co-ordinations where an Orchestrator tells the participating Microservices which local transaction needs to be executed.

Pros

  • Provide consistency via transactions in a highly scalable or loosely coupled, event-driven Microservice Architecture.
  • Provide consistency via transactions in Microservice Architecture where NoSQL databases without 2PC support are used.

Cons

  • Need to handle transient failures and should provide idempotency.
  • Hard to debug, and the complexity grows as the number of Microservices increase.

When to use Saga

  • In highly scalable, loosely coupled Microservice Architecture where event sourcing is used.
  • In systems where distributed NoSQL databases are used.

When not to use Saga

  • Lowly scalable transactional systems with SQL Databases.
  • In systems where cyclic dependency exists among services.

Enabling Technology Examples

AxonEventuateNarayana

SiteLock