Micro-services asynchronous communication — AWS SNS

Arunkumar Krishnan
3 min readSep 6, 2022

What is SNS?

Amazon SNS is the abbreviation of the popular AWS service ‘simple notification service’. It is a managed publish/subscribe (also known as “pub/sub”) service from Amazon Web Services. Essentially with SNS, one can publish messages to ‘SNS topic’, which will be received by multiple subscribers at the same time.

Publisher/Subscribe in simple terms

One popular analogy — I came across recently is that SNS can be thought of as a megaphone, where a speaker gives a speech to a crowd of listeners.

Everyone in the crowd receives all the words spoken at the same time. There could be new people coming late, who will miss all the old speech and start listening from the moment they join.

This can be mapped to SNS as below:

  • Speaker => Publisher of message
  • Megaphone => SNS Topic
  • Speech => Message
  • Crowd (people) => consumers of message

How is it different from SQS?

Compared to SQS, SNS mainly removes the need to poll for new messages and messages are delivered to more than 1 consumers immediately.

From an architectural point of view, SNS implements a “push model”, where subscribers/consumers receive the messages that are published to a topic. SQS implements a “pull model”, where consumers pull messages from a queue.

When to use SNS over SQS?

While SNS and SQS are related, they serve different purposes. SQS is a queuing system. Its purpose is to queue messages and ensure that each message is delivered exactly once a.k.a point-to-point messaging. SNS is a pub-sub system. Its purpose is to send notifications to all interested parties.

COMMON USAGE:

Application Integration

The Fanout scenarios like - an application wanted to process things in parallel, SNS could be useful. For example, we can model SNS to send notification when an order is placed for a product. We can have an EC2 server instance/s to handle the processing or fulfillment of the order. And another EC2 server instance/s to a data warehouse for analysis of all orders received.

Application alerts

Application and system alerts on predefined thresholds can be modeled to send notifications. For example, you can receive immediate notification when an event occurs, such as a change EC2 Auto Scaling group, or a new file being uploaded to a S3 bucket, or a metric threshold breached in CloudWatch.

User notifications

Amazon SNS can send email messages and text messages (SMS messages) to individuals or groups. For example, you could send e-commerce order confirmations as user notifications.

Coding:

Enough theory. It’s time to get your hands dirty.

CreateTopic() : Setup by publisher & subscriber:

We need to create a topic (to be used by both publisher & subscriber).

def create_topic(name):
sns = boto3.resource(“sns”)
topic = sns.create_topic(Name=name)
return topic
if __name__ == ‘__main__’:
print(create_topic(“test-sns”))

Output:
$ sns.Topic(arn=”arn:aws:sns:us-west-2:xxx:test-sns”)

Subscribe() - By subscriber:

Subscribe to a topic to receive notifications.

def subscribe(topic, protocol, endpoint):
subscription = topic.subscribe(Protocol=protocol, Endpoint=endpoint, ReturnSubscriptionArn=True)
return subscription
if __name__ == ‘__main__’:
subscribe(“arn:aws:sns:us-west-2:xxx:test-sns”, ”https”, ”https::/myapp.com/test-sns/onevent”)
# Here we configured to receive a callback @ App endpoint for every message published to topic.
# SNS supports other form of notifications like email, SMS, triggering LAMBDA, other AWS services, etc.. more details https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sns.html#SNS.Client.subscribe

publish_message() — By publisher:

Next, we will look at how to publish a message on a topic, that will then get delivered to all subscribers.

def publish_message(topic, message):
response = topic.publish(Message=message)
message_id = response[‘MessageId’]
return message_id
if __name__ == ‘__main__’:
publish_message(“arn:aws:sns:us-west-2:xxx:test-sns”, “test SNS message”)

--

--