Lesson 5 of 5 Data Pipeline Orchestration

Notifying and Buffering Pipeline Work with Amazon SNS and Amazon SQS

This lesson explains how Amazon SNS delivers notifications and fans out messages, and how Amazon SQS buffers work durably between producers and consumers, so pipelines report failures and pass work reliably.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish the roles of SNS and SQS.
  • Fan out messages to independent consumers.
  • Configure queues with visibility timeouts and dead-letter queues.
  • Combine CloudWatch alarms and event rules to monitor pipelines.

The two services

Amazon SNS and Amazon SQS are both messaging services but play different roles. SNS is publish and subscribe: a message published to a topic is pushed immediately to every subscriber, such as email, SMS, HTTP endpoints, Lambda functions, SQS queues or chat channels through Amazon Q Developer in chat applications. It keeps no messages, so in pipelines it sends alerts and fans one event out to several consumers.

SQS is a queue: messages wait, for up to 14 days, until a consumer polls and processes them, and each message is handled by one consumer. That makes it a durable buffer that absorbs bursts, lets consumers work at their own pace and retries failed messages. Combining them, an SNS topic delivering to several SQS queues, gives each consumer its own buffered copy. The following table summarises the differences.

Amazon SNS
ModelPublish and subscribe: push to every subscriber
DeliveryEmail, SMS, mobile push, HTTP, Lambda, SQS, chat through Amazon Q Developer in chat applications
RetentionNone; delivered immediately
Pipeline roleAlerts and fan-out
Amazon SQS
ModelQueue: each message processed by one consumer
DeliveryConsumers poll; Lambda event source mappings poll automatically
RetentionUp to 14 days
Pipeline roleDurable buffering, smoothing bursts, retries
Sort into groups

SNS or SQS?

Exam skills 1.3.4

Decide whether each statement describes Amazon SNS or Amazon SQS.

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

Fan-out

Publishing to an SNS topic subscribed by several SQS queues gives each consumer its own copy of every message and its own buffer and retries. Subscription filter policies deliver only matching messages to each queue, for example only events for one region.

Reliable queues

SQS queues must be configured so that failures are handled safely. The visibility timeout, the time a received message stays hidden from other consumers, must be longer than the time needed to process a message; otherwise the message reappears while it is still being processed and is processed twice.

A dead-letter queue with a maximum receive count sets aside messages that fail repeatedly, often called poison messages, so they do not block the queue or retry forever. Where the order of messages matters, or each message must be processed exactly once within a group, FIFO queues provide ordering and deduplication within each message group.

The following diagram shows what happens when processing outlasts the visibility timeout.

Why the visibility timeout must exceed processing time. When processing takes three minutes and the timeout is five, the message is deleted while still hidden. When processing takes eight minutes, the message becomes visible again at five minutes and a second consumer processes it too.
The timings are illustrative.
Troubleshooting

Queue problems

Exam skills 1.3.2 · 1.3.4

Find the fix for each queue problem, choosing again until you find it.

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

A monitoring pattern

Pipelines should be monitored so that failures are detected promptly, and a typical pattern combines two mechanisms. CloudWatch alarms watch pipeline metrics, such as job duration, the number of failed records, Kinesis iterator age and Redshift queue time, and publish to an SNS topic when a service level agreement is at risk of being breached.

Amazon EventBridge rules catch job failures the moment they happen, from services such as Glue, EMR and Step Functions, and publish them to SNS as well. Subscribers to the topic, such as an email list, a chat channel or an incident management tool, then receive both kinds of alert in one place.

The following diagram shows the topic, the three queues and their consumers.

One message, three independent consumers. A dataset-published message goes to an SNS topic subscribed by three SQS queues, each with its own settings, consumer and dead-letter queue. If the search indexer fails, its messages wait in its own queue and then its dead-letter queue, while the other consumers are unaffected.
Each queue has its own dead-letter queue.
Worked example

One event, three consumers

When a curated dataset is published, three systems must react: a data quality job, a search indexer and a partner export. Each must retry independently if it fails. The design publishes one message to an SNS topic subscribed by three SQS queues, configured as follows.

QueueConsumerConfiguration
dq-queueLambda that starts a Glue Data Quality runVisibility timeout 5 minutes; DLQ after 3 receives
index-queueLambda that updates OpenSearchFilter policy: only datasets tagged searchable
export-queuePartner export jobMaximum concurrency 2 to respect the partner's limits

If the search indexer fails, its messages wait in its own queue and then its dead-letter queue, while the other two consumers are unaffected. A CloudWatch alarm on each DLQ's depth notifies the team.

Check yourself

How is a team alerted when a Glue job fails?

Show answer

An EventBridge rule on Glue job state changes routes FAILED events to an SNS topic.

Exam Tips

Remember for the exam

  • To alert when a Glue job fails, route an EventBridge rule on Glue job state to SNS.
  • For durable buffering between steps with retries, use SQS with a dead-letter queue.
  • To send one message to several queues, use SNS fan-out.
  • To raise an alarm when a pipeline runs too long, send a CloudWatch alarm to SNS.
  • Set the SQS visibility timeout longer than the processing time.