Lesson 6 of 9 · 23m
Orchestration, Choreography, and Sagas
How multi-step serverless workflows stay consistent: Step Functions versus event choreography, compensating actions, and recovery design.
Real business operations rarely fit in one invocation. Charging a customer, reserving inventory, creating a shipment, and sending email involve separate systems and separate failures. There is no distributed transaction across Lambda, Stripe, DynamoDB, and your warehouse API. What you can build is a saga: local transactions plus compensating actions.
Choreography versus orchestration
Choreography means each service reacts to events: OrderCreated triggers inventory, InventoryReserved triggers payment, and so on. It is loosely coupled, but simple operational questions become hard: where is order 123 right now, what failed, and what happens next?
Orchestration puts a coordinator in charge. AWS Step Functions is the canonical serverless orchestrator: explicit state, retry history, branching, waits, and inspectable execution records. The cost is a central workflow definition and another service to understand.
A saga is recovery design
reserve inventory -> charge payment -> create shipment
|
failure after charge?
refund/void payment
release inventoryEvery forward step with externally visible effects needs a compensating step. Compensation is not rollback. You cannot un-send an email; you send a correction. You cannot undo a shipment; you create a return.
Step Functions makes the path explicit
{
"StartAt": "ReserveInventory",
"States": {
"ReserveInventory": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "inventory-reserve",
"Payload": { "orderId.$": "$.orderId" }
},
"Catch": [{ "ErrorEquals": ["States.ALL"], "Next": "OrderFailed" }],
"Next": "ChargePayment"
},
"ChargePayment": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "payments-charge",
"Payload": { "orderId.$": "$.orderId" }
},
"Catch": [{ "ErrorEquals": ["States.ALL"], "Next": "ReleaseInventory" }],
"Next": "CreateShipment"
},
"ReleaseInventory": { "Type": "Pass", "Next": "OrderFailed" },
"CreateShipment": { "Type": "Succeed" },
"OrderFailed": { "Type": "Fail" }
}
}Verbose, yes — but real recovery logic is verbose. In exchange, every input, output, retry, and failure path is inspectable.
Do not orchestrate everything
A single worker consuming one queue and writing one database row does not need a state machine. Use orchestration when the workflow has branching, human approval, waits, parallel fanout, or multi-service recovery. Step Functions can wait without paying for a running Lambda:
{ "Type": "Wait", "Seconds": 3600, "Next": "CheckShipmentStatus" }That replaces many "scan the database every minute" cron jobs.
Field notes
- Compensating actions need idempotency too; they will also retry.
- Keep workflow payloads small. Pass identifiers, not megabytes of mutable order data.
- Use execution names that correlate to business IDs so incidents are searchable.
- Distributed transactions do not appear because compute is managed. Design the saga or consciously accept inconsistency.
Was this lesson helpful?