Simple Notification Service (SNS)
Learn about Amazon SNS, the types of topics we can create using this service, and the subscriber endpoints it supports.
Amazon Simple Notification Service (SNS) is a message delivery service provided by Amazon that delivers messages from a publisher to a subscriber. It uses the publish-subscribe model to deliver these messages. In this model, a publisher sends a message to a topic, which is then delivered to all of its subscriber endpoints. Since the publishers and subscribers don’t need to know about each other, Amazon SNS can be used to decouple the components of a distributed system and deliver messages between them.
In Amazon SNS, a topic is a communication channel that receives messages from a publisher and delivers them to all available subscriber endpoints. We can create two types of SNS topics: a standard topic and a FIFO (first in, first out) topic.
Standard topics
In standard topics, messages are sent to the subscribers at least once, and there is no mechanism to preserve the order of the messages received from the topic’s producers. Other than this, in standard topics, there is a chance that message duplicates exist, as a producer might send the same message multiple times to the topic.
Standard topics are used in applications where message ordering is not critical and requires a high throughput.
FIFO topics
We can create FIFO (first in, first out) topics in Amazon SNS. In these topics, messages are sent to subscribers exactly once, and messages are delivered to the subscriber endpoints in the order they were received. These topics use a deduplication ID. In case multiple messages are received with the same deduplication ID, within five minutes, only one message is forwarded to the topic’s subscribers.
These topics are used in applications where the order of messages being sent and received is critical, and low throughput can be tolerated.
Note: We need to provide the suffix
.fifo
in our topics’ name in case we want to create a FIFO topic.
Ordering messages in FIFO topics
The order of messages being published is retained in FIFO topics and delivered exactly once to the topics’ subscribers. SNS does this by associating a sequence number with each message it receives. Also, the message group ID attribute is used in FIFO topics. This attribute is mandatory and set by the publisher when they make a publish message request. SNS ensures that messages with the same group ID are delivered to the subscribers in the order they are received.
One thing to keep in mind is that SNS may deliver messages with different timestamps to different subscribers of the topic. The order of the messages being sent is retained even in this case. Let’s assume an SNS topic has two subscribers, an SQS queue, and an email. If a producer publishes messages M1 and M2, there is a possibility that the SQS queue will receive the message M1 before SNS sends it to the user’s email. Even in this case, both SQS and the email will first receive the message M1 and then M2.
Subscriber endpoints in Amazon SNS
Amazon SNS supports various subscriber endpoints and provides us with application-to-application (A2A) and application-to-person (A2P) messaging. These subscriber endpoints receive the messages sent to the SNS topic by a publisher. We can filter the messages these endpoints receive using message filtering policies.
Amazon SNS supports emails, mobile text messages, and push notifications as subscriber endpoints in application-to-person messages and uses a device token on our behalf to send notifications to these services.
Application-to-application messages
The application-to-application messaging service available in Amazon SNS allows us to send messages between different components of distributed systems, microservices, and event-driven applications. Following are the subscriber endpoints SNS supports:
Lambda functions: We can invoke a Lambda function by adding it as a subscriber to our SNS topic. The Lambda function receives the message sent from a publisher as an input parameter.
Amazon SQS: Amazon SNS and SQS can be used to create a fanout architecture in applications that require immediate notifications and also want to store these notifications in a queue to process them later. We can also use FIFO queues with FIFO topics in case we want to ensure the order of the messages received is retained. In order to allow SNS to send notifications to a SQS queue, we must configure the access policy of our queue.
Kinesis Data Firehose: We can add Kinesis Data Firehose delivery streams as a subscriber to an SNS topic which can then be used to fan out SNS notifications to other AWS services such as Amazon S3, Amazon Redshift, Amazon OpenSearch Service, and other third services such as Datadog and MongoDB.
HTTP endpoints: Amazon SNS can send
POST
requests to the HTTP and HTTPS endpoints subscribed to it. The one thing we must keep in mind is that these HTTP endpoints must supportHTTP/1.1 401 Unauthorized
header responses.
Pricing in SNS
In Amazon SNS, we only pay for the resources we use. In standard topics, we pay for the number of API calls made to the topic per month and the cost of sending messages to the different subscriber endpoints. In FIFO topics, we are charged on the number of published and subscribed messages, and their payload. Here, the number of subscribed messages is calculated by multiplying the number of published messages by the number of subscriber endpoints.
We are also charged for message filtering in case we are filtering messages based on their payload and message data protection. Other than this, we are also charged for the SMS messages sent by Amazon SNS, depending upon the destination country.
Get hands-on with 1300+ tech skills courses.