# Saga Pattern

> Last updated: 2026-12-03.
> For the complete documentation index, see [llms.txt](https://docs.temporal.io/llms.txt).
> Any documentation page is available as raw Markdown by appending `.md` to its URL.
> For related questions, query `https://docs.temporal.io/api/retrieve?q=<question>&jtb=<job-to-be-done>&src=md-page&agent=<your-name>` (no auth; returns JSON `{content, source_url, page_url}`, with `source_url` pointing to Markdown for docs pages). Don't include personal data or credentials.
> If this page is wrong or missing something you needed, or Temporal behaves differently from what it says, report it with `https://docs.temporal.io/api/feedback?title=<summary>&message=<what-happened>&category=documentation&page_url=/design-patterns/saga-pattern&src=md-page&agent=<your-name>` (use `category=product` when the product, not the page, is wrong). Report only a specific problem you hit, send it once, and don't include personal data or credentials.

> Manages distributed transactions with compensating actions. Each step has a compensation that undoes its effects if subsequent steps fail.

## Overview

The Saga pattern manages distributed transactions across multiple services by coordinating a sequence of local transactions, each with a compensating action that can undo its effects if subsequent steps fail.

## Problem

In distributed systems, you need to maintain data consistency across multiple services or databases without using traditional ACID transactions.
When a multi-step business process fails partway through, you must undo the effects of completed steps to maintain system consistency.
Traditional two-phase commit does not scale well and creates tight coupling between services.

## Solution

You implement each step as a local transaction with a corresponding compensation transaction.
If any step fails, you execute compensation transactions in reverse order to undo the effects of all completed steps.
You register compensations as each step completes, then automatically trigger them when errors occur to ensure cleanup happens reliably.

The following diagram shows the worked example used by the runner: opening a customer account in four steps, where `addBankAccount` simulates a downstream failure to trigger compensation.

```mermaid
flowchart TD
    Start([Start Saga]) --> Step1[Step 1: createAccount]
    Step1 -->|Success| Step2[Step 2: addAddress]
    Step1 -->|Failure| End([End: Failed])

    Step2 -->|Success| Step3[Step 3: addClient]
    Step2 -->|Failure| Comp1[clearPostalAddresses]

    Step3 -->|Success| Step4[Step 4: addBankAccount]
    Step3 -->|Failure| Comp2[removeClient]

    Step4 -->|Success| Complete([End: Success])
    Step4 -->|Failure| Comp3[disconnectBankAccounts]

    Comp3 --> Comp2
    Comp2 --> Comp1
    Comp1 --> End
    classDef success stroke-width:1px
    classDef compensation stroke-width:1px
    classDef complete stroke-width:1px
    classDef fail stroke-width:1px
    class Step1,Step2,Step3,Step4 success
    class Comp1,Comp2,Comp3 compensation
    class Complete complete
    class End fail
```

The following describes each step in the diagram:

1. The Saga begins by executing Step 1 (`createAccount`).
2. If Step 1 succeeds, the Workflow proceeds to Step 2 (`addAddress`). If it fails, the Saga ends immediately — no compensations are registered yet.
3. If Step 2 succeeds, the Workflow proceeds to Step 3 (`addClient`). If it fails, the Workflow runs `clearPostalAddresses`.
4. If Step 3 succeeds, the Workflow proceeds to Step 4 (`addBankAccount`). If it fails, the Workflow runs `removeClient`, then `clearPostalAddresses`.
5. If Step 4 succeeds, the Saga completes. If it fails, the Workflow runs all three compensations in reverse: `disconnectBankAccounts`, `removeClient`, `clearPostalAddresses`. Note that `disconnectBankAccounts` is registered before `addBankAccount` runs, so it executes even if `addBankAccount` failed mid-flight — its implementation must be idempotent.

## Implementation

The following examples show how each SDK implements the Saga pattern.
Each language uses a different mechanism to register and execute compensations, but the core principle is the same: register a compensation before each step, and run all compensations in reverse order on failure.

**Python**

<!--SNIPSTART python-pattern-saga-pattern-workflow-->
[python-sandbox/design-patterns/saga/workflows.py](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/python-sandbox/design-patterns/saga/workflows.py)
```py
@workflow.defn
class OpenAccountWorkflow:
    @workflow.run
    async def run(self, req: OpenAccountRequest) -> str:
        # Compensation Activities, in the order they were registered.
        compensations = []

        try:
            # Step 1: create_account has no compensation. Leaving an empty
            # account stub on later failure is acceptable.
            await self.run_step(create_account, req)

            # Register compensation for Step 2 BEFORE execution
            compensations.append(clear_postal_addresses)
            await self.run_step(add_address, req)

            # Register compensation for Step 3 BEFORE execution
            compensations.append(remove_client)
            await self.run_step(add_client, req)

            # Register compensation for Step 4 BEFORE execution
            compensations.append(disconnect_bank_accounts)
            await self.run_step(add_bank_account, req)

            return f"Account {req.account_id} opened"
        except (ActivityError, asyncio.CancelledError):
            # On failure or cancellation, run the compensations in reverse order.
            for compensation in reversed(compensations):
                try:
                    await self.run_step(compensation, req)
                except ActivityError as err:
                    # Log the failure and keep compensating.
                    workflow.logger.error(f"Compensation failed: {err.cause}")
            # Re-raise so the Workflow Execution reports the original failure
            raise

    @staticmethod
    async def run_step(step, req: OpenAccountRequest) -> None:
        await workflow.execute_activity(
            step, req, start_to_close_timeout=timedelta(seconds=10)
        )
```
<!--SNIPEND-->

**Go**

<!--SNIPSTART go-pattern-saga-pattern-workflow-->
[go-sandbox/design-patterns/saga/workflow.go](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/go-sandbox/design-patterns/saga/workflow.go)
```go
func OpenAccount(ctx workflow.Context, req OpenAccountRequest) (result string, err error) {
	ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
		StartToCloseTimeout: 10 * time.Second,
	})

	// Compensation Activities, in the order they were registered.
	var compensations []any

	// On failure or cancellation, run the compensations in reverse order.
	defer func() {
		if err == nil {
			return
		}
		// A disconnected context keeps the Activity options but not the
		// cancellation, so compensations still run after the Workflow is canceled.
		compensateCtx, _ := workflow.NewDisconnectedContext(ctx)
		for i := len(compensations) - 1; i >= 0; i-- {
			compErr := workflow.ExecuteActivity(compensateCtx, compensations[i], req).Get(compensateCtx, nil)
			if compErr != nil {
				// Log the failure and keep compensating. The original error is still returned.
				workflow.GetLogger(ctx).Error("Compensation failed", "Error", compErr)
			}
		}
	}()

	// Step 1: CreateAccount has no compensation. Leaving an empty account
	// stub on later failure is acceptable.
	if err = workflow.ExecuteActivity(ctx, CreateAccount, req).Get(ctx, nil); err != nil {
		return "", err
	}

	// Register compensation for Step 2 BEFORE execution
	compensations = append(compensations, ClearPostalAddresses)
	if err = workflow.ExecuteActivity(ctx, AddAddress, req).Get(ctx, nil); err != nil {
		return "", err
	}

	// Register compensation for Step 3 BEFORE execution
	compensations = append(compensations, RemoveClient)
	if err = workflow.ExecuteActivity(ctx, AddClient, req).Get(ctx, nil); err != nil {
		return "", err
	}

	// Register compensation for Step 4 BEFORE execution
	compensations = append(compensations, DisconnectBankAccounts)
	if err = workflow.ExecuteActivity(ctx, AddBankAccount, req).Get(ctx, nil); err != nil {
		return "", err
	}

	return fmt.Sprintf("Account %s opened", req.AccountID), nil
}
```
<!--SNIPEND-->

**Java**

<!--SNIPSTART java-pattern-saga-pattern-workflow-->
[java-sandbox/design-patterns/saga/src/main/java/io/temporal/docs/designpatterns/saga/OpenAccountWorkflowImpl.java](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/java-sandbox/design-patterns/saga/src/main/java/io/temporal/docs/designpatterns/saga/OpenAccountWorkflowImpl.java)
```java
public class OpenAccountWorkflowImpl implements OpenAccountWorkflow {
  private static final Logger logger = Workflow.getLogger(OpenAccountWorkflowImpl.class);

  private final AccountActivities activities =
      Workflow.newActivityStub(
          AccountActivities.class,
          ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(10)).build());

  @Override
  public String openAccount(OpenAccountRequest req) {
    // Run compensations one at a time, and keep going if one of them fails.
    Saga saga =
        new Saga(
            new Saga.Options.Builder()
                .setParallelCompensation(false)
                .setContinueWithError(true)
                .build());

    try {
      // Step 1: createAccount has no compensation. Leaving an empty account
      // stub on later failure is acceptable.
      activities.createAccount(req);

      // Register compensation for Step 2 BEFORE execution
      saga.addCompensation(activities::clearPostalAddresses, req);
      activities.addAddress(req);

      // Register compensation for Step 3 BEFORE execution
      saga.addCompensation(activities::removeClient, req);
      activities.addClient(req);

      // Register compensation for Step 4 BEFORE execution
      saga.addCompensation(activities::disconnectBankAccounts, req);
      activities.addBankAccount(req);

      return "Account " + req.accountId() + " opened";
    } catch (TemporalFailure e) {
      // On failure or cancellation, run the compensations in reverse order.
      // A detached scope keeps them running after the Workflow is canceled.
      Workflow.newDetachedCancellationScope(
              () -> {
                try {
                  saga.compensate();
                } catch (Saga.CompensationException compensationFailure) {
                  // Log the failure. The original error is still re-thrown.
                  logger.error("Compensation failed", compensationFailure);
                }
              })
          .run();
      // Re-throw so the Workflow Execution reports the original failure
      throw e;
    }
  }
}
```
<!--SNIPEND-->

**TypeScript**

<!--SNIPSTART typescript-pattern-saga-pattern-workflow-->
[typescript-sandbox/src/design-patterns/saga/workflows.ts](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/typescript-sandbox/src/design-patterns/saga/workflows.ts)
```ts
type Compensation = () => Promise<void>;

export async function openAccount(req: OpenAccountRequest): Promise<string> {
  const compensations: Compensation[] = [];

  try {
    // Step 1: createAccount has no compensation. Leaving an empty account
    // stub on later failure is acceptable.
    await acts.createAccount(req);

    // Register compensation for Step 2 BEFORE execution
    compensations.unshift(() => acts.clearPostalAddresses(req));
    await acts.addAddress(req);

    // Register compensation for Step 3 BEFORE execution
    compensations.unshift(() => acts.removeClient(req));
    await acts.addClient(req);

    // Register compensation for Step 4 BEFORE execution
    compensations.unshift(() => acts.disconnectBankAccounts(req));
    await acts.addBankAccount(req);

    return `Account ${req.accountId} opened`;
  } catch (err) {
    // On failure or cancellation, run the compensations in reverse order
    // (unshift keeps them in LIFO order). A non-cancellable scope keeps them
    // running after the Workflow is canceled.
    await CancellationScope.nonCancellable(async () => {
      for (const compensate of compensations) {
        try {
          await compensate();
        } catch (compensationErr) {
          // Log the failure and keep compensating.
          log.error('Compensation failed', { error: compensationErr });
        }
      }
    });
    // Re-throw so the Workflow Execution reports the original failure
    throw err;
  }
}
```
<!--SNIPEND-->

The SDKs differ in how they track compensations and keep them running after the Workflow is canceled:

- Go keeps a slice of compensation Activities and runs them in reverse order in a deferred function, with a context from `workflow.NewDisconnectedContext`.
- Python keeps a list of compensation Activities, runs it with `reversed()`, and catches `asyncio.CancelledError` as well as `ActivityError`.
- TypeScript adds each compensation to the front of an array with `unshift()` and runs the array inside `CancellationScope.nonCancellable`.
- Java tracks compensations with the SDK's `Saga` helper, where `setContinueWithError(true)` keeps going after a failed compensation, and calls `saga.compensate()` inside `Workflow.newDetachedCancellationScope`.

In every SDK, compensations are registered before Activity execution, run in reverse order of registration, and keep running when one of them fails.
All compensations must be idempotent and able to handle cases where the forward Activity never executed.

### When to register compensations

There are two approaches for when to register compensation Activities:

1. **Register before Activity execution** (recommended for safety): This ensures the compensation runs even if the Activity fails after partial completion. For example, a credit card may be charged but the Activity fails before returning success. The compensation must be idempotent and handle cases where the forward Activity never executed (no-op). This is the safer default when Activities have side effects that may occur before failure.

2. **Register after Activity execution** (appropriate when safe): This only compensates Activities that completed successfully. The compensation logic is simpler because you do not need to check whether the forward action occurred. This approach is appropriate when Activities are truly atomic (all-or-nothing). The risk is partial completion without compensation if the Activity fails mid-execution.

The choice depends on your Activity's failure characteristics and whether the compensation can safely handle cases where the forward Activity never executed.
When in doubt, register compensations before execution and ensure they are idempotent.

## When to use

The Saga pattern is a good fit when you need to maintain consistency across multiple services or databases, traditional distributed transactions (two-phase commit) are too slow or unavailable, you can define compensating actions for each step in your business process, eventual consistency is acceptable for your use case, and you need to handle long-running transactions that may span hours or days.

It is not a good fit for operations that require strong ACID consistency, single-service transactions that can use a local database transaction, processes where compensations cannot be defined, or operations that must appear atomic to external observers.

## Benefits and trade-offs

The Saga pattern maintains eventual consistency without distributed locks, and each service can use its own database and transaction model.
Temporal's durable execution guarantees that compensations will execute even after Worker failures.
The pattern scales better than two-phase commit protocols.

The trade-offs to consider are that only eventual consistency is provided — intermediate states are visible to other processes.
You must design idempotent compensation Activities, and compensation logic must be maintained alongside forward logic.
Some operations may not have meaningful compensations.

## Comparison with alternatives

| Approach | Consistency | Rollback mechanism | Coupling | Scalability |
| :--- | :--- | :--- | :--- | :--- |
| Saga (orchestration) | Eventual | Compensating transactions | Loose | High |
| Two-phase commit | Strong (ACID) | Distributed lock/rollback | Tight | Low |
| Saga (choreography) | Eventual | Event-driven compensations | Very loose | High |
| Local transaction | Strong (ACID) | Database rollback | None | Single service |

## Best practices

- **Make all compensations idempotent.** Compensations may run even when the forward Activity never executed (if registered before execution) or may run multiple times on retry. Use idempotency keys to ensure safe re-execution.
- **Register compensations before Activity execution.** This ensures cleanup runs even if the Activity fails after partial completion. The compensation must handle the case where the forward action never occurred (no-op).
- **Use idempotency keys for forward Activities.** Pass a unique identifier (such as a client ID or Workflow ID) to each Activity so retries do not create duplicate side effects.
- **Set StartToCloseTimeout on compensation Activities.** Set a `StartToCloseTimeout` but avoid `ScheduleToCloseTimeout` on compensations. Do not set Workflow-level timeouts — let compensations retry until they succeed.
- **Run compensations in a scope that cancellation can't interrupt.** A [canceled scope can't schedule new Activities](/encyclopedia/workflow/cancellation-and-termination#what-happens-when-you-cancel-a-workflow), so compensations started from it fail at once. The [Implementation](#implementation) examples show the mechanism for each SDK.
- **Keep compensation payloads small.** Pass references (IDs, URLs) instead of full data objects to avoid exceeding the 2 MB payload limit.
- **Log compensation failures but continue.** If a compensation fails, log the error and continue executing remaining compensations. In production, alert for manual intervention on persistent compensation failures.
- **Re-throw the original error after compensating.** Always re-throw the original exception after running compensations so the Workflow reports the correct failure reason.

## Common pitfalls

- **Non-idempotent compensations.** Compensations may run even when the forward Activity never executed (if registered before execution) or may run multiple times on retry. All compensations must be idempotent.
- **Forgetting to register a compensation.** If a step succeeds but its compensation was never registered, a later failure leaves that step's effects permanently in place.
- **Compensations that can fail permanently.** If a compensation Activity fails with a non-retryable error, the Saga cannot fully roll back. Design compensations with generous retry policies.
- **Large payloads in compensation state.** Passing large objects through the compensation chain can exceed the 2 MB payload limit. Use references (IDs, URLs) instead of full data.
- **Swallowing the ContinueAsNew exception in TypeScript.** In TypeScript, `continueAsNew` works by throwing a special exception. A `catch` block that does not re-throw it, or a `finally` block that returns a value, silently prevents Continue-As-New.

## Related

### Patterns

- **[Error Handling & Retry Patterns](/design-patterns/error-handling-patterns)**: Often combined with the Saga pattern to handle transient failures before compensating.
- **[Child Workflows](/design-patterns/child-workflows)**: You can use Child Workflows to organize complex Sagas with multiple sub-Sagas.
- **[Long-Running Activity](/design-patterns/long-running-activity)**: Heartbeats work well with long-running compensation Activities.
- **[Early Return](/design-patterns/early-return)**: You can combine Early Return with the Saga pattern to return initialization results before compensation runs.

### Guides

- [Recover business processes without restarting](/guides/recover-without-restart): Adds LIFO saga compensation to a multi-step loan pipeline, with pre-registered compensations and a recoverable rollback loop for compensations that fail.

### Sample code

- [Go Sample](https://github.com/temporalio/samples-go/tree/main/saga) — Saga with `defer`-based compensations.
- [Java Sample](https://github.com/temporalio/samples-java/tree/main/core/src/main/java/io/temporal/samples/hello/HelloSaga.java) — Saga with the `Saga` API.
- [TypeScript Sample](https://github.com/temporalio/samples-typescript/tree/main/saga) — Saga with array-based compensations.
- [TypeScript runnable sample](https://github.com/temporalio/documentation-sdk-code-examples/tree/main/typescript-sandbox/src/design-patterns/saga) — The code on this page, with a Worker, a starter, and tests.
- [Python Sample](https://github.com/temporalio/samples-python) — Saga with list-based compensations.
- [.NET Sample](https://github.com/temporalio/samples-dotnet/tree/main/src/Saga) — Saga with `try`/`catch` compensations.
- [Ruby Sample](https://github.com/temporalio/samples-ruby/tree/main/saga) — Saga with `rescue`-based compensations.
- [Python SDK implementation guide](/develop/python/best-practices/error-handling#implement-saga-pattern) — Full walkthrough of the compensation-list technique, including retry and rollback code.
