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
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
- 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,cancelledordeclined. The "placed orders" queue filters onplaced, the "cancelled" queue and an email subscription filter oncancelled, 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
Amazon SNS
SNS publish/subscribe with topics and subscribers, the services that publish to it, how to publish, and its security.
Kinesis and Data Firehose
Kinesis Data Streams for real-time streaming, its shards and capacity modes, Amazon Data Firehose for loading streams into S3, Redshift and more, and how the two differ.