Saga

From G1Joshi/Agent-Skills · View interactive version

Saga is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Saga pattern implementation covering orchestration, choreography, and compensating transactions. Use when designing distributed transactions, multi-service workflows, or eventual consistency.

How to use this skill

Skill file

Saga Pattern

The Saga pattern manages data consistency across microservices in distributed transaction scenarios without two-phase commit (2PC). A Saga coordinates a sequence of local transactions, executing compensating transactions backward if any intermediate step fails.

When to Use

Quick Start

// Minimal Orchestrator-based Saga Definition
interface SagaStep<TContext> {
  name: string;
  execute: (ctx: TContext) => Promise<void>;
  compensate: (ctx: TContext) => Promise<void>;
}

async function runSaga<T>(context: T, steps: SagaStep<T>[]) {
  const executedSteps: SagaStep<T>[] = [];
  try {
    for (const step of steps) {
      console.log(`Executing step: ${step.name}`);
      await step.execute(context);
      executedSteps.push(step);
    }
    console.log("Saga completed successfully.");
  } catch (err) {
    console.error(
      `Saga failed. Rolling back compensating transactions...`,
      err,
    );
    for (const step of executedSteps.reverse()) {
      try {
        await step.compensate(context);
      } catch (compErr) {
        console.error(`Compensation failed for ${step.name}:`, compErr);
      }
    }
    throw err;
  }
}

Core Concepts

Orchestration vs Choreography Sagas

Sagas coordinate multi-service transactions using either a centralized orchestrator or decentralized events:

[ Orchestrator ] ──1. Reserve Stock──→ [ Inventory Service ]
                 ──2. Charge Card───→ [ Payment Service ] (FAILS!)
                 ──3. Compensate: Release Stock ──→ [ Inventory Service ]

Compensating Transactions (Semantic Rollback)

Because local transactions commit at each step, failures must be undone semantically through compensating transactions:

| Forward Transaction | Compensating Transaction |

| :----------------------------- | :----------------------------- |

| ReserveStock(orderId, items) | ReleaseStock(orderId, items) |

| ChargeCreditCard(amount) | RefundCreditCard(amount) |

| CreateShipmentLabel() | CancelShipmentLabel() |

Orchestrator State Machine Definition

Tracks workflow progression and triggers compensation on error:

// sagas/order-saga-orchestrator.ts
export class OrderSagaOrchestrator {
  async execute(order: OrderPayload) {
    let stockReserved = false;
    try {
      await inventoryService.reserve(order.id, order.items);
      stockReserved = true;

      await paymentService.charge(order.id, order.total);
      await shippingService.schedule(order.id);
    } catch (err) {
      console.error("Saga failed! Initiating compensation...", err);
      if (stockReserved) {
        await inventoryService.compensateRelease(order.id, order.items);
      }
      throw new Error("Order processing failed; changes compensated.");
    }
  }
}

Common Patterns

Idempotent Step Execution

Problem: Network retries during step execution can result in duplicate orders or double charges.

Solution:

Pass an idempotency key with every saga step command:

async function processPayment(orderId: string, idempotencyKey: string) {
  const existing = await paymentDb.findByKey(idempotencyKey);
  if (existing) {
    return existing.result; // Return previous outcome without recharging
  }
  const result = await paymentGateway.charge(orderId);
  await paymentDb.save({ idempotencyKey, orderId, result });
  return result;
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :------------------------- | :--------------------------------------------------- | :-------------------------------------------------------------------------------------------- |

| Saga stuck in PENDING | Orchestrator crashed or message dropped in queue. | Use persistent state machines (e.g., Temporal/Step Functions) with automated timeout retries. |

| Compensation Failed | Compensating action encountered non-retryable error. | Flag transaction for operator review and write to dead-letter queue (DLQ). |

| Duplicate Step Execution | Retry occurred without idempotency check. | Enforce unique idempotency keys per saga step execution. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.