Multi-step Processes
A business process that crosses databases or external services cannot usually be rolled back like one local transaction.
What, why, when and how
What is it?
A multi-step process coordinates a business operation across independently committed actions and records durable progress.
Why is it needed?
A crash between reserving inventory and collecting payment can otherwise leave the product in an unexplained partial state.
When should I use it?
Use durable workflows when an operation crosses systems, waits for callbacks, or requires retries and compensating actions.
When should I choose something else?
Keep a single local transaction when all required changes share one database boundary. A workflow is not a distributed rollback that erases external effects.
How do I use it?
Persist intent, give each step an identity, record outcomes, retry safe actions, reconcile uncertain responses and define compensation for completed steps.
Can you show a concrete example?
An order reserves stock, requests payment and times out. Record payment as uncertain and query its stable provider identity before trying again; release stock only through the declared cancellation policy.
Read the diagram
Order o7 reserves stock; payment requested; Provider response times out: outcome unknown; Reconcile the same payment ID before retrying
Follow the detailed diagrams below to trace the additional state changes and failure cases behind these answers.
A business process that crosses databases or external services cannot usually be rolled back like one local transaction. Model it as durable state with explicit transitions, retries, and repair. The important question is what happens after a crash between steps, especially when the previous step may already have created an external effect.
Learning goals
Durable state machines; orchestration; outbox; sagas; idempotent activities; compensation; timeouts; reconciliation; manual repair.
Persist the facts that survive a coordinator crash
An order spans stock, payment and shipment. A local transaction cannot roll back a provider call. Store an operation identity and progress independently of the coordinator process:
| Step | Durable fact | Uncertain result | Repair |
|---|---|---|---|
| Stock | Hold H1 owns two units | Hold request response lost | Resolve H1 |
| Payment | Operation P1 targets H1 | Provider timeout | Query/reconcile P1 |
| Shipment | Shipment S1 created | Worker acknowledgement lost | Return/reconcile S1 |
Read the diagram
Step / Durable fact / Uncertain result / Repair; Stock / Hold H1 owns two units / Hold request response lost / Resolve H1; Payment / Operation P1 targets H1 / Provider timeout / Query/reconcile P1; Shipment / Shipment S1 created / Worker acknowledgement lost / Return/reconcile S1
Orchestration stores the next decision in one workflow owner. Choreography lets services react to events; ownership of terminal state and correlation still needs to be clear. A durable execution engine records history and timers so a new worker can reconstruct progress. It does not automatically make an activity's external effect idempotent.
Compensation is a new business action, such as refunding P1 and releasing H1. It can fail or have a different customer effect from “the operation never happened.” If P1 is unknown, releasing stock and charging again under P2 can create a paid order without stock or duplicate payment. Resolve first or use a declared uncertainty policy and track repair durably. Correlate callbacks to their operation and ignore replayed facts without dropping legitimate state transitions.
Version long-lived workflows so old history remains interpretable. Keep large payloads in versioned external storage and reference them; cap history or continue into a new bounded run with explicit transfer state. Test an upgrade while a payment timer is waiting, not only new workflows.
Show answer and explanation
Answer: A coordinator crash after payment acceptance resumes from persisted facts. If acceptance was not recorded, query the same P1; do not assume replay permits a new charge.
Step-by-step reasoning
1. Persist the workflow state
Give the workflow an identity, current state, step attempts, and deadline. Store enough information to resume after process loss. A queue message can wake a worker, but the queue alone should not be the only record of the business process. Make transition predicates explicit so duplicate wake-ups do not repeat completed steps blindly.
2. Protect every activity boundary
Reserve inventory using a stable reservation identity, authorize payment using a stable payment identity, and confirm only under valid state. Each activity needs idempotency or reconciliation according to its destination. The workflow engine cannot retroactively make an arbitrary external API exactly once.
3. Use compensation deliberately
If payment authorization fails, release inventory. If a charge completed but fulfillment cannot proceed, a refund may be required. These are new actions with their own failure modes and audit records, not time travel. Some steps are irreversible or only partially compensable; route them to manual repair when needed.
4. Bound time and expose stuck work
Set step deadlines, retry budgets, and escalation policies. Backoff transient failures, quarantine permanent validation problems, and monitor age in each state. A workflow that retries forever without visibility is not reliable from the customer’s perspective. Cancellation must define which already-completed effects remain.
Contracts and state
The following sketch makes the decision boundary concrete. Field names and capacity assumptions are illustrative; adapt them to the stated product contract.
Order workflow
pending -> inventory_held -> payment_authorized -> confirmed
payment failure -> releasing_inventory -> cancelled
unknown payment -> reconciling -> authorized or failed
Every transition: expected_state + durable attempt identityWorked example
A worker authorizes payment and crashes before saving the result. On restart it sees the authorization step unfinished. It must query or retry under the same provider identity, not create a new authorization. After resolving the outcome, it records progress and proceeds. This is the central difference between retrying computation and retrying a business effect.
Failure walkthrough
Compensation can fail too. Releasing inventory may time out, or refunding may be temporarily unavailable. Store compensation state and retry it with a stable identity. Show the customer an honest pending resolution status. Reconciliation compares the internal workflow with provider records to find outcomes callbacks never delivered.
Read the diagram
Payment authorization sent → Provider accepts → Worker crashes before recording → Resume same workflow step → Resolve using original identity
Decisions and trade-offs
| Approach | Strength | Cost |
|---|---|---|
| Orchestration | Visible central process state | Coordinator and workflow evolution |
| Event choreography | Looser service coupling | Harder end-to-end diagnosis |
| Saga compensation | Business recovery across owners | Not equivalent to atomic rollback |
Check your understanding
Why is a refund not a rollback of the original charge?
Show answer and explanation
Answer: The charge already existed outside the local transaction and may have observable consequences. A refund is another provider operation that can be delayed, fail, or require reconciliation. Persist and audit both actions.
Transfer to a new scenario
Reserve inventory, authorize a payment, then confirm; recover after each transition.
Why is refunding money a new business action rather than rolling back a completed external call?
Primary documentation
Read the first-party engineering account or official technical reference. Company engineering posts describe the scope and date of that publication; the interview reconstruction and scenarios here are original teaching examples.
Persist progress across independent systems
A database transaction cannot atomically include an unrelated payment provider and shipping service. Model the business process as durable states with transitions, stable operation IDs and compensations. A compensation is a new business action, such as refunding a charge, and can itself fail or need manual handling.
The transactional outbox solves one specific gap: update local business state and record the intent to publish in the same transaction. A relay publishes later and may publish more than once. Consumers still need deduplication or idempotent transitions. The outbox does not make every downstream side effect exactly once.
Choose orchestration when a central workflow makes dependencies, deadlines and recovery easier to understand. Choreography can decouple producers but makes a long business process harder to trace. In either case, propagate a correlation ID and expose unresolved compensation and uncertain effects to operators.
Read the diagram
Approach / Strength / Cost; Orchestration / Visible central process state / Coordinator and workflow evolution; Event choreography / Looser service coupling / Harder end-to-end diagnosis; Saga compensation / Business recovery across owners / Not equivalent to atomic rollback
Operational sketch
created -> inventory_reserved -> payment_authorized -> confirmed
payment_failed -> release_inventory -> cancelled
unknown_payment -> reconcile -> confirmed or compensate
outbox row commits with each local transitionA tempting mistake
Do not compensate an operation merely because its response timed out. Determine whether it happened, and make compensation safe to repeat. A retry policy without a terminal escalation path can leave workflows stuck forever.
Transfer exercise
What if inventory release succeeds but its acknowledgement is lost?
Show answer and explanation
Answer: Retry the same compensation identity. The release operation checks the reservation state and must not increase available stock twice.
Why is a refund not a rollback of the original charge?
Your design draft
Clarify assumptions, explain your approach, and test the difficult cases. Save your draft, then compare it with the study notes.
Self-review checklist
Self-guided practice. Automated AI feedback and code execution are not connected.