Building a Resilient, Event-Driven Order Processing System on AWS
Learn how to decouple microservices and prevent cascading failures using Amazon EventBridge, AWS SQS, and AWS Lambda with practical architectural patterns.
Tightly coupled microservices often start out simple, only to turn into distributed monoliths at scale. When an order service calls inventory, billing, and notification APIs synchronously, a single latency spike in the billing provider can bring down the entire checkout flow.
Transitioning to an event-driven architecture (EDA) eliminates these bottlenecks. By decoupling event producers from consumers using Amazon EventBridge, Amazon SQS, and AWS Lambda, your system can absorb traffic spikes, isolate failures, and scale components independently.
The Architecture Pattern: Router + Queue + Compute
While you can wire Amazon EventBridge directly to AWS Lambda, pairing EventBridge with Amazon SQS provides an essential durability buffer for mission-critical workloads.
Ingestion (Producer): An API Gateway and Lambda function validate the incoming checkout request and publish an OrderPlaced event to a custom EventBridge bus.
Routing (Amazon EventBridge): EventBridge inspects the event payload and routes copies to multiple independent targets based on rules (e.g., Inventory Service, Billing Service, Analytics).
Buffering (Amazon SQS): Instead of invoking downstream compute directly, EventBridge sends the event to service-specific SQS queues. This protects downstream databases from sudden traffic surges.
Processing (AWS Lambda): Lambda polls the SQS queue in controlled batches, processes the business logic, and deletes the message upon success.
Step 1: Defining the Event Contract
A clean event-driven system relies on strict, predictable schemas. Here is a standard structure for an OrderPlaced event published to EventBridge:
JSON
{
"Source": "com.mycompany.orders",
"DetailType": "OrderPlaced",
"EventBusName": "commerce-event-bus",
"Detail": "{"orderId": "ORD-98231", "customerId": "CUST-441", "totalAmount": 149.99, "currency": "USD", "items": [{"sku": "KB-01", "qty": 1}]}"
}
Step 2: Designing the EventBridge Rule
To route only relevant events to the inventory queue, create an EventBridge rule that filters on both the source and detail-type:
JSON
{
"source": ["com.mycompany.orders"],
"detail-type": ["OrderPlaced"]
}
If a specific downstream consumer only cares about high-value orders (for example, a fraud-check service), you can filter directly on numeric payload values without writing a single line of Lambda code:
JSON
{
"source": ["com.mycompany.orders"],
"detail-type": ["OrderPlaced"],
"detail": {
"totalAmount": [{ "numeric": [">=", 500] }]
}
}
Step 3: Making Consumers Idempotent and Resilient
In distributed systems, networks fail and messages get delivered more than once. EventBridge and Standard SQS queues guarantee at-least-once delivery, meaning your downstream Lambda functions must be idempotent—processing the same event twice should produce the exact same result without duplicating side effects (like charging a customer's credit card twice).
Three production essentials for your consumer functions:
Enable ReportBatchItemFailures: When Lambda processes a batch of 10 messages from SQS and 1 fails, default behavior retries the entire batch. Enabling ReportBatchItemFailures tells SQS to only retry the specific failed message.
Track Idempotency Keys: Use a DynamoDB table with a conditional write (attribute_not_exists(orderId)) and a Time-to-Live (TTL) attribute to lock and track processed event IDs. Tools like Powertools for AWS Lambda handle this with a single decorator.
Configure Dead-Letter Queues (DLQs): Attach an SQS DLQ to your primary queue with a maxReceiveCount of 3 to 5. If a malformed payload causes repeated crashes, the message moves to the DLQ for inspection rather than blocking the queue indefinitely, and an Amazon CloudWatch Alarm notifies your team.
Key Takeaways
Synchronous for queries, asynchronous for commands: If the user needs an immediate answer on screen, use synchronous APIs. If the action triggers background workflows, emit an event.
Buffer heavy workloads: Put SQS between EventBridge and Lambda whenever downstream resources (like relational databases or third-party APIs) have strict concurrency limits.
Design for duplicate delivery: Always validate idempotency before executing state-changing operations.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article