architect-handbook

Software Architect Handbook

View on GitHub

Saga distributed transaction pattern

Overview

The Saga design pattern is a way to manage data consistency across microservices in distributed transaction scenarios.

A saga is a sequence of transactions that updates each service and publishes a message or event to trigger the next transaction step. If a step fails, the saga executes compensating transactions that counteract the preceding transactions.

Context and problem

Transactions must be atomic, consistent, isolated, and durable (ACID). Cross-service data consistency requires a cross-service transaction management strategy.

In multi-service architectures:

Database-per-microservice

A database-per-microservice model provides many benefits for microservices architectures. Encapsulating domain data lets each service use its best data store type and schema, scale its own data store as necessary, and be insulated from other services’ failures. However, ensuring data consistency across service-specific databases poses challenges.

Two-phase commit (2PC)

Distributed transactions like the two-phase commit (2PC) protocol require all participants in a transaction to commit or roll back before the transaction can proceed. However, some participant implementations, such as NoSQL databases and message brokering, don’t support this model.

Interprocess communication (IPC)

Operating system-provided IPC allows separate processes to share data but has limitations due to synchronicity and availability. For distributed transactions to commit, all participating services must be available, potentially reducing overall system availability.

Solution

The Saga pattern provides transaction management using a sequence of local transactions.

A local transaction is the atomic work effort performed by a saga participant. 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, the saga executes a series of compensating transactions that undo the changes that were made by the preceding local transactions.

When to use

Use the Saga pattern when you need to:

The Saga pattern is less suitable for:

Issues and considerations

Common concerns

Consider the following points when implementing the Saga pattern:

Anomalies

The following anomalies can appen without proper measures:

Countermeasures

Suggested countermeassures to reduce or prevent anomalies include:

Transaction types

In Saga patterns:

Implementation

There are two common saga implementation approaches, choreography and orchestration. Each one has its own set of challenges and technologies to coordinate the workflow.

Choreography

Choreography is a way to coordinate sagas where participants exchange events without a decentralized point of control.

With choreography, each local transaction publishes domain events that trigger local transactions in other services.

Choreography - Benefits

Choreography - Drawbacks

Ochestration

Orchestation is a way to coordinate sagas where a centralized controller tells the saga participants what local transactions to execute. The saga orchestator handles all the transactions and tells the participants which operation to perform based on events.

The orchestrator executes saga requests, stores, and interprets the states of each task, and handles failure recovery with compensating transactions.

Orchestration - Benefits

Orchestration - Drawbacks