Why AWS SNS and AWS SQS are often used together?

What is SNS?

SNS (Simple Notification Service) is a distributed publish-subscribe or Notification system. Messages are published to subscribers as and when they are send by publishers to SNS.

It is used in scenarios where you may not want the external services to make connection to your host to get any time of messages (eg. lifecycle events). In such cases, external services can subscribe to your SNS topic and gets the messages immediately once it is published by the publisher.

What is SQS?

SQS (Simple Queue Service) is a distributed queueing system. As name denotes, it is a queue where messages are pushed (send) and the message stays in the queue till the consumer/receivers poll the message from the SQS or till the maximum SQS message retention period of 14 days. A messages can be received by only one receiver at a time.

It is used in scenarios where you want to receive messages at your own pace.

As SQS uses polling mechanism and hence it introduces some latency in message delivery unlike SNS where the messages gets pushed to subscribers immediately.

When to use it together in a Design?

SNS and SQS are usually coupled together in designs. If an SNS is configured to send messages to an HTTP endpoint or email, failures like client being offline, network issues and host failures may result in the message being dropped. To avoid this and to receive message at client’s own pace, SNS can send message to subscribed SQS queue of the client. The client reads the message, process and delete it. This helps achieving guaranteed delivery of messages.

SNS can have multiple subscribers and it distributes several copies of message to its subscribers. SQS can be one of the subscriber and stores the copy of message till it gets consumed by the receiver or its retention period.