Idempotency for Multi-Step Operations
You'll learn how to make complex workflows safely retryable by tracking which steps completed and resuming from the right place.
The Challenge of Multi-Step Workflows
When an operation involves multiple steps (like booking a flight and charging a card and sending confirmation), a retry after partial completion could cause chaos. You might charge the card twice or send duplicate emails. Simple idempotency keys aren't enough when only some steps succeeded before a timeout or crash.
Tracking Operation Stages
The solution is to store which stages completed alongside your idempotency key. Think of it like a checklist for a recipe: if you added flour but the power went out, you don't start over—you pick up at "add eggs."
When a retry arrives with the same idempotency key:
- Check the stored progress: Load the workflow state
- Skip completed steps: Don't re-charge that card
- Resume from failure point: Execute only remaining steps
- Return the final result: Whether fresh or from the previous attempt
Implementation Pattern
Store a status record:
- Idempotency key:
"abc-123" - Current stage:
"payment_completed" - Completed steps:
["validate", "reserve_seat", "charge_card"] - Pending steps:
["send_email", "update_inventory"]
On retry, compare the request against completed steps and execute only what's left. If the workflow already finished entirely, return the cached result immediately.
The Checklist Analogy
Imagine assembling furniture: you check off each step as you go. If interrupted, you consult your checklist rather than starting from scratch. Multi-step idempotency works the same way—progress tracking prevents redundant work and ensures each action happens exactly once.
Key Takeaway: Make multi-step workflows idempotent by persisting operation stages alongside idempotency keys, enabling safe re-execution that skips completed steps and resumes from the failure point.