Sub topic 13
The fan-out pattern: one event, many queues
This chapter assumes you can create SQS queues and SNS topics, know what a visibility timeout and a dead-letter queue are, and have seen the queue access policy for SNS. The examples use Python with boto3.
The problem fan-out solves
An order is created. Billing, shipping, and analytics all need to react. If the order service calls each of them, it must know all three, and one slow or failing consumer holds up the others.
In the fan-out pattern, the producer publishes one event to an SNS topic. Each interested system has its own SQS queue subscribed to that topic. SNS copies the event into every subscribed queue, and each consumer reads from its own queue at its own pace.
Why use queues behind the topic
You could subscribe each consumer directly to the topic, but a queue between them adds three things:
- Buffering. If a consumer is down or slow, messages wait in its queue, up to the retention period.
- Independent retries. Each queue has its own visibility timeout and dead-letter queue, so a poison message in billing does not affect shipping.
- Independent scaling. Each consumer group scales on its own queue depth.
Building it
The SNS topic needs permission to write to each queue. The queue policy must name the topic as the allowed source. Without it, the subscription is created but no messages arrive.
import json
import boto3
sqs = boto3.client("sqs")
sns = boto3.client("sns")
topic_arn = sns.create_topic(Name="order-events")["TopicArn"]
for name in ["billing", "shipping", "analytics"]:
queue_url = sqs.create_queue(QueueName=name)["QueueUrl"]
queue_arn = sqs.get_queue_attributes(
QueueUrl=queue_url, AttributeNames=["QueueArn"]
)["Attributes"]["QueueArn"]
policy = {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"Service": "sns.amazonaws.com"},
"Action": "sqs:SendMessage",
"Resource": queue_arn,
"Condition": {"ArnEquals": {"aws:SourceArn": topic_arn}},
}],
}
sqs.set_queue_attributes(
QueueUrl=queue_url, Attributes={"Policy": json.dumps(policy)}
)
sns.subscribe(
TopicArn=topic_arn,
Protocol="sqs",
Endpoint=queue_arn,
Attributes={"RawMessageDelivery": "true"},
)
sns.publish(TopicArn=topic_arn, Message=json.dumps({"orderId": "1001"}))After the publish, each of the three queues holds one copy of the message. Each consumer deletes only its own copy.
Common errors
- Messages never arrive. The queue policy is missing, or its
aws:SourceArndoes not match the topic ARN. Check this first. - Consumer fails to parse the body. Without raw message delivery, the body is an SNS JSON envelope, and your payload is inside its
Messagefield as a string. - Duplicates. Standard topics and queues deliver at least once, so consumers must be idempotent.
- FIFO mismatch. A FIFO topic can deliver only to FIFO queues. A standard topic can deliver to standard queues, and also to FIFO queues in some configurations, but you lose the ordering guarantee.
- Topic and queue in different regions or accounts. This works only with extra permissions and care. Start in one region and one account.
Exercise
A fan-out has three subscribed queues, and the shipping queue receives nothing while billing and analytics work. The subscription shows as confirmed. What do you check first?
Show answer
Check the shipping queue's access policy. It must allow sqs:SendMessage from sns.amazonaws.com with an aws:SourceArn condition matching the topic's ARN. A missing or mismatched policy on that one queue blocks delivery to it only. Also check that a filter policy on that subscription is not excluding every message.