Lesson 2 of 3 Amazon SQS, SNS and EventBridge

Fanning Out Messages with Amazon SNS

This lesson explains how Amazon SNS, a pub/sub service, pushes messages to many subscribers at once, and how it combines with SQS in the fan-out pattern.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe topics and subscription types.
  • Apply the SNS-to-SQS fan-out pattern.
  • Distinguish SNS from SQS.

Topics and subscriptions

Amazon SNS delivers each message published to a topic to every subscription on it. Subscribers can be SQS queues, Lambda functions, HTTP or HTTPS endpoints, email addresses, SMS numbers, mobile push endpoints and Amazon Data Firehose streams. Filter policies on message attributes or the message body send each subscriber only the messages it wants.

FIFO topics deliver ordered, deduplicated messages to SQS FIFO queues. SNS retries failed deliveries, each subscription can have a dead-letter queue for messages that cannot be delivered, topics can be encrypted with AWS KMS, and topic policies control who can publish and subscribe.

The fan-out pattern

Several SQS queues subscribe to one topic, so independent services each get a durable copy and process it at their own pace, e.g. an order event reaching fulfilment, billing and analytics queues. S3 event notifications can target SNS to fan out object events. Queues in other accounts or Regions can subscribe, given topic and queue policies.

The following diagram shows the order example with three subscribed queues.

The fan-out pattern. An order service publishes one event to an SNS topic. Three SQS queues subscribe, so fulfilment, billing and analytics each receive a durable copy and process it at their own pace; a slow or failed consumer does not affect the others.
The queues give each consumer its own durable buffer, which SNS alone, with no storage, does not provide.
True or false

The fan-out pattern

Exam skills 2.1.3 · 2.1.1

Check how the SNS-to-SQS fan-out pattern works.

Log in to try this activity Interactive activities are free with an account.

SNS or SQS

SNS and SQS are often used together but work in opposite ways. SNS is a publish-subscribe service: a publisher sends a message to a topic, and SNS pushes a copy to every subscriber immediately, whether an SQS queue, a Lambda function, an HTTP endpoint, an email address or a phone. SNS does not keep messages, so a subscriber that is unavailable can miss one after delivery retries are exhausted.

SQS is a queue: messages wait until a consumer polls for them, processes them and deletes them, so work is not lost while consumers are down or busy. Subscribing several SQS queues to one SNS topic, the fan-out pattern, delivers each message to several independent consumers, each processing at its own pace. The following table summarises the differences.

SNS
ModelPush to all subscribers
StorageDoes not keep messages
SQS
ModelConsumers poll
StorageKeeps messages until processed

Combining them gives both fan-out and durability.

Worked example

One upload, three jobs

When a video is uploaded to S3, three independent jobs must run: create a thumbnail, transcode it and update a search index. Each must be able to fail and retry without affecting the others.

ComponentRole
S3 event notificationPublishes to an SNS topic
SNS topicFans out a copy to three SQS queues
Three SQS queuesBuffer the work for each job separately
Three consumers (Lambda or containers)Process their own queue at their own pace

An S3 event notification can target only one destination per event type and prefix combination, which is why SNS fan-out (or EventBridge) is used when several consumers need the same event.

Check yourself

How can one SNS topic deliver only some messages to a particular subscriber?

Show answer

With a subscription filter policy that matches message attributes or the message body.

Exam Tips

Remember for the exam

  • When one message must reach several independent consumers, fan out from SNS to SQS queues.
  • When each subscriber should receive only certain messages, use SNS filter policies.
  • To send email or SMS alerts from alarms, use an SNS topic.
  • SNS does not keep messages for later retrieval, so add SQS subscribers for durability.
  • To deliver one event to queues in other accounts, use an SNS topic with cross-account SQS subscriptions.