Understanding SNS: What It Is and How It Works

Aditya GadhaveAditya Gadhave
7 min read

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

SNS

Amazon SNS is a web service that manages sending messages to the subscribing endpoint. There are two clients of SNS:

  • Subscribers

  • Publishers

SNS

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

SNS

The below screen shows that the topic has been created successfully

SNS

  • To create a subscription, click on the Create subscription.

SNS

  • Now, choose the endpoint type and enter the Endpoint address, i.e., where you want to send your notification.

SNS

  • The below screen shows that the status of subscription is pending.

SNS

The below screen shows that mail has been sent to the subscriber. A Subscriber has to click on the Confirm Subscription.

SNS

  • Click on the topic name, i.e., hello and then click on the Publish message.

SNS

  • Enter the subject, Time to Live and Message body to send to the endpoint.

SNS

The message has been sent to all the subscribers that have been mentioned in the ID.

Advantages of AWS SNS:

  1. Reliability and Scalability: SNS is highly reliable with automatic scaling to accommodate millions of messages, ensuring timely delivery even under heavy load.

  2. Cost-effective: SNS operates on a pay-as-you-go model, making it cost-effective as users pay only for messages sent and delivered.

  3. Multi-Protocol Delivery: SNS supports multiple protocols (e.g., HTTP/HTTPS, email, SMS, SQS, and mobile push notifications), allowing flexible message distribution.

  4. High Throughput: SNS can process high volumes of messages per second, making it suitable for applications requiring fast message dissemination.

  5. 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:

  1. 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.

  2. Delivery Uncertainty for Some Protocols: For email or SMS, there's no guaranteed delivery acknowledgment, which may cause issues for critical notifications.

  3. No Ordering: SNS does not guarantee message ordering, which may cause issues in applications where sequence matters.

  4. Limited Filtering: SNS allows filtering messages by attributes, but the filtering options may be limited for complex message structures.

Uses of AWS SNS:

  1. 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.
  2. 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.
  3. 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.
  4. Cross-Region Notifications:

    • SNS is used to send notifications across multiple AWS regions, which is essential for multi-region architectures and disaster recovery setups.
  5. 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) :

FeatureAmazon SQSAmazon SNS
Type of ServiceMessage Queueing ServicePub/Sub Messaging Service
Message Delivery ModelPoll-based: Consumers pull messages from the queuePush-based: SNS pushes messages to subscribers
Use CaseMessage queuing and decoupling between producers and consumersReal-time notifications and fan-out messaging
Message RecipientsOne-to-one communication (point-to-point)One-to-many communication (broadcast)
Message PersistenceMessages are stored in the queue until consumedMessages are not stored; sent directly to subscribers
Message RetentionUp to 14 daysNo message retention (only delivers in real-time)
Ordering of MessagesFIFO queues available for ordered deliveryNo ordering; messages are sent to subscribers as they come
Message DuplicationFIFO: No duplicates, Standard Queue: Duplicates possibleDuplicates possible in cases of retries
Supported ProtocolsN/A (Only SQS clients can consume messages)HTTP/HTTPS, email, SMS, SQS, Lambda, mobile push notifications
ScalingScales based on message volume, queue lengthScales based on number of subscribers
RetriesDead-letter queues can capture failed messagesAutomatically retries delivery based on configuration
Cost ModelPay per request and per message stored in the queuePay per request and per message delivered
Typical Use CasesDecoupling microservices, task queues, buffering work requestsApplication 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. ๐Ÿ˜Š

0
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.