1/15/21

SQS vs SNS

 Here's a comparison of the two:

Entity Type

  • SQS : Queue (Similar to JMS)
  • SNS : Topic (Pub/Sub system)

Message consumption

  • SQS : Pull Mechanism - Consumers poll and pull messages from SQS
  • SNS : Push Mechanism - SNS Pushes messages to consumers

Use Case

  • SQS : Decoupling 2 applications and allowing parallel asynchronous processing
  • SNS : Fanout - Processing the same message in multiple ways

Persistence

  • SQS : Messages are persisted for some (configurable) duration if no consumer is available (max 2 weeks), so consumer does not have to be up when messages added to queue.
  • SNS : No persistence. Whichever consumer is present at the time of message arrival gets the message and the message is deleted. If no consumers are available then the message is lost after a few retries.

Consumer Type

  • SQS : All the consumers are typically identical and hence process the messages in exact same way (each message is processed once by one consumer, though in rare cases messages may be re-sent)
  • SNS : The consumers might process the messages in different ways

Sample applications

  • SQS : Jobs framework: The Jobs are submitted to SQS and the consumers at the other end can process the jobs asynchronously. If the job frequency increases, the number of consumers can simply be increased to achieve better throughput.
  • SNS : Image processing. If someone uploads an image to S3 then watermark that image, create a thumbnail and also send a Thank You email. In that case S3 can publish notifications to a SNS Topic with 3 consumers listening to it. 1st one watermarks the image, 2nd one creates a thumbnail and the 3rd one sends a Thank You email. All of them receive the same message (image URL) and do their processing in parallel.

How to do:
https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-configure-subscribe-queue-sns-topic.html

You can subscribe one or more Amazon SQS queues to an Amazon Simple Notification Service (Amazon SNS) topic. When you publish a message to a topic, Amazon SNS sends the message to each of the subscribed queues. Amazon SQS manages the subscription and any necessary permissions. For more information about Amazon SNS, see What is Amazon Simple Notification Service? in the Amazon Simple Notification Service Developer Guide.

When you subscribe an Amazon SQS queue to an SNS topic, Amazon SNS uses HTTPS to forward messages to Amazon SQS. For information about using Amazon SNS with encrypted Amazon SQS queues, see Configure KMS permissions for AWS services.

To subscribe a queue to an SNS topic (console)

  1. Open the Amazon SQS console at https://console.aws.amazon.com/sqs/.

  2. In the navigation pane, choose Queues.

  3. From the list of queues, choose the queue to subscribe to the SNS topic.

  4. From Actions, choose Subscribe to Amazon SNS topic.

  5. From the Specify an Amazon SNS topic available for this queue menu, choose the SNS topic for your queue.

    If the SNS topic isn't listed in the menu, choose Enter Amazon SNS topic ARN and then enter the topic's Amazon Resource Name (ARN).

  6. Choose Save.

  7. To verify the result of the subscription, publish to the topic and then view the message that the topic sends to the queue. For more information, see Publishing a message to an Amazon SNS topic in the Amazon Simple Notification Service Developer Guide.