Xoxoftware - XOXO Creative Studio | Web & Mobile App Development | Fred Cheung | Hong Kong
AWSMessaging

Amazon SQS and SNS

Managed messaging — SQS for decoupled queue-based communication and SNS for pub/sub fan-out to multiple subscribers.

Overview

Amazon Simple Queue Service (SQS) is a fully managed message queue for decoupling producers and consumers, while Amazon Simple Notification Service (SNS) is a managed pub/sub service that fans out messages to multiple subscribers simultaneously.

FeatureSQSSNS
PatternQueue (point-to-point)Pub/Sub (one-to-many)
Consumer modelConsumers poll the queueSubscribers receive push notifications
DeliveryAt-least-once (Standard) / Exactly-once (FIFO)At-least-once
PersistenceMessages stored until consumed or expiredNo persistence — immediate delivery or drop
ThroughputNearly unlimited (Standard) / 3,000 msg/s with batching (FIFO)Nearly unlimited publishes
OrderingBest-effort (Standard) / Strict FIFOFIFO via FIFO SNS topic → FIFO SQS only

Core Concepts

ConceptDescription
Standard QueueNearly unlimited throughput, best-effort ordering, at-least-once delivery
FIFO QueueStrict message ordering and exactly-once processing; up to 3,000 msg/s with batching
Dead-Letter Queue (DLQ)Destination for messages that fail processing after a configured number of attempts
Visibility TimeoutPeriod during which a received message is hidden from other consumers (default 30 s)
Long PollingReduces empty responses by waiting up to 20 s for a message to arrive
Message Group IDFIFO concept — messages with the same group ID are processed in order
Deduplication IDFIFO concept — prevents duplicate messages within a 5-minute deduplication window
SNS TopicA logical channel to which publishers send messages
SNS SubscriptionAn endpoint (SQS, Lambda, HTTP, email, SMS) that receives copies of topic messages
Filter PolicyJSON policy on a subscription that filters which messages are delivered
Fan-OutPattern where one SNS topic pushes to multiple SQS queues for parallel processing

How SQS Works

Producer → SQS Queue → Consumer (poll) → Process → Delete Message
                ↓ (failure)
         Dead-Letter Queue
  • Retention: 1 minute to 14 days (default 4 days)
  • Max message size: 256 KB (use Extended Client Library + S3 for larger payloads)
  • Delay queue: Postpone delivery of new messages by 0–15 minutes
  • In-flight messages: up to 120,000 (Standard) / 20,000 (FIFO)

How SNS Works

Publisher → SNS Topic
              ├── SQS Queue A (filter: order events)
              ├── SQS Queue B (filter: payment events)
              ├── Lambda Function
              ├── HTTP/S Endpoint
              └── Email / SMS
  • Message filtering: Apply JSON filter policy per subscription to reduce downstream noise
  • Raw message delivery: Skip SNS JSON wrapping for SQS/HTTP subscribers
  • Message attributes: Key-value metadata attached to each published message
  • FIFO topics: Pair with FIFO SQS queues for ordered, deduplicated fan-out

Fan-Out Pattern (SNS + SQS)

Order Service
    → SNS Topic (OrderPlaced)
        ├── SQS Queue → Inventory Service
        ├── SQS Queue → Billing Service
        └── SQS Queue → Analytics Pipeline

Each SQS queue receives an independent copy of the message and processes it at its own pace — failure in one consumer does not affect others.


SQS vs SNS Decision Guide

CriteriaChoose SQSChoose SNS
One consumer per messageYesNo — use SNS + SQS fan-out
Multiple consumersOne queue per consumer (no fan-out)Yes — single publish, multiple receive
Need message persistenceYes — up to 14 daysNo — deliver or lose
Pull-based processingYes — consumer polls at own paceNo — push-based
Need ordering guaranteesFIFO queueFIFO topic → FIFO queue
Event notification (email/SMS)Not directlyYes — built-in email, SMS, HTTP

Common Use Cases

  • Application decoupling — SQS queue between microservices to absorb load spikes and prevent cascading failures.
  • Fan-out processing — SNS topic publishes an event; multiple SQS queues process it independently (e.g., order → billing + inventory + analytics).
  • Batch job buffering — Producers write to SQS; a fleet of workers polls and processes at a controlled rate.
  • S3 event notifications — S3 publishes object-created events to SNS topic, fanning out to Lambda, SQS, and email subscribers.
  • Cross-account messaging — SNS topic in one account fans out to SQS queues in other accounts via resource policies.

SAA/SAP Exam Tips

SAA Tip: "Decouple components" + "handle traffic spikes" → SQS Standard Queue. "Decouple" + "strict ordering" → SQS FIFO Queue.

SAA Tip: "Fan-out to multiple destinations from a single event" → SNS + SQS. This is the classic fan-out pattern tested heavily on the exam.

Exam Trap: SQS messages are not pushed — consumers must poll. If the question says "push-based" delivery, the answer is SNS (or EventBridge), not SQS.

SAP Tip: FIFO SQS throughput is 300 msg/s without batching, 3,000 msg/s with batching. If a scenario requires higher throughput with ordering, consider Kinesis Data Streams instead.


Cross-Cloud Equivalents

ProviderService / SolutionNotes
AWSAmazon SQS / Amazon SNSBaseline
AzureAzure Queue Storage (queue) / Azure Service Bus (advanced) / Azure Event Grid (pub/sub)Service Bus supports FIFO + sessions
GCPGoogle Cloud Pub/SubUnified queue + pub/sub in one service
On-PremisesRabbitMQ, Apache Kafka, ActiveMQSelf-managed message brokers

Pricing Model

DimensionUnitNotes
SQS requestsPer million requestsFirst 1 M requests/month free; Standard cheaper than FIFO
SQS data transferPer GB outIntra-region SQS → EC2 is free
SNS publishesPer million publishesFirst 1 M publishes/month free
SNS deliveriesPer delivery typeSQS/Lambda free; HTTP tiered; SMS per-message charge

Built by Fred Cheung @CookedRicer · Powered by Fumadocs & Github Copilot

On this page