📨 SNS + SQS Optimization
SNS (Simple Notification Service) and SQS (Simple Queue Service) are the backbone of event-driven architectures in AWS. Understanding their combined patterns — especially the fanout pattern and message filtering — is critical for optimization.
🔑 Covers: SNS Basics · SQS Basics · Fanout Pattern · Subscription Filter Policies · Event RoutingSNS — Simple Notification Service
📋 SNS Key Concepts
📌 Topic
A named endpoint that publishers send messages to. Think of it as a "channel". Example: "OrderEvents" topic receives all order-related events.
📡 Publisher
Anyone who sends a message to a topic. Could be: your Lambda function, API Gateway, S3 (on object upload), CloudWatch Alarm, or your application code.
👥 Subscriber
Anyone who wants to receive messages from a topic. Types: SQS queue, Lambda function, HTTP/HTTPS endpoint, email, SMS, mobile push. Each gets a COPY of every message.
SQS — Simple Queue Service
⚙️ Key SQS Configuration
| Setting | Purpose | Default |
|---|---|---|
| Visibility Timeout | How long a message is hidden from other consumers after one consumer picks it up. If the consumer doesn't delete it before timeout, the message reappears. | 30 seconds |
| Message Retention | How long messages stay in the queue if not processed. | 4 days (max 14 days) |
| Dead Letter Queue (DLQ) | Where messages go after failing processing N times. Lets you investigate failed messages. | Not configured |
| Standard Queue | At-least-once delivery, best-effort ordering, unlimited throughput. | Default type |
| FIFO Queue | Exactly-once processing, strict ordering, limited to 3000 msg/sec. | Optional |
The SNS + SQS Fanout Pattern — Core Architecture
The fanout pattern is when one SNS message is "fanned out" to multiple SQS queues. Each queue has its own consumer processing the message independently. This decouples the publisher from each consumer.
✅ Why Fanout is Better Than Direct Calls
❌ Without Fanout (Direct Calls)
- Order service calls inventory service directly
- Order service calls shipping service directly
- Order service calls analytics service directly
- If any downstream service is slow → order service is slow
- Adding a new subscriber = change order service code
- Tight coupling between all services
✅ With SNS + SQS Fanout
- Order service just publishes to SNS — one call
- SNS delivers to all queues asynchronously
- If inventory service is slow, order service is unaffected
- Adding new subscriber = just subscribe a new SQS queue to SNS
- Loose coupling — services are independent
SNS Subscription Filter Policies — Message Routing
By default, ALL subscribers to an SNS topic receive ALL messages. But what if you only want some subscribers to receive certain types of messages? Subscription filter policies solve this.
• HighValueOrders queue: Only wants orders where
orderValue > 1000• NewCustomerOrders queue: Only wants orders where
customerType = "new"• UrgentOrders queue: Only wants orders where
priority = "urgent"Without filters, all 3 queues would receive ALL orders and have to filter them in code. With filter policies, SNS does the filtering — each queue only gets what it cares about.