Sub topic 18
Choosing between SQS, SNS, and combinations
This chapter assumes you know how SQS queues hold messages until a consumer deletes them. It also assumes you know how SNS topics push a published message to every subscriber, and that you have read the earlier chapters on fan-out, filter policies, and dead-letter queues. Here you turn that knowledge into a decision.
The core difference
SQS is a buffer that consumers pull from. SNS is a broadcaster that pushes to subscribers. A queue holds each message until one consumer processes and deletes it. A topic holds nothing. If a subscriber is unreachable, SNS retries according to its delivery policy and then drops the message, unless the subscription has a dead-letter queue.
| Question | SQS | SNS |
|---|---|---|
| Delivery model | Consumers pull | Topic pushes |
| Recipients per message | One consumer processes it | Every matching subscriber gets a copy |
| Storage | Retained up to 14 days | None |
| Slow or offline consumer | Messages wait in the queue | Retries, then loss or a subscription DLQ |
| Typical use | Work distribution, load leveling | Event notification, broadcasting |
Decision criteria
Ask these questions in order:
- Does the work need to survive a consumer outage or a traffic spike? If so, put a queue in front of the consumer.
- Do several independent systems need the same event? If so, publish to a topic.
- Does each of those systems work at its own pace or fail on its own? If so, give each one its own queue.
- Do you need ordering or deduplication? If so, use FIFO, and remember that an SNS FIFO topic can only deliver to SQS FIFO queues.
- Is the consumer a person or an external endpoint, such as email, SMS, or an HTTP webhook? Use SNS directly.
Common combinations
- SQS alone. One producer, one pool of workers. The queue absorbs bursts, and you scale workers on queue depth.
- SNS alone. Alerts to email or SMS, or a push to an HTTP endpoint. You do not need buffering or replay.
- SNS to SQS fan-out. Each consumer gets its own queue and its own dead-letter queue. A filter policy keeps each queue to the events it cares about. A slow consumer does not affect the others.
- SQS to Lambda. The queue buffers, and Lambda scales with it. Return
batchItemFailuresso one bad message does not force the whole batch to retry.
A worked decision
An order service must tell billing, shipping, and an analytics job about every new order. Billing and shipping must not lose orders when they are down. Analytics only needs order_created events.
Publish each event to one order-events topic. Subscribe three queues. Give billing and shipping their own queues with dead-letter queues, and give analytics a queue with a filter on eventType. Adding a fourth consumer later means adding a subscription. The order service does not change.
Fan-out also has costs: more resources to manage, a queue access policy per queue, and at-least-once delivery at every hop. Each consumer must still be idempotent.
Exercise
A payment service must send each receipt to an email address and also hand it to an invoicing system that is sometimes offline for hours and must not lose any receipt. Which services do you use, and why?
Show answer
Publish receipts to an SNS topic. Subscribe the email address directly, since email needs no buffering. Subscribe an SQS queue for the invoicing system, so receipts wait in the queue while it is offline. Add a dead-letter queue to that queue for messages that keep failing.
Exercise
Why might you give each consumer of an SNS topic its own queue instead of sharing one queue between them?
Show answer
Consumers sharing a queue would compete for messages, so each message would reach only one of them. Separate queues give every consumer its own copy, its own pace, its own retry and dead-letter handling, and its own filter policy.