Orchestrating Business Processes with Step Functions
This lesson explains how AWS Step Functions orchestrates processes that span several services as state machines with built-in error handling and a visible execution history.
Learning outcomes
After completing this lesson, you will be able to:
- Choose between Standard and Express workflows.
- Describe Retry and Catch error handling.
- Implement the saga pattern with compensating actions.
- Choose between orchestration and choreography.
Workflow types
Step Functions has two workflow types. Standard workflows run for up to a year, and each step runs exactly once, with the full execution history recorded. They are charged per state transition, which suits long-running, auditable business processes, including waits for human approval. Express workflows run for at most five minutes. They run at least once when started asynchronously, or at most once when started synchronously, so their steps must tolerate repetition. They are charged per execution, duration and memory, which makes them far cheaper for high-volume, short event processing, such as transforming every incoming record. The following table summarises the differences.
Building blocks
Error handling is declared on each state rather than coded. Retry rules handle transient errors with backoff, and Catch rules route permanent errors to recovery or compensation states. Parallel states run branches concurrently, Map states process the items of a list, and Distributed Map processes millions of S3 objects or records with high concurrency, which suits large batch jobs.
Callbacks with task tokens pause a workflow until an external system or a person responds, for example for a manager's approval, which may take days in a Standard workflow. More than 200 direct service integrations call services such as DynamoDB, SQS, ECS, Glue and Bedrock without Lambda functions in between, which removes code whose only purpose is to call another AWS API.
The saga pattern
Distributed transactions across services cannot use one database transaction. In the saga pattern, each step has a compensating action that undoes it, and a Catch on each step runs the compensations for the steps already completed. For example, if booking the flight fails after the hotel was reserved, the workflow cancels the hotel. The state machine makes the sequence explicit and records what happened.
Orchestration or choreography
In choreography, services react to each other's events with no central coordinator, which gives loose coupling but makes a long process hard to follow. In orchestration, a central workflow calls each service and holds the process state, which gives visibility, error handling and compensation in one place. Many systems use both: events between bounded contexts, orchestration within a process.
A booking saga with compensation
A travel platform books a flight, a hotel and a car through three independent services. If the car booking fails, the customer must not be left with a flight and hotel they did not want. Distributed transactions are not available across services, so the workflow defines a compensating step for each action and runs them in reverse on failure:
"BookFlight": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke",
"Parameters": { "FunctionName": "book-flight", "Payload.$": "$" }, "ResultPath": "$.flight",
"Catch": [{ "ErrorEquals": ["States.ALL"], "ResultPath": "$.error", "Next": "BookingFailed" }],
"Next": "BookHotel" },
"BookHotel": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke",
"Parameters": { "FunctionName": "book-hotel", "Payload.$": "$" }, "ResultPath": "$.hotel",
"Catch": [{ "ErrorEquals": ["States.ALL"], "ResultPath": "$.error", "Next": "CancelFlight" }],
"Next": "BookCar" },
"BookCar": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke",
"Parameters": { "FunctionName": "book-car", "Payload.$": "$" }, "ResultPath": "$.car",
"Catch": [{ "ErrorEquals": ["States.ALL"], "ResultPath": "$.error", "Next": "CancelHotel" }],
"Next": "Confirmed" },
"CancelHotel": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke",
"Parameters": { "FunctionName": "cancel-hotel", "Payload.$": "$" }, "Next": "CancelFlight" },
"CancelFlight": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke",
"Parameters": { "FunctionName": "cancel-flight", "Payload.$": "$" }, "Next": "BookingFailed" },
"BookingFailed": { "Type": "Fail", "Error": "BookingFailed" },
"Confirmed": { "Type": "Succeed" }Every booking and cancellation function must be idempotent, because Step Functions may retry them. The workflow gives operators a visual record of every booking's path, which is the main advantage of orchestration over pure event choreography for business-critical processes.
When should an Express workflow be chosen over a Standard workflow?
Show answer
For high-volume, short-lived processing that finishes within five minutes, where cost per execution matters. Standard workflows suit long-running processes that need exactly-once steps and a full history.
Exam Tips
Remember for the exam
- Compensating transactions across services call for the Step Functions saga pattern.
- Long-running, exactly-once business processes use Standard workflows; high-volume short ones use Express.
- For human approval inside a workflow, use a callback with a task token.
- To process millions of S3 objects in parallel, use Distributed Map.
- Direct service integrations remove Lambda functions that only call another AWS API.