"One message can only feed one consumer." With a single queue that is true: once a worker takes a message, it's gone for everyone else. But real events usually matter to several parts of a system at once. An order needs billing, an email and an analytics record. The fan-out pattern solves this: you publish once, and every subscriber gets its own copy, at its own pace.
The pattern
Fan-out combines two services:
- An Amazon SNS topic in front. SNS is push-based publish/subscribe: anything published to the topic is delivered to every subscription.
- One Amazon SQS queue per job, each subscribed to the topic. SQS is pull-based: messages wait in the queue until a worker asks for them.
The producer publishes a message to the topic once. SNS pushes a copy into every subscribed queue, and each consumer (a Lambda function, a container, a server) reads its own queue. The producer doesn't know or care how many consumers exist. Adding a new job means adding a new queue and subscription, not changing the producer's code.
Why put queues behind the topic
You could subscribe the workers to SNS directly, but the queue in between is what gives each job its independence:
- Failure isolation: if the email worker is down, its messages simply wait in its queue. Billing and analytics keep going.
- Own pace: a slow consumer builds up a backlog in its own queue without slowing the others.
- Retries and dead-letter queues per job: each queue has its own visibility timeout and its own DLQ, so one job's poison messages don't affect the rest.
Filter policies
By default, every subscription receives every message. When a job only cares about some of them, add a subscription filter policy: a small JSON document that SNS checks against the message attributes or, if you choose, the message body. For example, a refunds queue can subscribe with {"type": ["refund"]} and never see sales. Filtering at the topic is cheaper and simpler than receiving everything and throwing most of it away in your code.
Order matters too: an SNS FIFO topic delivers to SQS queues in order per message group and removes duplicates, for flows where sequence is important.
The common mistake: permissions
The most frequent reason "fan-out doesn't work" is that the messages never arrive in the queue, with no error at the publisher. The queue's access policy must allow sqs:SendMessage from the SNS topic (usually with an aws:SourceArn condition naming the topic). If the queue is encrypted with a KMS key, that key's policy must also let SNS use it, which means a customer managed key. Check both before you debug anything else.
Where AI fits
Fan-out maps neatly onto AI pipelines. A user uploads a document; your app publishes one message. Three queues receive it: one worker summarises it, one translates it, one classifies it, each calling its own model in parallel. If the translation model is slow or rate-limited, that queue absorbs the backlog while summaries and labels finish on time, and each job retries independently. When you add a fourth task later, say extracting entities, you subscribe one more queue without touching the others.
Quick recap
- Publish once to an SNS topic; one SQS queue per job gets its own copy.
- Queues give each job its own pace, retries and failure isolation.
- Use filter policies to send each queue only what it needs, and make sure the queue policy (and KMS key) let the topic in.