Back to: AWS-Basics-Advanced
π Amazon SNS (Simple Notification Service) β Complete Tutorial with Real-World Example
In modern cloud architectures, services should not talk to each other directly. Instead, they should communicate using events.
This is exactly where Amazon SNS (Simple Notification Service) shines.
Letβs understand SNS from basics to real-world routing with SQS π
π What is Amazon SNS?
Amazon Simple Notification Service (SNS) is a fully managed pub/sub (publishβsubscribe) messaging service.
π It allows a producer to publish a message once, and SNS fan-outs that message to multiple subscribers.
Simple definition:
SNS helps you send messages to many systems at the same time without tight coupling.
π§ Core Concepts of SNS
1οΈβ£ SNS Topic
A Topic is a logical access point where messages are published.
-
Producers publish messages to a topic
-
Subscribers receive messages from the topic
π Think of a topic like a radio channel β anyone tuned in hears the broadcast.
2οΈβ£ Types of SNS Topics
SNS supports two types of topics:
πΉ 1. Standard Topic
-
At-least-once delivery
-
Messages may arrive out of order
-
High throughput
-
Supports:
-
SQS (Standard)
-
Lambda
-
HTTP/HTTPS
-
Email, SMS
-
β Best for notifications, alerts, fan-out events
πΉ 2. FIFO Topic
-
Exactly-once delivery
-
Message ordering guaranteed
-
Lower throughput compared to Standard
-
Can publish only to FIFO SQS queues
β Best for financial, order, payment workflows
π What is a Subscription?
A subscription connects a topic to an endpoint.
Examples of endpoints:
-
SQS queue
-
Lambda function
-
HTTP/HTTPS endpoint
-
Email / SMS
π SNS pushes messages to subscribers automatically.
π§ SNS Message Flow (High Level)
π Real-World Use Cases of SNS
SNS is widely used for:
-
π Application alerts & notifications
-
π§Ύ Order & payment event processing
-
π Microservices communication
-
π Fan-out architectures
-
π¨ CloudWatch alarms
-
π© Decoupling services
π― Real-World Scenario: ORDER & PAYMENT Events
Problem Statement
You have:
-
One application publishing events
-
Two SQS FIFO queues:
-
Order Queue
-
Payment Queue
-
Requirement:
-
If event is ORDER β send to Order SQS
-
If event is PAYMENT β send to Payment SQS
π Architecture Design
π Step 1: Create SNS FIFO Topic
-
Go to SNS β Topics
-
Click Create topic
-
Type: FIFO
-
Topic name:
-
Enable:
-
Content-based deduplication (optional)
-
π Step 2: Create SQS FIFO Queues
Create two queues:
πΉ Order Queue
πΉ Payment Queue
π Step 3: Subscribe SQS to SNS Topic
πΉ Subscribe Order Queue
-
Protocol: Amazon SQS
-
Endpoint:
order-queue.fifo
Filter policy:
πΉ Subscribe Payment Queue
-
Protocol: Amazon SQS
-
Endpoint:
payment-queue.fifo
Filter policy:
π SNS filtering works ONLY on message attributes, not message body.
π¨ Important Rule (Very Important!)
β SNS does NOT filter using message body
β
SNS filters using Message Attributes
π Step 4: Publish Message with eventType Attribute
Example: ORDER Message
Message Body
Message Attribute
| Name | Type | Value |
|---|---|---|
| eventType | String | ORDER |
β‘οΈ Message goes to Order Queue only
Example: PAYMENT Message
Message Body
Message Attribute
| Name | Type | Value |
|---|---|---|
| eventType | String | PAYMENT |
β‘οΈ Message goes to Payment Queue only
π Required: SQS Queue Policy
Each SQS queue must allow SNS to send messages.
Example:
π AWS console usually auto-adds this when subscribing.
π§ Why This Design Is Powerful
-
β Loose coupling
-
β Easy scalability
-
β Clean event routing
-
β Supports retries & DLQs
-
β Production-ready pattern
π Summary
| Concept | Description |
|---|---|
| SNS | Pub/Sub messaging service |
| Topic | Entry point for messages |
| Subscription | Delivery mechanism |
| Filter Policy | Route messages by attributes |
| FIFO Topic | Ordered & exactly-once delivery |
| SNS + SQS | Best event-driven architecture |
π Final Trainer Tip
SNS decides routing using message attributes, not JSON body
This single line saves hours of debugging π