Sub topic 21
Choosing between SQS, SNS, and combinations
This chapter assumes you have read the earlier chapters. You should know how an SQS queue stores messages, how the visibility timeout and dead-letter queues work, how an SNS topic publishes to subscribers, and how an SNS-to-SQS fan-out is wired together. Here you use that knowledge to decide which service, or which combination, fits a workload.
The core difference
SQS and SNS solve different problems, and the clearest way to tell them apart is to ask who controls delivery.
- SQS is pull-based. Messages wait in a queue until a consumer asks for them. The consumer decides when to read and how fast. Each message is normally processed by one consumer, and the queue stores it for up to the retention period (4 days by default, up to 14).
- SNS is push-based. A publisher sends one message to a topic, and SNS immediately pushes a copy to every subscriber. SNS does not hold messages for subscribers that are slow or offline. It retries delivery according to the subscription's retry policy and then gives up, or sends the message to a subscription dead-letter queue if you configured one.
| Question | SQS | SNS |
|---|---|---|
| Who moves the message to the consumer? | The consumer polls | SNS pushes |
| How many consumers get each message? | One (competing consumers) | All matching subscribers |
| Does it buffer while consumers are down? | Yes, up to the retention period | No, only retries for a limited time |
| Can consumers control their own pace? | Yes | No, the subscriber must keep up |
| Typical use | Work queue, load leveling | Event broadcast, notifications |
Decision criteria
Work through these questions in order. Each answer narrows the choice.
- Does more than one system need the same event? If yes, you need SNS (or another broadcast mechanism) in front. A single queue cannot give every consumer its own copy, because consumers on one queue compete for messages.
- Does a consumer need to work at its own pace, or survive downtime? If yes, that consumer needs a queue. SNS alone will push to a slow endpoint and may lose the message after retries are exhausted.
- Is the receiver a person or an external endpoint? Email, SMS, mobile push, and HTTP/S webhooks are SNS protocols. SQS has no way to notify a person.
- Do you need ordering or exactly-once-style behavior? Use a FIFO queue, or a FIFO topic with FIFO queues. Remember the throughput and naming constraints from the FIFO chapter.
- Do different consumers care about different events? Use filter policies on the subscriptions so each queue receives only what it needs.
Pattern 1: SQS alone
Use a queue by itself when one logical consumer group processes the work and the producer does not need to know who that is.
Good fits:
- Background jobs such as image resizing, report generation, or email sending, where a web request drops a job and returns.
- Load leveling. A burst of traffic lands in the queue, and workers drain it at a steady rate that the database can handle.
- Work distribution across many identical workers. Adding workers increases throughput without changing the producer.
Watch for: a second team that wants the same messages. If they attach a second consumer to the same queue, the two will split the messages between them. That is the usual sign that you need SNS in front of the queue.
Pattern 2: SNS alone
Use a topic by itself when the subscribers are endpoints that can accept a push and where losing a message after retries is acceptable or handled elsewhere.
Good fits:
- Operational alerts to email or SMS, for example a CloudWatch alarm that notifies an on-call group.
- Mobile push notifications.
- Triggering a Lambda function directly from an event where the function is fast, idempotent, and you accept SNS retry semantics.
Watch for: no buffer. If an HTTP endpoint is down longer than the retry policy allows, the message is dropped unless you configured a subscription dead-letter queue. A subscription dead-letter queue is itself an SQS queue, so even here SQS ends up supporting SNS.
Pattern 3: SNS to SQS fan-out
Combine them when one event must reach several independent consumers and each needs durability and its own pace. This is the pattern from the fan-out chapter.
What each piece contributes:
- The topic gives the producer one place to publish. Adding a new consumer means adding a subscription, with no change to the producer.
- Each queue isolates its consumer. If the analytics consumer is down for six hours, its queue grows while billing and shipping continue normally.
- Each queue has its own visibility timeout, retention, and dead-letter queue, tuned to that consumer.
- Filter policies keep each queue from receiving events it would discard.
The cost is more resources to manage: one queue, one queue policy, and one subscription per consumer. Use infrastructure as code so that adding a consumer is a small, reviewable change.
Pattern 4: SQS with Lambda
A Lambda function with an SQS event source mapping gives you a managed consumer. Lambda polls the queue, invokes your function with a batch, and deletes the messages on success. Combine it with the earlier patterns by making the queue the Lambda trigger.
Prefer this over subscribing Lambda directly to SNS when:
- The function can be slow or fail, and you want the queue to absorb retries and use a dead-letter queue.
- You need to limit concurrency to protect a downstream system. The event source mapping supports a maximum concurrency setting, while a direct SNS invocation does not give you that control.
- You want batching, so one invocation handles several messages.
Prefer direct SNS-to-Lambda when the work is short, idempotent, and low volume, and the extra queue adds no value.
Comparing the combinations
| Need | Recommended design |
|---|---|
| One consumer group, background work | SQS |
| Alert a person by email or SMS | SNS |
| Several systems need every event, each at its own pace | SNS topic with one SQS queue per consumer |
| Several systems need different subsets of events | SNS fan-out with filter policies |
| Strict ordering per customer, multiple consumers | FIFO topic with FIFO queues, using message group IDs |
| Protect a database from traffic bursts | SQS with a Lambda or worker consumer and limited concurrency |
| Notify an external webhook reliably | SNS with HTTP/S subscription and a subscription dead-letter queue |
Common mistakes
- Using SNS where a queue was needed. Symptoms: lost messages during consumer outages, or a slow endpoint causing retries to exhaust. Put a queue between the topic and the consumer.
- Putting several unrelated consumers on one queue. They split the messages instead of each receiving all of them. Give each consumer its own queue and fan out from a topic.
- Choosing FIFO by default. FIFO adds ordering and deduplication, but it has lower throughput limits than standard queues and requires message group IDs. Use it only when the ordering or deduplication is a real requirement.
- Skipping the dead-letter queue. Without one, a poison message is retried until retention expires and hides real failures. Configure a redrive policy and alarm on the dead-letter queue depth.
- Forgetting idempotency. Standard queues and SNS both deliver at least once. Every consumer must tolerate duplicates regardless of which pattern you choose.
- Ignoring cost shape. Each publish, each delivery, and each queue request is billed. Long polling, batching, and filter policies reduce the number of requests and unneeded deliveries.
A quick worked decision
Suppose an online store publishes an event whenever an order is placed. Billing must charge the card, shipping must create a label only for new orders, analytics wants every event, and an operations group wants an email if an order is flagged as fraudulent.
- Several systems need the event, so use an SNS topic.
- Billing, shipping, and analytics are services that must not lose events and run at different speeds, so give each an SQS queue subscribed to the topic.
- Shipping needs only new orders, so add a filter policy on
eventTypeto its subscription. - Billing must process each customer's events in order, so make the topic and billing queue FIFO and use the customer ID as the message group ID. Shipping and analytics tolerate reordering, but a FIFO topic can only deliver to FIFO queues and SQS standard queues. Choose per queue, based on what each needs.
- The operations email is a person, so add an email subscription filtered to
eventType = order_flagged. - Every queue gets a dead-letter queue and a CloudWatch alarm on message age and dead-letter queue depth.
Exercise
A team has one SQS queue that two services read from. Each service reports that it sees only about half of the orders. What is the cause, and what is the minimal fix?
Show answer
Consumers on one queue compete for messages, so each message goes to only one of them. The fix is to publish orders to an SNS topic and subscribe a separate SQS queue for each service, so each service gets its own copy of every message. Add the queue access policy that lets the topic send to each queue.
Exercise
You need to send an email to an on-call group when a payment fails, and you also need a payment-retry worker to process the same failure at its own pace. Which services do you use, and why?
Show answer
Use an SNS topic with two subscriptions. An email subscription notifies the on-call group, because SQS cannot send email. An SQS queue subscription holds the failure events for the retry worker, because the queue buffers messages and lets the worker consume at its own pace and retry safely with a dead-letter queue.
Exercise
A Lambda function subscribed directly to an SNS topic calls a database that sometimes becomes slow. Under load the function errors and some events are lost. How would you redesign this?
Show answer
Subscribe an SQS queue to the topic and make the queue the Lambda event source. The queue buffers events when the database is slow, a maximum concurrency setting limits how many invocations hit the database, partial batch failure reporting retries only the failed messages, and a dead-letter queue captures events that keep failing.