SNS topics and fan-out
Publish once, deliver to many: SNS topics and subscriptions, the SNS-to-SQS fan-out, filter policies, delivery retries, dead-letter queues and FIFO topics.
SNS topics and fan-out
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A topic pushes a copy of each published message to every subscription. It is not a store you read from.
- Subscribe one SQS queue per consuming service, so each gets its own buffer, retry behaviour and dead-letter queue.
- The queue needs a resource policy that lets sns.amazonaws.com send to it, limited to your topic's ARN.
- A filter policy on a subscription drops unwanted messages inside SNS. It can match message attributes or a JSON body, and changes can take up to 15 minutes to apply.
- When retries are exhausted SNS discards the message unless that subscription has a dead-letter queue.
- By default SNS wraps the message in a JSON envelope. Raw message delivery hands an SQS subscriber exactly what you published.
One event, several listeners
An order is placed. Billing needs to charge it, the warehouse needs to pack it, analytics wants to count it. You could have checkout call all three, but then checkout has to know who cares, and adding a fourth listener means changing checkout.
Turn it around. Checkout announces “order placed” to a topic and stops caring. Anyone interested subscribes. It’s a mailing list: the sender writes one message, the list does the copying. This is publish and subscribe, and on AWS the simplest form is Amazon SNS.
SNS pushes. It does not hold messages for you to collect later. Subscriptions can be SQS queues, Lambda functions, HTTP/S endpoints, Firehose streams, email, SMS or mobile push. If you publish while nobody is subscribed, nobody gets it.
Fan-out: put a queue behind each listener
The pattern you’ll use most is a topic with one SQS queue per consuming service:
checkout --> SNS topic "orders" --+--> queue billing --> billing workers
+--> queue warehouse --> warehouse workers
+--> queue analytics --> analytics workers
The topic does the copying and each queue does the waiting. If the warehouse system is down for two hours, its queue grows while billing carries on. Each team sets its own visibility timeout, dead-letter queue and scaling, and a new consumer is a new subscription.
One step is easy to miss. SNS writes to your queue as a service, so the queue’s resource policy has to allow it:
{
"Effect": "Allow",
"Principal": { "Service": "sns.amazonaws.com" },
"Action": "sqs:SendMessage",
"Resource": "arn:aws:sqs:eu-west-1:111122223333:billing",
"Condition": {
"ArnEquals": { "aws:SourceArn": "arn:aws:sns:eu-west-1:111122223333:orders" }
}
}
Without the condition, any topic that knows your queue’s ARN could push into it through SNS.
Filtering and the envelope
By default every subscription receives everything. A filter policy narrows that. It lists property names with accepted values, such as {"type": ["physical"]}, and SNS checks it before delivering. The filter policy scope decides where it looks: message attributes (the default) or a JSON message body.
I ran this against moto, a local emulator. billing subscribed plainly. warehouse subscribed with that filter on a message attribute and with raw message delivery turned on. Then I published one physical and one digital order:
billing received 2 messages, each an envelope:
{"Type":"Notification","MessageId":"...","TopicArn":"...:orders",
"Message":"{\"order\":\"physical-1\"}","MessageAttributes":{...}, ...}
warehouse received 1 message:
{"order":"physical-1"}
The billing body is trimmed here. Two things show. The digital order never reached the warehouse queue, so no warehouse code had to receive and throw it away. And without raw message delivery your payload is a string inside SNS’s envelope, which your consumer must unwrap. moto doesn’t enforce queue policies, so this worked without the statement above. Real AWS would not deliver.
A filter is eventually consistent: AWS says a new or changed policy can take up to 15 minutes to take full effect.
When delivery fails
SNS retries according to a delivery policy for the protocol. For SQS and Lambda subscribers that hit server-side errors it is very patient: 100,015 attempts over 23 days. For SMS, email and mobile push it is 50 attempts over 6 hours. HTTP/S policies you can tune yourself. A client-side error, such as a deleted endpoint or a policy that now denies SNS, is not retried.
Either way, once SNS gives up the message is discarded. Unless you attached a dead-letter queue to that subscription, it is gone. This is a different thing from the queue’s own dead-letter queue: the subscription’s catches “SNS couldn’t hand it over”, the queue’s catches “the consumer couldn’t process it”. Production fan-outs usually want both.
Size and ordering
A topic accepts messages up to 256 KiB by default. You can raise a topic’s MaximumMessageSize to 1 MiB, but such a topic is limited to SQS, Lambda and Firehose subscribers and 100 subscriptions in total.
A standard topic promises at-least-once delivery without strict order, so consumers still need to be idempotent. A FIFO topic keeps order within a message group and deduplicates publishes, and pairs with SQS FIFO queues. Use it only when order is part of correctness.
SNS or something else?
SNS is the right tool for “copy this to these subscribers, fast, with simple filters”. If you need routing on rich patterns across many event sources, including AWS services and SaaS partners, look at EventBridge. If consumers must replay yesterday’s data, look at Kinesis.
About the video
The embed is AWS’s own “Back to Basics: Building Fan-Out Serverless Architectures Using SNS, SQS and Lambda”, August 2024, just over 5 minutes. Its chapters go from why point-to-point calls couple services, through publish and subscribe, to an order-processing example with SNS, SQS and message filtering. It’s short and stays at the architecture level; the queue policy, envelope, retries and dead-letter queue are covered here. I judged it from its chapters and description only, since transcripts couldn’t be fetched. English was judged from its English caption track and title. Be A Better Dev’s older SNS-versus-SQS comparison is in deeper.
What was run: the fan-out, filter and raw delivery output is from moto 5.1.22, an emulator. The queue policy, retry counts, size limits and filter propagation delay are from the AWS docs, checked 2026-11-29.
Where this fits
SNS copies a message to a list you maintain. The next lesson routes events by their content, with EventBridge.
Check yourself
Three services need every new order. Why not have all three read one SQS queue?
Consumers of one queue compete: each message goes to one of them. You need a copy per service, which means a topic with three queues subscribed.
You subscribed a queue to a topic, publishing succeeds, and nothing ever arrives. What do you check first?
The queue's access policy. It must allow the sns.amazonaws.com service principal to call sqs:SendMessage, usually with a condition on aws:SourceArn equal to the topic. Then check the subscription's filter policy, and whether the queue is encrypted with a KMS key that SNS may use.
Your consumer parses the body as an order and fails because the top-level keys are Type, MessageId and Message. Why?
Raw message delivery is off, so the body is the SNS envelope with your payload as a string in Message. Either unwrap it or enable raw message delivery on the subscription.
A new filter policy seems to be ignored for a few minutes after deployment. Is that a bug?
Not necessarily. Subscription filter policy changes are eventually consistent and can take up to 15 minutes to take full effect.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.