AWS SNS

The Simple Notification Service is another form of messaging service in AWS, similar to SQS. The key difference is that SNS pushes messages out to subscribers whereas SQS stores the messages until they are processed.

Before a message can be sent, and SNS topic must exist. One service such as a lambda will publish to this topic. Another service, will subscribe to the topic and be alerted when a message is sent to it.

With SNS, one message can be sent to multiple subscribers simultaneously. In addition there is message filtering at the subscription level so subscribers can choose to receive only the messages they are interested in.

Many other AWS services can act as SNS subscribers. Some common examples:

  • SQS queues
  • Lambda functions (trigger on receipt of a message)
  • HTTP endpoints
  • Email/SMS/Push notifications to mobile apps

Combining with SQS

Utilising SNS with SQS is a common pattern in serverless applications.

We use SNS to decouple publishers from subscribers and then use SQS to handle the different subscriber tasks. This is known as the “Fanout-Queue” pattern.

SQS fanout pattern

For example, an order is placed which results in a publication event to an SNS topic.

Multiple systems subscribe to this topic, e.g. an inventory system, a shipping system and an analytics system.

Each system has its own queue for processing the message.