What is the difference between SQS and SNS?
Understand SQS vs SNS: pull-based queues vs push-based pub/sub, when to use each, and the SNS-to-SQS fan-out pattern for reliable event-driven AWS apps.
Expected Interview Answer
Amazon SQS is a pull-based message queue where one consumer group processes each message exactly once, while Amazon SNS is a push-based pub/sub service that fans a single message out to many subscribers simultaneously.
SQS decouples producers from consumers by storing messages durably until a worker polls and deletes them, making it ideal for load-levelling and reliable one-to-one work distribution. SNS instead pushes each published message to every subscribed endpoint (Lambda, HTTP, email, or SQS queues) at once, giving one-to-many broadcast. A common pattern is the fan-out design: SNS publishes to multiple SQS queues so each downstream system gets its own durable copy to process independently.
- SQS decouples and buffers workloads so spikes do not overwhelm consumers
- SQS guarantees each message is processed by exactly one worker
- SNS broadcasts one message to many subscribers instantly
- Combining SNS + SQS gives durable fan-out to independent consumers
- Both are fully managed and scale automatically
AI Mentor Explanation
SQS is like the queue of batters padded up in the pavilion — each new batter walks out one at a time and only one occupies the crease, so every delivery is faced by exactly one person. SNS is like the stadium PA announcing a wicket: the single announcement reaches every spectator, commentator, and scoreboard at once, all reacting in parallel to the same broadcast.
Step-by-Step Explanation
Step 1
Identify the delivery model
Choose SQS when work must be processed once by a single consumer; choose SNS when the same message must reach many subscribers.
Step 2
Create the SNS topic
Producers publish messages to a topic rather than addressing consumers directly.
Step 3
Add subscribers
Subscribe endpoints such as Lambda, HTTP/S, email, or SQS queues to the topic for fan-out.
Step 4
Buffer with SQS
Subscribe SQS queues to the topic so each downstream system gets a durable, independently processed copy.
Step 5
Poll and delete
SQS consumers long-poll for messages, process them, then delete to prevent reprocessing after the visibility timeout.
What Interviewer Expects
- Clear pull (SQS) vs push (SNS) distinction
- Understanding of pub/sub fan-out
- Knowledge of the SNS-to-SQS fan-out pattern
- Awareness of exactly-once vs one-to-many delivery
- When to combine both services
Common Mistakes
- Saying SNS stores messages durably like a queue
- Claiming SQS can broadcast to multiple consumers of one queue
- Confusing SNS topics with SQS queues
- Ignoring the fan-out pattern that pairs both services
- Forgetting SQS visibility timeout and message deletion
Best Answer (HR Friendly)
“SQS is a waiting line where each message is handled by exactly one worker, which is great for spreading out work reliably. SNS is a loudspeaker that sends one message to many listeners at once, and the two are often combined so a single event reaches several systems, each with its own copy.”
Code Example
# Create a topic and two queues
TOPIC_ARN=$(aws sns create-topic --name order-events --query TopicArn --output text)
Q1_URL=$(aws sqs create-queue --queue-name billing-queue --query QueueUrl --output text)
Q2_URL=$(aws sqs create-queue --queue-name analytics-queue --query QueueUrl --output text)
# Get each queue ARN and subscribe it to the topic
Q1_ARN=$(aws sqs get-queue-attributes --queue-url "$Q1_URL" --attribute-names QueueArn --query Attributes.QueueArn --output text)
aws sns subscribe --topic-arn "$TOPIC_ARN" --protocol sqs --notification-endpoint "$Q1_ARN"
# Publish once; every subscribed queue receives its own copy
aws sns publish --topic-arn "$TOPIC_ARN" --message '{"orderId":"1234","total":49.99}'
# A consumer pulls and deletes from its queue
aws sqs receive-message --queue-url "$Q1_URL" --wait-time-seconds 20Follow-up Questions
- How does the SQS visibility timeout prevent duplicate processing?
- What is the difference between standard and FIFO queues?
- How would you implement fan-out so three services each process every event?
- When would you use an SNS dead-letter queue?
- How does long polling reduce cost and empty responses in SQS?
MCQ Practice
1. Which statement best describes SQS?
SQS is a pull-based queue; consumers poll for messages and each message is processed by a single consumer before deletion.
2. In an SNS-to-SQS fan-out, what does each subscribed queue receive?
Fan-out delivers an independent copy of each published message to every subscribed SQS queue, so downstream systems process them separately.
3. Which delivery model does SNS use?
SNS pushes each published message to all subscribers at once, following a one-to-many pub/sub model.
Flash Cards
SQS delivery model — Pull-based queue; each message processed by exactly one consumer, then deleted.
SNS delivery model — Push-based pub/sub; one message fanned out to many subscribers at once.
Fan-out pattern — SNS publishes to multiple SQS queues so each system gets its own durable copy.
When to combine SNS + SQS — When one event must reach several independent consumers, each buffered and processed reliably.