Simple Notification Service

0

🌐 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)

Producer β†’ SNS Topic β†’ Subscribers (SQS / Lambda / Email)

πŸ“Œ 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

Application
|
v
SNS Topic (FIFO)
|
|--(eventType=ORDER)--> Order Queue (FIFO)
|
|--(eventType=PAYMENT)-> Payment Queue (FIFO)

πŸ›  Step 1: Create SNS FIFO Topic

  1. Go to SNS β†’ Topics

  2. Click Create topic

  3. Type: FIFO

  4. Topic name:

    order-payment-topic.fifo
  5. Enable:

    • Content-based deduplication (optional)


πŸ›  Step 2: Create SQS FIFO Queues

Create two queues:

πŸ”Ή Order Queue

order-queue.fifo

πŸ”Ή Payment Queue

payment-queue.fifo

πŸ›  Step 3: Subscribe SQS to SNS Topic

πŸ”Ή Subscribe Order Queue

  • Protocol: Amazon SQS

  • Endpoint: order-queue.fifo

Filter policy:

{
"eventType": ["ORDER"]
}

πŸ”Ή Subscribe Payment Queue

  • Protocol: Amazon SQS

  • Endpoint: payment-queue.fifo

Filter policy:

{
"eventType": ["PAYMENT"]
}

πŸ“Œ 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

{
"orderId": 101,
"amount": 500,
"currency": "INR"
}

Message Attribute

Name Type Value
eventType String ORDER

➑️ Message goes to Order Queue only


Example: PAYMENT Message

Message Body

{
"paymentId": 9001,
"orderId": 101,
"status": "SUCCESS"
}

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:

{
"Effect": "Allow",
"Principal": { "Service": "sns.amazonaws.com" },
"Action": "sqs:SendMessage",
"Resource": "QUEUE_ARN",
"Condition": {
"ArnEquals": {
"aws:SourceArn": "SNS_TOPIC_ARN"
}
}
}

πŸ“Œ 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 πŸ˜„

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top