CodePark/ questions / SQS, SNS & EventBridge
START PRACTICINGPRACTICE
Backend question · Cloud & AWS › SQS, SNS & EventBridge

When an order is placed, the email, analytics and inventory services all need to react. How do you build this with SNS and SQS, and why not have them all read one SQS queue?

One SQS queue delivers each message to only one consumer. So three services reading one queue would compete for messages, and each service would see only some of the orders.

Use fan-out instead:

  • The order service publishes to one SNS topic.
  • Each service has its own SQS queue, subscribed to that topic.
  • SNS copies every message to every queue.

Each queue's access policy must allow the topic to send messages to it.

Each consumer now gets its own buffering, retries, scaling and DLQ (dead-letter queue). So a slow analytics service does not delay the emails.

Two useful options:

  • Subscription filter policies let a queue receive only matching messages, such as only orders over a certain value.
  • Raw message delivery sends the original payload without the SNS envelope (the extra wrapper SNS adds). Enable it if consumers want only the original message.

To add a new consumer later, you add a queue and a subscription. The publisher does not change.

  • A queue delivers each message to only one consumer, so several consumers on one queue would compete.
  • Publish to an SNS topic, and give each consumer its own SQS queue subscribed to it.
  • Each queue's policy must allow the topic to send to it.
  • Each consumer gets its own buffering, retries, scaling and DLQ.
  • Filter policies and raw message delivery are available, and new consumers need no publisher changes.