Skip to Content

SNS fan-out and filtering

The SNS plus SQS fan-out pattern, sending S3 events to several queues, SNS to S3 through Firehose, SNS FIFO topics, and message filtering.

SNS + SQS fan-out

SNS plus SQS fan-out: the buying service publishes once to an SNS topic, and each subscribed SQS queue, one for the fraud service and one for the shipping service, gets its own copy to process at its own pace; each queue policy must allow SNS to write, and it works across Regions SNS plus SQS fan-out: the buying service publishes once to an SNS topic, and each subscribed SQS queue, one for the fraud service and one for the shipping service, gets its own copy to process at its own pace; each queue policy must allow SNS to write, and it works across Regions

Push once to SNS, receive in every queue.

Fan-out combines the two services:

  • Push once to SNS; every SQS queue subscribed to the topic receives a copy.
  • Fully decoupled, no data loss: SNS delivers to the queues, and SQS keeps each copy until its consumer processes it.
  • SQS adds persistence, delayed processing and retries of work.
  • More queues can be added (more consumer services) over time without touching the producer.
  • Each SQS queue's access policy must allow SNS to write to it.
  • Cross-Region delivery: a topic can deliver to queues in other Regions.

Application: S3 events to several queues

For the same combination of event type (for example object created) and prefix (for example images/), S3 allows only one event notification rule. To send the same S3 event to several SQS queues, send it to an SNS topic and fan out from there: to queues, a Lambda function, and anything else.

Application: SNS to S3 through Firehose

SNS can deliver to Amazon Data Firehose, so every message published to a topic can be stored: buying service → SNS topic → Firehose → Amazon S3 (or any other Firehose destination).

SNS FIFO topics

A FIFO topic keeps messages in order, like an SQS FIFO queue:

  • ordering by message group ID (messages in the same group are delivered in order);
  • deduplication, using a deduplication ID or content-based deduplication;
  • subscribers can be SQS FIFO and SQS standard queues;
  • limited throughput, the same as SQS FIFO (300 messages per second per message group, 3,000 per topic by default).

SNS FIFO + SQS FIFO fan-out is the pattern when fan-out + ordering + deduplication are needed.

Message filtering

SNS message filtering: a new order with state placed is published to the topic; it reaches the queue whose filter policy is state placed and the queue with no filter policy, but not the queues filtering on cancelled or declined SNS message filtering: a new order with state placed is published to the topic; it reaches the queue whose filter policy is state placed and the queue with no filter policy, but not the queues filtering on cancelled or declined

Each subscription takes only the messages it wants.
  • A filter policy is a JSON policy on a subscription that says which messages it receives, based on message attributes or the message body.
  • A subscription without a filter policy receives every message.
  • Example: a buying service publishes orders with state = placed, cancelled or declined. The "placed orders" queue filters on placed, the "cancelled" queue and an email subscription filter on cancelled, and an "all orders" queue has no filter.

Real-world examples

  • An e-commerce site publishes each order once; separate queues feed fraud checks, shipping and analytics, and a new loyalty service is added later with one new subscription.
  • A photo app fans out each S3 upload event through SNS to a thumbnail queue, a moderation queue and a Lambda function.
  • A price feed uses an SNS FIFO topic and SQS FIFO queues so every store gets price changes in order.

Exam tips

  • "One event, processed independently by several services" → SNS + SQS fan-out.
  • "Send the same S3 event to several queues" → S3 → SNS → SQS queues.
  • "Fan-out with ordering and deduplication" → SNS FIFO + SQS FIFO.
  • "Each subscriber should only get some messages" → SNS filter policies.
  • "Store all messages from a topic in S3" → SNS → Data Firehose → S3.
  • "Queue doesn't receive messages from the topic" → the queue's access policy must allow SNS.

Common traps

  • "Create several S3 event rules with the same prefix and event type." S3 allows only one; fan out with SNS.
  • "Publish the same message to each queue from the producer." That couples the producer to every consumer; publish once to SNS.

Hands-on lab: SNS to two queues

Goal: fan out one message to two SQS queues, one with a filter.

Costs money

A few requests cost far less than a cent. Any Region works.

Before you start: have the saa-alerts topic from the SNS lab.

Create two queues and subscribe them

Create standard queues saa-all and saa-cancelled. For each, open it → Subscribe to Amazon SNS topic → saa-alerts. The console adds the access policy that lets SNS write to the queue.

Add a filter policy

In SNS, open the saa-cancelled subscription → Edit → Subscription filter policy, scope Message attributes: {"state": ["cancelled"]}. Save.

Publish two messages

Publish to saa-alerts with a message attribute state (String) = placed, then another with state = cancelled. Poll both queues: saa-all has two messages, saa-cancelled has only the cancelled one.

Expected result: one publish, delivered to every matching queue.

Clean up: delete the subscriptions, the saa-alerts topic and both queues.

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt