AWS Application Integration: SNS Topics & Subscriptions
In the AWS ecosystem, Amazon Simple Notification Service (SNS) is a highly available, durable, secure, fully managed pub/sub messaging service. It allows you to decouple microservices, distributed systems, and serverless applications by enabling a “Fan-out” architecture.
The Real-World Analogy
Think of SNS like a Podcast Feed. A creator (Publisher) uploads an episode to a specific show (Topic). Multiple listeners (Subscribers) receive that episode automatically via different apps or notifications (Protocols like SQS, Email, or Lambda). The creator doesn’t need to know who the listeners are; they just publish to the feed.
Core Concepts & Configuration
SNS operates on a Pub/Sub (Publisher/Subscriber) model. Publishers communicate asynchronously with subscribers by sending messages to a topic, which is a logical access point and communication channel.
- Topics: The central hub where messages are sent. Names must be unique within an AWS account/region.
- Subscriptions: The “endpoints” that want to receive messages. A single topic can have millions of subscribers.
- Protocols: SNS supports SQS, Lambda, HTTP/S, Email, SMS, and Mobile Push (APNs, GCM).
- Message Filtering: By default, a subscriber receives every message. Filtering allows subscribers to specify a policy (JSON) so they only receive messages they care about (e.g., only “OrderPlaced” events).
SNS Standard vs. SNS FIFO
| Feature | SNS Standard | SNS FIFO |
|---|---|---|
| Throughput | Nearly unlimited | Up to 300 msg/sec (or higher with batching) |
| Ordering | Best-effort | Strict Ordering (First-In-First-Out) |
| Delivery | At-least-once | Exactly-once |
| Subscribers | All protocols supported | Only SQS FIFO queues |
Security & Durability
SNS ensures message safety through several layers:
- Encryption at Rest: Integration with AWS KMS (SSE-KMS).
- Encryption in Transit: Messages are delivered via HTTPS.
- Access Control: IAM Policies for API access and SNS Access Policies (Resource-based) for cross-account permissions.
- Dead Letter Queues (DLQ): If SNS cannot deliver a message to a subscriber (after retries), it can move the message to an SQS DLQ for analysis.
Decision Matrix: If–Then Guide
- If you need to send one message to multiple SQS queues… Then use the SNS Fan-out pattern.
- If you need strict message ordering across multiple consumers… Then use SNS FIFO + SQS FIFO.
- If you need to trigger a function whenever a file is uploaded to S3… Then use S3 Event Notifications to an SNS Topic.
- If you want to reduce costs by not processing irrelevant messages… Then implement SNS Subscription Filter Policies.
Exam Tips and Gotchas
- Fan-out Pattern: This is the #1 SNS scenario. Sending a message to SNS, which then pushes to multiple SQS queues for parallel processing.
- SNS vs SQS: SNS is “Push” (server to client). SQS is “Pull” (client polls the server).
- Cross-Account: To allow another AWS account to publish to your topic, you must update the SNS Topic Policy.
- TTL: SNS messages are not stored long-term. If a message isn’t delivered within the retry window, it’s lost unless a DLQ is configured.
- Golden Nugget: SNS FIFO can only have SQS FIFO as a subscriber. You cannot subscribe an Email or Lambda to an SNS FIFO topic.
Topics covered:
Summary of key subtopics covered in this guide:
- Pub/Sub architectural patterns and decoupling.
- SNS Standard vs FIFO performance and limitations.
- Supported protocols (SQS, Lambda, HTTP, SMS, Email).
- Message Filtering and Fan-out strategies.
- Security via KMS and Resource-based policies.
- Resiliency with Dead Letter Queues (DLQs).
SNS Architectural Infographic
Integrations
IAM: Control who can Publish/Subscribe.
KMS: Server-side encryption for sensitive data.
CloudWatch: Monitor NumberOfMessagesPublished and delivery failures.
Scaling & Limits
Standard: Unlimited throughput, millions of subscribers per topic.
Filtering: Offload logic from subscribers to SNS to save compute costs.
Optimization
SNS is pay-as-you-go. Charges are based on the number of notifications delivered and the number of API requests. Note: Data transfer costs apply for cross-region delivery.
Production Use Case: E-commerce Order Processing
When a customer places an order, the “Orders Service” publishes a message to an SNS Topic. This message is fanned out to:
- An SQS Queue for the Shipping Service.
- An SQS Queue for the Analytics Service.
- A Lambda Function that sends a confirmation email to the customer.
Benefit: If the Analytics service is down, the Shipping and Email services still process the order immediately.