Resilient architectures · 11 min
Fan-out with Amazon SNS and events with Amazon EventBridge
In this lesson you will learn when to use an Amazon SQS queue, an Amazon SNS topic or an Amazon EventBridge rule, and how to build the fan-out from an SNS topic to several SQS queues with filter policies.
Amazon SNS and Amazon EventBridge are two AWS services that deliver the same fact to several parts of an application, something an Amazon SQS queue doesn't do. A queue is a waiting list where one part of the application leaves tasks and another part picks them up when it can. When three microservices read from the same SQS queue, each order is processed by only one of the three, because the queue distributes the work. For example, if an online store puts its orders in a queue and three programs read from it, each order is taken by the first one that is free, and the other two never see it.
The opposite case is a fact that matters to several teams. For example, when an order is paid, billing has to issue the invoice, shipping has to prepare the package and analytics wants to count it. If the store calls all three, it ends up coupled to each one again, that is, it depends on all three answering, and if one goes down, the order fails. This is what publish and subscribe (pub/sub) is for, where the store publishes the fact only once and each interested party receives its copy.
What is fan-out with Amazon SNS?
Amazon Simple Notification Service (Amazon SNS) delivers messages from publishers to subscribers through topics. A topic is a named channel, such as "orders": the publisher is whoever sends messages to the channel and the subscribers are the ones who signed up to receive them. The publisher sends a message to the topic, and SNS pushes it to all the subscribers without them having to ask. These subscribers can be SQS queues, Lambda functions, HTTP endpoints, email, SMS, mobile push notifications or Amazon Data Firehose. For example, the store publishes "order paid" once to the topic, and SNS sends an email to the customer and a message to the shipping queue at the same time.
SQS works the other way around, because the consumer pulls from the queue when it can and in the meantime the message waits there. The SNS documentation sums up the difference: SNS sends time-critical messages to many subscribers, and SQS decouples the sender from the receiver without both having to be available at the same time. Decoupling means separating two parts so that one doesn't depend on the other working at that moment. When they are used together, with an SNS topic and several SQS queues subscribed to it, the design is called fan-out.

In a fan-out, the store publishes each event only once to a topic, such as "orders", and each queue receives its copy and processes it at its own pace. That is why, if shipping is down for an hour, its messages wait in its queue while billing keeps working as usual. For example, when it comes back, the shipping program finds all the orders from that hour in its queue and prepares them one by one.
Filter policies
By default, a subscriber receives all the messages of the topic, but with a filter policy on the subscription it receives only the ones that meet a condition. For example, if the billing policy only accepts paid and the shipping policy accepts paid and shipped, a canceled order reaches nobody. These policies read the message attributes, which are labels with a name and a value that travel with it, such as event = paid. If you change the policy's scope, they read the message body in JSON instead.
Which queue does it reach?
The Billing subscription has a filter policy that only accepts paid, and the Shipping subscription one that accepts paid and shipped. The store publishes a message with event = shipped. Which queue receives it?
How does Amazon EventBridge work?
Amazon EventBridge also connects components with events, but with a different model. An event is the notice that something happened, such as "an order was paid" or "a computer was shut down". In EventBridge, an event bus, the channel events travel through, receives events from AWS services, from your applications and from SaaS providers. In the rules and targets model, which AWS now calls Custom Event Bus - Classic, each rule has an event pattern, which is a JSON document that says which fields and values the event has to contain. If the event matches the pattern, EventBridge sends it to the rule's targets, which can be up to five per rule.

For example, a rule can let through only the aws.ec2 events whose state is stopped. Because your account's default bus receives the events from AWS services, reacting when an instance stops doesn't require writing any code on the EC2 side. That way, if someone shuts down one of the store's computers, a rule can send an alert to the team without anyone having written code for it on the computer.
EventBridge Scheduler and EventBridge Pipes
EventBridge has two other features, which are meant for cases different from the bus:
- EventBridge Scheduler, which schedules tasks with cron or rate (every so often) expressions, or for a single time. For example, every night the store runs a task that deletes abandoned carts.
- EventBridge Pipes, which connects a source with a target, point to point, with filtering and enrichment in between, which means adding data to the message before delivering it. For example, Pipes takes the orders from a queue, lets through only the large ones and sends them to a single target.
The new Custom Event Bus
AWS added a new type of bus, called Custom Event Bus, where you create subscribers and the bus stores the events for as long as you choose and can send them again (replay). AWS recommends starting new applications with this bus. However, the model you need to understand for the exam is the rules and targets one. For example, with the new bus, if the store's analytics program had a bug yesterday, the bus can send it that day's events again.
True or false?
0 of 3In an SNS topic without filter policies, each subscriber receives all the messages.
An EventBridge rule can send an event to an unlimited number of targets.
With fan-out from SNS to SQS, if a consumer is down its messages are lost.
When to use SQS, SNS or EventBridge?

The three services decouple components, but each one delivers messages in a different way:
- Amazon SQS, which is a work queue where a consumer pulls the message, processes it and deletes it, and which absorbs spikes, the moments when a lot of tasks arrive at once. For example, the store puts the photos that sellers upload in a queue and a program shrinks them one by one.
- Amazon SNS, which is a topic that pushes each message to many subscribers instantly, and which the reliability pillar describes as push, high-throughput, many-to-many messaging. For example, the store tells billing, shipping and analytics about each payment at the same time.
- Amazon EventBridge, which is a bus with rules that filter by any field of the event, and which receives events from AWS services and from SaaS without you writing code on the source side. For example, the store reacts when its external payments system reports that a payment was declined.
Match each requirement with the service
0 of 4 pairsTap a requirement and then the service that solves it.
0 of 4 pairs.
On the exam
Questions on this topic usually describe several systems that depend on the same fact. Phrases such as "several systems have to react to the same event" or "process in parallel independently" point to fan-out, with an SNS topic and one SQS queue per consumer, and if in addition "each consumer only wants some messages", the answer adds filter policies. On the other hand, "react to a change in an AWS service" or "events from a SaaS application" point to EventBridge. For example, if the question says that billing and shipping have to receive every order even if one of them goes down, the answer is an SNS topic with a queue for each one.
cloud path tip: if the question asks to "run something every hour" or "at a fixed time", the answer is EventBridge Scheduler.
Review question
When a customer places an order, three microservices (inventory, billing and notifications) have to process it. Each one works at its own pace and none of them should lose orders if it goes down for a few minutes. Which architecture meets this with the least coupling?
Full account and new experience account
Amazon SNS, Amazon SQS and Amazon EventBridge are in the free plan of the new experience account, the simplified way to open an AWS account, within the project's Region (us-east-2 for Latin America). What you can't do in that account is send events to another Region, because there no service supports cross-Region features. For example, you can build the store's whole fan-out in us-east-2, but you can't send its events to a bus in São Paulo.
Next
- Fanout Amazon SNS notifications to Amazon SQS queues, the page in the Amazon SNS developer guide.
- What is Amazon Simple Queue Service?, the resource for this step, which at the end compares SQS, SNS and Amazon MQ.
- Take a Solutions Architect practice exam at /examenes.