AWS Simple Notification Service(SNS)

AWS Simple Notification Service(SNS)

Simple Notification Service(SNS) is a managed service based on the publisher/subscriber model aka pub-sub. The messages in SNS are delivered via Topics. The publisher (or producer) sends a message to a topic. The subscriber (or consumer) receives messages from the topic. Multiple producers can send messages to SNS topics and multiple entities can be consumers.

It is important to understand at the outset that SNS sends messages to consumers or subscribers using a "push" mechanism. So SNS sends a message immediately to its appropriate destination as soon as it is received in the topic. This makes SNS allow applications to send time-critical messages to multiple subscribers.

Types of communication by SNS

Communication in SNS happens via a Topic. As mentioned above, there are two parties involved publishers and subscribers. Publishers or Event Sources can be any application or service running in an AWS environment capable of sending messages. These include among others AWS Lambda, EC2, Cloudwatch etc.

SNS's role in communication can be divided into two broad categories based on the Event Destination of the topic

Application-To-Application: SNS supports communication between two applications. The event destination could be

  1. Amazon Lambdas: Lambdas could be configured to be triggered by an SNS message. Here message acts as a payload.

  2. AWS SQS: SNS can send a message to an SQS queue. Using both of them together, messages can be delivered to applications that require immediate notification of an event and also persisted in an Amazon SQS queue for other applications to process at a later time

  3. HTTP/HTTPS endpoint: SNS sends a post request to the endpoint using a message as the request body.

  4. Amazon Kinesis Data Firehouse: Through Kinesis Data Firehose delivery streams, you can fan out Amazon SNS notifications to Amazon Simple Storage Service (Amazon S3), Amazon Redshift, Amazon OpenSearch Service (OpenSearch Service), and to third-party service providers such as Datadog, New Relic, MongoDB, and Splunk.

Application-To-Person: means using Amazon SNS to send notifications to end users. These are the following Event Destinations:

  1. Mobile Text Messages(SMS): You can send a message directly to a phone number, or multiple phone numbers at once by subscribing those phone numbers to a topic and sending your message to the topic. When you send SMS messages, you can identify yourself to your recipients using either SenderID(alphabetic name) or Origination Number(numeric string to identify phone number)

  2. Mobile push notifications: sending push notifications directly to mobile applications. It can appear as message alerts or sound alerts.

  3. Email notifications: based on user emails.

Understanding SNS in depth would require us to go through the following concepts in detail.

SNS Topic

A topic is a communication channel to which publishers send messages and from which subscribers consume them. We need to specify the following while configuring the topic

  • Type of the topic: FIFO, Standard

  • Name and display-name of the topic.

  • Enable encryption for securing data

  • Access policy to configure who can publish messages

  • Delivery Retry Policy: to decide what happens in case of failed delivery of messages.

  • Delivery Status Logging: to determine how SNS logs delivery of messages in CloudWatch

Subscribers

To receive messages we need to set up subscribers on the topic. Two things need to be configured

  • Protocol: To specify what would be the type of event destination viz HTTPS, SQS, lambda or email etc.

  • Endpoint: To specify the actual event destination e.g. URL for HTTPS endpoint or queue name for SQS.

Message Publishing

A publisher sending a message to a topic is called message publishing.

  • Message attributes: gives structured metadata items like timestamps, identifiers etc. These are separate from the message body but sent along with it.

  • Message batching: publishing multiple messages to the topic in a single API call.

Message Delivery

A subscriber receiving a message from a topic is called message delivery.

  • Raw message delivery: when enabled, all the SNS message metadata is stripped and only the actual message is sent.

  • Some services support cross-region delivery like SQS.

  • Message delivery status can also be logged and sent to Clouudwatch.

  • We can configure Dead Letter Queues(based on SQS) when a message fails to deliver for any further analysis if needed.

  • Message archiving and analytics: as a fan-out notification to Kinesis Data Firehose Delivery streams.

Message Durability

Amazon SNS uses several strategies that work together to provide message durability: Published messages are stored across multiple, geographically separated servers and data centers. If a subscribed endpoint isn't available, Amazon SNS runs a delivery retry policy. To preserve any messages that aren't delivered before the delivery retry policy ends, you can create a dead-letter queue.

Message Filtering

A subscriber to a topic receives all the messages published in the topic by default. SNS provides configuring filter policy on subscriber so that subscriber receives only specific types of messages. Filtering scope could be Message attributes(default) or message body.

Filter policy is based on an attribute and list of values specified in it. A message should have the specified attribute and at least one of the values from a list of values to pass the filter.

Message Security

SNS supports message security by supporting Encryption at Rest. Server-side encryption (SSE) lets you store sensitive data in encrypted topics. SSE protects the contents of messages in Amazon SNS topics using keys managed in AWS Key Management Service.

Conclusion

Amazon Simple Notification Service offers a robust and reliable mechanism to serve push-based notifications to various services. It works seamlessly with other services like SQS, Lambdas, Firehose etc. It is extremely important in sending end-user notifications based on emails, messages etc. Further, it finds its primary utility in broadcasting messages to multiple subscribers. It also forms a critical component when supporting real-time alerts and monitoring of applications!

If you found this article informative, don't forget to like and share it! :)