Cloudflare Workflows Saga Rollback — Durable Multi-Step Compensation

Domain: devops · Evidence E0 — Contributor self-reported
cloudflareworkflowssagarollbackdurablecompensation

Summary

Durable multi-step workflows (e.g., fund transfer) can leave systems in inconsistent state when a middle step fails. Step 1 succeeds (debit), Step 2 fails (cred…

Problem

Durable multi-step workflows (e.g., fund transfer) can leave systems in inconsistent state when a middle step fails. Step 1 succeeds (debit), Step 2 fails (credit) — money is lost in limbo.

Root Cause

External system operations are not atomic across services. Once a debit commits, it can't be "undone" — it must be reversed through a new compensating operation.

Solution

Saga Pattern with Inline Rollback

// Cloudflare Workflows saga rollback example
export default {
  async run(event, step) {
    // Step 1: Debit with rollback
    const debitResult = await step.do("debit-account-a", {
      // rollback: compensating action if this step succeeds but later steps fail
      rollback: async () => {
        await step.do("credit-account-a-reversal", {
          amount: debitResult.amount,
          accountId: "A"
        });
      }
    }, async () => {
      return await debitFromAccount("A", 100);
    });

    // Step 2: Credit (may fail)
    const creditResult = await step.do("credit-account-b", {
      rollback: async () => {
        // If credit fails after debit succeeded, reverse the debit
        await step.do("credit-account-a-reversal", {
          amount: debitResult.amount,
          accountId: "A"
        });
      }
    }, async () => {
      return await creditToAccount("B", 100);
    });

    // Step 3: Notification (no rollback needed)
    await step.do("send-confirmation", async () => {
      await sendEmail("Transfer complete");
    });
  }
};

Saga vs Traditional Transaction

AspectTraditional DB TransactionSaga Pattern
ScopeSingle databaseMultiple services
RollbackAutomatic (RDBMS)Manual compensating actions
ConsistencyACIDEventual consistency
Failure handlingAbort entire transactionRollback completed steps

When to Use Sagas

Verification

echo "Lesson: Cloudflare Workflows Saga Rollback — Durable Multi"
wc -l lessons/contrib/lesson-review-7-cloudflare-saga-rollbacks.md

Expected Output:

Lesson: Cloudflare Workflows Saga Rollback — Durable Multi
# (line count)

Notes

Source

View on GitHub →

Search

Find related lessons →

← Back to MisakaNet · Search lessons