Understanding SNS: What It Is and How It Works
What is SNS?
SNS stands for Simple Notification Service.
It is a web service which makes it easy to set up, operate, and send a notification from the cloud.
It provides developers with the highly scalable, cost-effective, and flexible capability to publish messages from an application and sends them to other applications.
It is a way of sending messages. When you are using AutoScaling, it triggers an SNS service which will email you that "your EC2 instance is growing".
SNS can also send the messages to devices by sending push notifications to Apple, Google, Fire OS, and Windows devices, as well as Android devices in China with Baidu Cloud Push.
Besides sending the push notifications to the mobile devices, Amazon SNS sends the notifications through SMS or email to an Amazon Simple Queue Service (SQS), or to an HTTP endpoint.
SNS notifications can also trigger the Lambda function. When a message is published to an SNS topic that has a Lambda function associated with it, Lambda function is invoked with the payload of the message.
SNS Publishers and Subscribers
Amazon SNS is a web service that manages sending messages to the subscribing endpoint. There are two clients of SNS:
Subscribers
Publishers
Publishers
Publishers are also known as producers that produce and send the message to the SNS which is a logical access point.
Subscribers
Subscribers such as web servers, email addresses, Amazon SQS queues, AWS Lambda functions receive the message or notification from the SNS over one of the supported protocols (Amazon SQS, email, Lambda, HTTP, SMS).
How to use SNS
Move to the SNS service available under the application services.
Click on the Topics appearing on the left side of the Console.
Click on the Create Topic to create a new topic.
- Enter the topic name in a text box
The below screen shows that the topic has been created successfully
- To create a subscription, click on the Create subscription.
- Now, choose the endpoint type and enter the Endpoint address, i.e., where you want to send your notification.
- The below screen shows that the status of subscription is pending.
The below screen shows that mail has been sent to the subscriber. A Subscriber has to click on the Confirm Subscription.
- Click on the topic name, i.e., hello and then click on the Publish message.
- Enter the subject, Time to Live and Message body to send to the endpoint.
The message has been sent to all the subscribers that have been mentioned in the ID.
Advantages of AWS SNS:
Reliability and Scalability: SNS is highly reliable with automatic scaling to accommodate millions of messages, ensuring timely delivery even under heavy load.
Cost-effective: SNS operates on a pay-as-you-go model, making it cost-effective as users pay only for messages sent and delivered.
Multi-Protocol Delivery: SNS supports multiple protocols (e.g., HTTP/HTTPS, email, SMS, SQS, and mobile push notifications), allowing flexible message distribution.
High Throughput: SNS can process high volumes of messages per second, making it suitable for applications requiring fast message dissemination.
Serverless Integration: It integrates well with AWS serverless services like Lambda, making it easier to trigger functions based on incoming messages.
Disadvantages of AWS SNS:
Limited Message Storage: SNS is a push-based messaging system and does not store messages for a long time. If a subscriber is unavailable, they may miss messages.
Delivery Uncertainty for Some Protocols: For email or SMS, there's no guaranteed delivery acknowledgment, which may cause issues for critical notifications.
No Ordering: SNS does not guarantee message ordering, which may cause issues in applications where sequence matters.
Limited Filtering: SNS allows filtering messages by attributes, but the filtering options may be limited for complex message structures.
Uses of AWS SNS:
Application Alerts and Notifications:
- Used to send notifications (such as application status, billing, or error alerts) to developers, IT staff, or end-users via SMS, email, or push notifications.
Message Fan-out with SQS:
- SNS can be combined with Amazon Simple Queue Service (SQS) to distribute messages to multiple SQS queues, which is helpful for decoupling microservices or other distributed systems.
Real-time Event Processing:
- By triggering AWS Lambda functions, SNS can process events in real-time for applications such as IoT, monitoring, and data processing pipelines.
Cross-Region Notifications:
- SNS is used to send notifications across multiple AWS regions, which is essential for multi-region architectures and disaster recovery setups.
Mobile Push Notifications:
- SNS can send push notifications to mobile devices across various platforms (iOS, Android) through integrated push services, which is useful for mobile app notifications.
What is SQS?
SQS stands for Simple Queue Service.
SQS was the first service available in AWS.
Amazon SQS is a web service that gives you access to a message queue that can be used to store messages while waiting for a computer to process them.
Amazon SQS is a distributed queue system that enables web service applications to quickly and reliably queue messages that one component in the application generates to be consumed by another component where a queue is a temporary repository for messages that are awaiting processing.
With the help of SQS, you can send, store and receive messages between software components at any volume without losing messages.
Using Amazon sqs, you can separate the components of an application so that they can run independently, easing message management between components.
Any component of a distributed application can store the messages in the queue.
Messages can contain up to 256 KB of text in any format such as json, xml, etc.
Any component of an application can later retrieve the messages programmatically using the Amazon SQS API.
Different between SQS And SNS
Here's a comparison between Amazon Simple Queue Service (SQS) and Amazon Simple Notification Service (SNS) :
Feature | Amazon SQS | Amazon SNS |
Type of Service | Message Queueing Service | Pub/Sub Messaging Service |
Message Delivery Model | Poll-based: Consumers pull messages from the queue | Push-based: SNS pushes messages to subscribers |
Use Case | Message queuing and decoupling between producers and consumers | Real-time notifications and fan-out messaging |
Message Recipients | One-to-one communication (point-to-point) | One-to-many communication (broadcast) |
Message Persistence | Messages are stored in the queue until consumed | Messages are not stored; sent directly to subscribers |
Message Retention | Up to 14 days | No message retention (only delivers in real-time) |
Ordering of Messages | FIFO queues available for ordered delivery | No ordering; messages are sent to subscribers as they come |
Message Duplication | FIFO: No duplicates, Standard Queue: Duplicates possible | Duplicates possible in cases of retries |
Supported Protocols | N/A (Only SQS clients can consume messages) | HTTP/HTTPS, email, SMS, SQS, Lambda, mobile push notifications |
Scaling | Scales based on message volume, queue length | Scales based on number of subscribers |
Retries | Dead-letter queues can capture failed messages | Automatically retries delivery based on configuration |
Cost Model | Pay per request and per message stored in the queue | Pay per request and per message delivered |
Typical Use Cases | Decoupling microservices, task queues, buffering work requests | Application alerts, real-time notifications, fan-out to multiple endpoints |
Conclusion :
AWS SNS, on the other hand, serves as a reliable, flexible messaging service designed for broadcasting messages to multiple endpoints. By supporting multiple communication protocols, SNS allows for real-time notifications, alerts, and fan-out messaging to a wide audience or different application components.
If you have any questions, need clarifications, or want to discuss anything related to AWS technologies, feel free to reach out to me on LinkedIn. Connect with me at Aditya Gadhave, and I'll be more than happy to assist you. ๐
Subscribe to my newsletter
Read articles from Aditya Gadhave directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Aditya Gadhave
Aditya Gadhave
๐ Hello! I'm Aditya Gadhave, an enthusiastic Computer Engineering Undergraduate Student. My passion for technology has led me on an exciting journey where I'm honing my skills and making meaningful contributions.