AWS · Solution pattern
Event-driven order processing
Accept an order quickly, then process payment, stock and shipping asynchronously, with retries and no lost orders.
Also on: Google Cloud
The business problem
An online shop must accept orders at unpredictable rates, including sale-day spikes, and then charge the card, reserve stock, arrange shipping and tell the customer. These steps are slow, depend on outside systems that sometimes fail, and must not lose or duplicate an order. If the web request had to wait for every step, one slow dependency would make the whole checkout slow or fail.
The answer is to separate accepting the order (fast, durable) from processing it (asynchronous, retried, observable), and to connect the two with events.
Requirements
- Accept an order and answer the customer in well under a second, even when downstream systems are slow.
- Never lose an order, and never charge or ship it twice if a request or step is retried.
- Absorb traffic spikes without capacity planning, and cost close to nothing when idle.
- Add new consumers of order events (analytics, fraud checks) without changing the intake code.
- Handle a failure halfway through, such as payment taken but stock unavailable, by undoing earlier steps.
- Show what happened to every order for support and audit.
The AWS design
API Gateway receives the order and calls an intake Lambda function. The function writes the
order to DynamoDB with a client-supplied idempotency key (a conditional write rejects a
duplicate) and publishes an OrderPlaced event to EventBridge. The customer gets their
answer at this point.
EventBridge rules deliver the event to an SQS queue (with a dead-letter queue) for fulfilment and to an SNS topic for notifications and other subscribers. A worker Lambda reads the queue and starts a Step Functions workflow that runs the order saga: take payment, reserve stock, create the shipment, and run compensating steps if one fails.
Components and their roles
| Component | Role |
|---|---|
| API Gateway | Public HTTPS entry point: authenticates callers, throttles abuse and forwards POST /orders to the intake function. |
| Intake Lambda | Validates the order, writes it to DynamoDB with an idempotency key, publishes the OrderPlaced event. |
| Orders table | Durable record of each order and its status; the conditional write on the order ID makes retries safe. |
| Event bus | Receives OrderPlaced and routes it by rule, so new consumers are added without touching intake. |
| Fulfilment queue | Buffers orders for processing and holds failed ones in a dead-letter queue for review. |
| Worker Lambda | Reads batches from the queue and starts one workflow execution per order. |
| Order workflow | Runs payment, stock and shipping as steps with retries, and undoes completed steps when a later one fails. |
| Notifications topic | Fans out order events to email or SMS to the customer and to analytics subscribers. |
How the pieces connect
- API Gateway → Intake Lambda: forwards the validated request
- Intake Lambda → Orders table: conditional write of the order with its idempotency key
- Intake Lambda → Event bus: publishes OrderPlaced
- Event bus → Fulfilment queue: a rule matching OrderPlaced delivers to the queue
- Event bus → Notifications topic: a second rule delivers to the notifications topic
- Fulfilment queue → Worker Lambda: batches of messages trigger the worker
- Worker Lambda → Order workflow: starts one execution per order
- Order workflow → Notifications topic: publishes success or failure
Security and identity
- Callers: API Gateway authenticates with Cognito or a JWT authoriser, and AWS WAF can block common attacks on the public API.
- Least privilege: each Lambda function and the state machine has its own IAM role with only the actions it needs (intake can write one table and put events; the worker can receive from one queue and start one workflow).
- Data: DynamoDB, SQS and SNS encrypt at rest; use a customer managed KMS key if you must control and audit key use. Payment card data should be handled by a payment provider rather than stored in your tables.
- Service to service: queue and topic policies allow only EventBridge and the intended publishers to send; keep secrets in Secrets Manager.
Availability and failure handling
Every component is a regional managed service spread across Availability Zones, so there are no servers to fail over. The design survives failures by retrying and by recording state:
- The order is written before anything else happens, so a failure afterwards cannot lose it.
- Idempotency keys make a retried request or a duplicate queue message harmless.
- The queue retries a message until it is deleted, and a dead-letter queue collects ones that keep failing.
- Step Functions retries steps with backoff and runs compensating steps (refund, release stock) if a later step fails.
Surviving the loss of a whole Region needs a second Region and a way to route to it; DynamoDB global tables replicate the orders table, and the rest is recreated from infrastructure as code.
Scaling
API Gateway, Lambda, EventBridge, SQS and DynamoDB (on-demand) scale with traffic, so a spike lengthens the queue rather than failing requests. The limits to watch are the API Gateway account throttle, Lambda concurrency (cap the worker so downstream systems are not overwhelmed), and the Step Functions start rate. Choose an order ID with many distinct values so DynamoDB spreads load across partitions.
Limits that shape the design
| Item | Value | Context | Source |
|---|---|---|---|
| API Gateway integration timeout (REST API) | 29 seconds by default Can be raised with a quota request |
Why the intake function must answer quickly and leave slow work to the queue and workflow. | Source checked |
| Lambda maximum run time | 900 seconds Fixed limit |
Source checked |
|
| SQS maximum message size | 1,048,576 bytes (1 MiB) | Source checked |
|
| Step Functions maximum execution duration, Standard | 1 year Fixed limit |
Long enough for slow payment or shipping confirmations. | Source checked |
Cost drivers
There is no charge for idle capacity. Costs scale with the number of orders and are driven by: API Gateway requests; Lambda requests and duration; DynamoDB request units and storage; events published to and delivered from EventBridge; SQS and SNS requests and deliveries (in 64 KB chunks); and Step Functions state transitions on Standard workflows. At very high steady volume, compare Express workflows, an HTTP API instead of a REST API, and always-on containers.
Trade-offs
- Eventual consistency: the customer's confirmation comes before payment completes, so the user interface has to show a "processing" status and the system needs a way to tell them the result.
- More moving parts than one service: you trade a simple synchronous call for several managed services, and must monitor queues, dead-letter queues and workflow failures.
- At-least-once delivery: every consumer must be idempotent.
- Cost at scale: per-request pricing is excellent for variable load but can exceed the cost of containers at steady high volume.
- Simpler alternative: a small shop can write the order and call the payment provider in one request, and add events when the need appears.
Related
Courses and lessons
- Amazon SQS: queues, FIFO, visibility timeout and dead-letter queues · AWS Solutions Architect – Associate (SAA-C03) · Members
- Amazon SNS and the fan-out pattern · AWS Solutions Architect – Associate (SAA-C03) · Members
- Amazon EventBridge: event buses, rules, Scheduler and Pipes · AWS Solutions Architect – Associate (SAA-C03) · Members
- AWS Step Functions: orchestrating workflows · AWS Solutions Architect – Associate (SAA-C03) · Members
- AWS Lambda · AWS Solutions Architect – Associate (SAA-C03) · Members
Services, comparisons and patterns
- Amazon SQS · Service
- Amazon SNS · Service
- Amazon EventBridge · Service
- AWS Step Functions · Service
- AWS Lambda · Service
- Amazon API Gateway · Service
- Amazon DynamoDB · Service
- SQS vs SNS vs EventBridge · Comparison