Skip to main content

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โ€‹

FeatureSNS (Pub/Sub)SQS (Queue)
ModelPush to all subscribersPull by one consumer
PersistenceโŒ No retentionโœ… Up to 14 days
ConsumersAll subscribers get every messageOne consumer per message
DeliveryFire-and-forgetGuaranteed (with retries)
Use caseFan-out, alerts, notificationsDecoupling, 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?

  1. Atomic delivery โ€” one publish, all subscribers receive
  2. Consumer independence โ€” each queue processes at its own pace
  3. Failure isolation โ€” if audit service fails, ETL is unaffected
  4. Easy extension โ€” add new consumers without changing producer

Subscription Protocolsโ€‹

ProtocolUse CaseRetry
SQSAsync processing (fan-out)SQS retains message
LambdaServerless processingSNS retry policy
HTTP/HTTPSWebhook delivery3 retries, then DLQ
EmailHuman notificationsNo retry
SMSMobile alertsNo retry
Kinesis Data FirehoseStream to S3/RedshiftManaged
Mobile PushAPNs, FCM, ADMPlatform-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โ€‹

ScopeDescription
MessageAttributes (default)Filter on message attributes
MessageBodyFilter on JSON body content (newer feature)

SNS FIFOโ€‹

FeatureStandard SNSFIFO SNS
OrderingBest-effortStrict FIFO
DeduplicationโŒโœ… (5-min window)
ThroughputNearly unlimited300 TPS (3,000 with batching)
SubscribersAll protocolsSQS FIFO only
NamingAnyMust 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โ€‹

SNS Exam Cheat Sheet
  1. Fan-out = SNS โ†’ multiple SQS queues (classic exam pattern)
  2. Message filtering = subscribers only receive relevant messages (saves processing)
  3. SNS FIFO = only SQS FIFO subscribers supported
  4. No message persistence โ€” if subscriber is down, message is lost (use SQS for persistence)
  5. SNS + SQS = reliable fan-out (SQS retains messages even if consumer is slow)
  6. Up to 12.5M subscriptions per topic
  7. Message size: 256 KB max
  8. 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.


๐Ÿ”— Resourcesโ€‹