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).
๐ฐ What Is SNS?โ
Think of SNS as a radio broadcast โ one speaker, many listeners. The publisher sends one message to a topic, and SNS delivers it to all subscribers in parallel. Compare with SQS where each message goes to exactly one consumer.
SNS vs SQSโ
| Feature | SNS (Pub/Sub) | SQS (Queue) |
|---|---|---|
| Model | Push to all subscribers | Pull by one consumer |
| Persistence | โ No retention | โ Up to 14 days |
| Consumers | All subscribers get every message | One consumer per message |
| Delivery | Fire-and-forget | Guaranteed (with retries) |
| Use case | Fan-out, alerts, notifications | Decoupling, buffering, rate control |
When to Use Both Together (Fan-Out)โ
S3 Event / Application Event
โ
โผ
[SNS Topic] โ single publish
/ โ \
โผ โผ โผ
[SQS] [SQS] [Lambda] โ multiple independent consumers
Queue Queue Function
(ETL) (Audit) (Alert)
Why fan-out with SNS + SQS?
- Atomic delivery โ one publish, all subscribers receive
- Consumer independence โ each queue processes at its own pace
- Failure isolation โ if audit service fails, ETL is unaffected
- Easy extension โ add new consumers without changing producer
Subscription Protocolsโ
| Protocol | Use Case | Retry |
|---|---|---|
| SQS | Async processing (fan-out) | SQS retains message |
| Lambda | Serverless processing | SNS retry policy |
| HTTP/HTTPS | Webhook delivery | 3 retries, then DLQ |
| Human notifications | No retry | |
| SMS | Mobile alerts | No retry |
| Kinesis Data Firehose | Stream to S3/Redshift | Managed |
| Mobile Push | APNs, FCM, ADM | Platform-specific |
Message Filteringโ
Without filtering, ALL subscribers receive ALL messages. Filter policies let subscribers receive only relevant messages:
// Publisher sends message with attributes:
{
"MessageBody": "{\"orderId\": \"ORD-123\", \"amount\": 150}",
"MessageAttributes": {
"category": { "DataType": "String", "StringValue": "electronics" },
"price": { "DataType": "Number", "StringValue": "150" },
"region": { "DataType": "String", "StringValue": "us-east-1" }
}
}
// Subscription 1 filter: Only electronics over $100
{
"category": ["electronics"],
"price": [{ "numeric": [">=", 100] }]
}
// Subscription 2 filter: Only US regions
{
"region": [{ "prefix": "us-" }]
}
// Subscription 3: No filter โ receives ALL messages
Filter Policy Scopeโ
| Scope | Description |
|---|---|
| MessageAttributes (default) | Filter on message attributes |
| MessageBody | Filter on JSON body content (newer feature) |
SNS FIFOโ
| Feature | Standard SNS | FIFO SNS |
|---|---|---|
| Ordering | Best-effort | Strict FIFO |
| Deduplication | โ | โ (5-min window) |
| Throughput | Nearly unlimited | 300 TPS (3,000 with batching) |
| Subscribers | All protocols | SQS FIFO only |
| Naming | Any | Must end in .fifo |
SNS + Lambda DLQโ
If Lambda subscription fails after retries:
SNSTopic:
Type: AWS::SNS::Subscription
Properties:
Protocol: lambda
Endpoint: !GetAtt MyFunction.Arn
TopicArn: !Ref MyTopic
RedrivePolicy:
deadLetterTargetArn: !GetAtt SNSFailureDLQ.Arn
Java SDK โ Publishingโ
SnsClient snsClient = SnsClient.create();
// Publish with message attributes (for filtering)
snsClient.publish(PublishRequest.builder()
.topicArn("arn:aws:sns:us-east-1:123:order-events")
.message("{\"orderId\": \"ORD-123\", \"amount\": 150}")
.messageAttributes(Map.of(
"category", MessageAttributeValue.builder()
.dataType("String").stringValue("electronics").build(),
"price", MessageAttributeValue.builder()
.dataType("Number").stringValue("150").build()))
.build());
// FIFO topic
snsClient.publish(PublishRequest.builder()
.topicArn("arn:aws:sns:us-east-1:123:order-events.fifo")
.message("{\"orderId\": \"ORD-456\"}")
.messageGroupId("customer-A")
.messageDeduplicationId("ORD-456-event")
.build());
๐ฏ DVA-C02 Exam Tipsโ
- Fan-out = SNS โ multiple SQS queues (classic exam pattern)
- Message filtering = subscribers only receive relevant messages (saves processing)
- SNS FIFO = only SQS FIFO subscribers supported
- No message persistence โ if subscriber is down, message is lost (use SQS for persistence)
- SNS + SQS = reliable fan-out (SQS retains messages even if consumer is slow)
- Up to 12.5M subscriptions per topic
- Message size: 256 KB max
- Cross-account: Use SNS access policy to allow other accounts to publish/subscribe
๐งช Practice Questionsโ
Q1. OrderPlaced event needs to go to inventory, billing, and shipping services. Best architecture?
A) App publishes to 3 SQS queues
B) SNS Topic โ 3 SQS subscriptions
C) Kinesis Data Stream
D) Direct Lambda calls
โ Answer & Explanation
B โ SNS fan-out: one publish, all subscribers receive in parallel. Adding new consumers doesn't change the producer.
Q2. Only the billing service needs "electronics" orders above $100. Other services need all orders. How?
A) Separate SNS topics per category
B) SNS message filtering on the billing subscription
C) Lambda pre-processor
D) SQS message groups
โ Answer & Explanation
B โ Filter policies on individual subscriptions. Billing gets {"category": ["electronics"], "price": [{"numeric": [">=", 100]}]}. Others have no filter.
Q3. SNS FIFO topic needs guaranteed ordering. Which subscriber types are supported?
A) Lambda, SQS, HTTP
B) SQS FIFO only
C) All subscriber types
D) Lambda and SQS FIFO
โ Answer & Explanation
B โ SNS FIFO only supports SQS FIFO queue subscribers.