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…
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.
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.
// 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");
});
}
};| Aspect | Traditional DB Transaction | Saga Pattern |
|---|---|---|
| Scope | Single database | Multiple services |
| Rollback | Automatic (RDBMS) | Manual compensating actions |
| Consistency | ACID | Eventual consistency |
| Failure handling | Abort entire transaction | Rollback completed steps |
echo "Lesson: Cloudflare Workflows Saga Rollback — Durable Multi"
wc -l lessons/contrib/lesson-review-7-cloudflare-saga-rollbacks.mdExpected Output:
Lesson: Cloudflare Workflows Saga Rollback — Durable Multi
# (line count)