AWS & Modern Cloud Architecture · Chapter 9 · ~20 min · Intermediate
Decoupled Architecture: SQS, SNS & EventBridge
Design resilient event-driven systems: master asynchronous message queues with Amazon SQS, broadcast 1-to-many events using SNS fan-out patterns, route enterprise events with Amazon EventBridge, and isolate poison-pill messages using Dead Letter Queues.
- Differentiate Point-to-Point message queuing (SQS) from Pub/Sub broadcasting (SNS)
- Implement Fan-Out architectural patterns to broadcast events across heterogeneous consumer systems
- Route structured enterprise events using Amazon EventBridge content-based pattern matching
- Design resilient Dead Letter Queue (DLQ) redrive mechanisms and poison-pill message handling
Decoupled Architecture: SQS, SNS & EventBridge: learn it visually
Build event-driven cloud systems: asynchronous pub/sub fan-out with SNS, SQS point-to-point worker queues, Dead-Letter Queues, and EventBridge schema-based content filtering.
1Amazon SQS: Distributed Message Queuing
In tightly coupled architectures, when an order service calls downstream billing or inventory services over synchronous HTTP, any momentary network blip or downstream downtime immediately bubbles up as a checkout failure to the end user.
Amazon Simple Queue Service (SQS) was AWS's first service launched in 2004. It provides a fully managed, infinitely scalable message queue that buffers messages between software components:
Nearly Unlimited Throughput
Guarantees that a message is delivered at least once. Throughput is virtually unlimited. Best-effort message ordering (occasional out-of-order delivery can occur).
Strict First-In-First-Out
Guarantees exact chronological ordering and exactly-once processing using Message Deduplication IDs. Supports up to 3,000 messages/sec with high throughput batching. Queue name must end with .fifo.
The SQS Visibility Timeout Lifecycle:
When a worker consumer pulls a message from SQS via ReceiveMessage, SQS does not delete it. Instead, it starts a Visibility Timeout (default 30 seconds). During this window, the message is hidden from all other workers:
- Worker Succeeds: Worker explicitly calls
DeleteMessage. The message is permanently purged. - Worker Crashes: The visibility timeout expires. The message becomes visible again in the queue for another worker to process!
A worker EC2 instance pulls an order message from an SQS queue. Halfway through processing, the server abruptly loses power. What happens to the message?
2Architectural Mental Models: SQS vs. SNS vs. EventBridge
Choosing the wrong messaging primitive creates brittle microservices and unmaintainable polling loops. Master these three structural paradigms:
Wrong idea: Microservices should invoke downstream APIs directly via synchronous REST
Assuming that Order Service calling Payment Service calling Inventory Service calling Email Service via synchronous HTTP is clean and simple.
Real system: Decouple microservices asynchronously with SQS queues to absorb spikes
Order Service commits the order and pushes a message to SQS in 10ms. Downstream worker pools consume messages at their own sustainable processing rate.
3Amazon SNS: 1-to-Many Pub/Sub & Fan-Out
While SQS is a point-to-point pull model (one message consumed by one worker),Amazon Simple Notification Service (SNS) is a push-based publisher/subscriber (pub/sub) service. A single event published to an SNS Topic is instantaneously pushed to thousands of endpoints: HTTP webhooks, AWS Lambda functions, email, mobile push notifications, and Amazon SQS queues.
The SNS-to-SQS Fan-Out Pattern:
In modern microservices, combining SNS and SQS is the premier architectural pattern. The producer publishes once to an SNS Topic. SNS fans out copies into multiple dedicated SQS queues. Each downstream service (billing, fulfillment, analytics) consumes from its own queue at its own pace:
// SNS Subscription Filter Policy Example
// The Analytics queue only receives orders from the USA or UK with amount >= 100
{
"country": ["USA", "UK"],
"amount": [{ "numeric": [">=", 100] }]
}Why should an application publish an 'OrderCreated' event to an SNS topic subscribed to multiple SQS queues, rather than writing directly to multiple SQS queues from the web app?
4Amazon EventBridge: Enterprise Event Bus
Amazon EventBridge (formerly CloudWatch Events) is a serverless event bus that connects applications using data from your own applications, integrated SaaS applications (like Datadog, PagerDuty, Zendesk), and native AWS services.
Declarative Rules
EventBridge evaluates JSON patterns directly on the event body without requiring custom routing code.
Type Safety & Code Gen
Automatically infers schemas from event payloads and generates typed code bindings for Java, TypeScript, or Python.
Point-in-Time Replay
Archive all events on a bus and replay past events to test bug fixes or hydrate new microservices.
// EventBridge Rule: Match payments failed in EU region
{
"source": ["ecommerce.payments"],
"detail-type": ["PaymentFailed"],
"detail": {
"region": ["eu-west-1", "eu-central-1"],
"errorCode": [{ "prefix": "CARD_" }]
}
}What is the primary architectural differentiator between Amazon EventBridge and Amazon SNS?
5Hands-On: Test Event Fan-Out & DLQ Redrive
Publish different event payloads through an SNS/EventBridge fan-out architecture. Observe how payments, shipping, and VIP rules react, and watch corrupted poison pills automatically quarantine into the Dead Letter Queue:
Event Fan-Out & Content-Based Routing Simulator
Consumes all orders to charge customer credit card.
Generates warehouse packing slips and courier labels.
Content-based filter routes high-dollar purchases to executive Slack.
6Bug Hunt: Fix the SQS Redrive Policy & EventBridge Rule
A backend engineer created this JSON configuration to attach a Dead Letter Queue to an order processing pipeline and route high-dollar purchases. However, it contains three schema and type errors:
maxReceiveCountis set to an invalid string instead of an integer.- EventBridge
sourceis a string instead of an array of strings. - EventBridge
detail-typeis a string instead of an array of strings.
Fix the JSON configuration and verify the result:
🎯 Find and fix the bugs in the code above, then press Run & verify to test your solution.
Need a hint?
- Change
"maxReceiveCount": "invalid_string"to an integer like3or5. - EventBridge event patterns require arrays: set
"source": ["ecommerce.orders"]. - Set
"detail-type": ["OrderPlaced"]as an array.
7Chapter Knowledge Challenge
Validate your decoupled messaging and asynchronous architecture expertise:
A banking application requires strict chronological ordering for debit and credit transactions for each user account. Which AWS messaging configuration must be selected?
Why is setting a Dead Letter Queue (DLQ) considered mandatory for resilient enterprise messaging architectures?
8Recap & Architecture Best Practices
- Amazon SQS: Pull-based message queuing with Visibility Timeouts to absorb traffic spikes and isolate microservice failures.
- Amazon SNS: Push-based pub/sub broadcasting engineered for 1-to-many fan-out to multiple worker SQS queues.
- Amazon EventBridge: Enterprise event bus featuring declarative JSON content filtering, SaaS integrations, and event replay.
- Dead Letter Queues: Safeguard queues against poison pills by isolating un-processable messages after a configurable retry threshold.
Ready for Chapter 10? Mark this chapter as complete to proceed to Infrastructure as Code: