Amazon SNS (Simple Notification Service)
Core concept: SNS is a pub/sub service โ one message published to a Topic is pushed to all subscribers simultaneously (fan-out).
SNS vs SQSโ
| Feature | SNS | SQS |
|---|---|---|
| Model | Push (pub/sub) | Pull (queue) |
| Persistence | โ No (messages not stored) | โ Yes (up to 14 days) |
| Consumers | Many at once (all subscribers) | One consumer per message |
| Use case | Fan-out, alerts, notifications | Decoupling, buffering |
Subscription Typesโ
| Protocol | Use Case |
|---|---|
| SQS | Fan-out to a queue for async processing |
| Lambda | Direct serverless processing |
| HTTP/HTTPS | Webhook delivery |
| Email / Email-JSON | Human notifications |
| SMS | Mobile text messages |
| Mobile Push | APNs (iOS), FCM (Android), ADM |
Fan-Out Pattern (SNS + SQS)โ
[S3 Event / App]
โ
โผ
[SNS Topic]
/ โ \
โผ โผ โผ
[SQS] [SQS] [Lambda]
Queue Queue
(ETL) (Audit) (Alert)
Why not publish to multiple SQS directly? โ SNS fan-out is atomic: one publish โ all subscribers in parallel. โ Adding new consumers doesn't change the producer.
Message Filteringโ
Each SQS subscription can define a filter policy so it only receives relevant messages:
// Subscription filter โ only receive orders from "electronics" category
{
"category": ["electronics"],
"price": [{ "numeric": [">=", 100] }]
}
Messages are published with message attributes, and SNS only delivers to matching subscriptions.
SNS FIFOโ
- Ordered messages to SQS FIFO subscribers only
- Supports deduplication (same as SQS FIFO)
- Topic name must end in
.fifo - Lower throughput than standard SNS
๐งช Practice Questionsโ
Q1. An e-commerce app publishes an OrderPlaced event. Three services need to process it: inventory, billing, and shipping. What is the BEST architecture?
A) One SQS queue per service, application publishes to all 3
B) SNS Topic โ 3 SQS subscriptions (fan-out)
C) Kinesis Data Stream
D) Direct Lambda invocation from the order service
โ Answer & Explanation
B โ The SNS fan-out pattern (SNS โ multiple SQS queues) is the standard answer for "multiple consumers need the same event." Each service gets its own queue, decoupling processing speed and failure domains.
Q2. A notification system needs to send alerts via email to admins and via SMS to on-call engineers from the same alert. What SNS feature enables this?
A) Message Filtering
B) Multiple Subscriptions on one Topic
C) SNS FIFO
D) Dead Letter Queue
โ Answer & Explanation
B โ SNS Topics support multiple subscriptions with different protocols. One email subscription + one SMS subscription receives every published message.