Ace Cloud Interviews
📨

AWS Messaging & Integration

SNS

Pub/sub messaging service for fan-out notifications and application decoupling

Amazon Simple Notification Service (SNS) is a fully managed pub/sub messaging service that enables fan-out messaging to large numbers of subscribers including SQS queues, Lambda functions, HTTP endpoints, and email. It decouples producers from consumers, allowing a single message publish to trigger multiple downstream systems simultaneously. SNS is a foundational building block for event-driven architectures on AWS.

How SNS Works: Topics, Publishers, and Subscribers

SNS operates on a topic-based publish/subscribe model. Publishers send messages to a topic, and SNS delivers that message to all confirmed subscribers of that topic. Delivery happens in near real-time and in parallel to all subscribers.

ConceptDescriptionExample
TopicLogical access point and communication channelarn:aws:sns:us-east-1:123:order-placed
PublisherAny AWS service or application that sends a messageEC2 app, Lambda, CloudWatch alarm
SubscriberEndpoint that receives messages from the topicSQS queue, Lambda, HTTP/S endpoint, email, SMS
MessageUp to 256 KB of text dataJSON payload with order details
Subscription filterPolicy to selectively receive messages based on attributesOnly receive messages where order_type=premium
💡

SNS is a push model - it delivers messages to subscribers immediately. Contrast this with SQS where consumers poll for messages. Use SNS when you need immediate fan-out delivery to multiple endpoints.

Standard vs FIFO Topics

SNS offers two topic types with different delivery guarantees and throughput characteristics.

FeatureStandard TopicFIFO Topic
Message orderBest-effort orderingStrict first-in, first-out per message group
DeliveryAt-least-once (duplicates possible)Exactly-once processing
Throughput300 million messages/second300 messages/second per topic (can request increase)
Subscriber typesSQS, Lambda, HTTP/S, email, SMS, mobile pushSQS FIFO queues only
Message deduplicationNot supportedSupported via deduplication ID
Use caseHigh-volume fan-out, notificationsFinancial transactions, order processing
⚠️

FIFO topics can only deliver to FIFO SQS queues. If you need email or Lambda subscribers with ordering guarantees, you need a different architecture - typically a FIFO queue with a Lambda consumer.

Fan-Out Pattern and Message Filtering

The SNS fan-out pattern is one of the most common architectures in AWS. A single SNS topic fans out to multiple SQS queues, each owned by a different service. This decouples the publisher from consumers and allows each consumer to process at its own pace.

Message filtering reduces the number of messages each subscriber processes. Instead of filtering in consumer code, you attach a filter policy to the subscription.

bash
# Publish a message with attributes
aws sns publish \
  --topic-arn arn:aws:sns:us-east-1:123:orders \
  --message '{"orderId": "abc123", "amount": 99.99}' \
  --message-attributes '{
    "orderType": {"DataType":"String","StringValue":"premium"},
    "region": {"DataType":"String","StringValue":"us-east"}
  }'

# Create a subscription with filter policy
aws sns subscribe \
  --topic-arn arn:aws:sns:us-east-1:123:orders \
  --protocol sqs \
  --notification-endpoint arn:aws:sqs:us-east-1:123:premium-orders \
  --attributes '{"FilterPolicy":"{\"orderType\":[\"premium\"]}"}'
💡

Filter policies can match on string values, string prefixes, numeric ranges, and existence of attributes. A single filter policy can have up to 5 attribute conditions. This is far more efficient than routing in Lambda code.

Delivery Policies, Retries, and Dead-Letter Queues

SNS has built-in retry logic for HTTP/S endpoints. For Lambda and SQS subscribers, AWS manages retries at the destination service level. Understanding retry behavior is critical for building reliable systems.

Subscriber TypeRetry BehaviorDLQ Support
HTTP/HTTPSImmediate, then 1s, 20s, 20s... up to 23 days totalNo native DLQ - use SQS in front
SQSSQS handles retries via visibility timeout and redrive policyYes - SQS DLQ
LambdaLambda retries twice on async invocation errorsYes - SNS subscription DLQ or Lambda destination
EmailNo retries - one delivery attemptNo
SMSCarrier-level retries onlyNo
⚠️

For critical workflows, never publish directly from SNS to Lambda without a DLQ configured on the Lambda function or SNS subscription. A Lambda cold-start error or throttle will drop the message permanently without a DLQ.

bash
# Set a DLQ on an SNS subscription (redrive policy)
aws sns set-subscription-attributes \
  --subscription-arn arn:aws:sns:us-east-1:123:orders:abc-sub-id \
  --attribute-name RedrivePolicy \
  --attribute-value '{"deadLetterTargetArn":"arn:aws:sqs:us-east-1:123:sns-dlq"}'

SNS Pricing and Cost Optimization

Delivery TypePricingNotes
HTTP/HTTPS$0.60 per million deliveriesFirst 100k free per month
SQS deliveriesFreeNo charge for SNS-to-SQS
Lambda deliveriesFreePay only for Lambda execution
Email/Email-JSON$2.00 per 100k notificationsNot for transactional - use SES
SMS (US)$0.00645 per messageVaries heavily by country
Mobile push$0.50 per million notificationsAPNS, GCM, ADM
💡

SNS-to-SQS delivery is free, which is why the fan-out pattern (SNS to multiple SQS queues) is so cost-effective. The SNS cost is essentially zero - you only pay for what processes the messages downstream.

🎯

Interview Focus Points

  • 1Explain the SNS fan-out pattern and why you would use it instead of publishing directly to multiple consumers.
  • 2What is the difference between SNS Standard and FIFO topics, and when would you choose FIFO?
  • 3How does SNS message filtering work, and what is the performance benefit of filtering at the subscription level vs consumer code?
  • 4How would you ensure no messages are lost when SNS delivers to a Lambda function that is throttled or errors?
  • 5What happens to an SNS message if the HTTP endpoint is down? Walk me through the retry behavior.
  • 6Can you subscribe an SQS FIFO queue to an SNS Standard topic? Why or why not?
  • 7How would you implement a pattern where only certain consumers receive certain messages from a single SNS topic?
  • 8What is the difference between SNS and EventBridge for event routing?