Skip to content
Back to Cloud Services

AWS SQS & SNS Messaging

SQS queues for decoupling and SNS pub/sub for fan-out — at-least-once delivery, scales to infinite.

AWSIntermediate

Why

SQS/SNS decouple services and absorb traffic spikes — 1M+ messages/sec, pay per request. SQS FIFO guarantees ordering, SNS fan-out to many queues.

Free tier: 1M requests/mo.

When

Use SQS for task queues, SNS for event broadcast, and SNS→SQS fan-out for parallel processing. Avoid for streaming (use Kinesis) or needing exactly-once (use Lambda idempotency).

How

1. Create queue — aws sqs create-queue --queue-name my-queue 2. Send — aws sqs send-message --queue-url URL --message-body '{"job":"x"}' 3. Receive — aws sqs receive-message, process, delete on success 4. Visibility timeout — message hidden during processing, reappears if not deleted 5. DLQ — dead-letter-queue config, failed messages move after maxReceiveCount 6. SNS topic — aws sns create-topic, subscribe SQS/Lambda/HTTP endpoints 7. Fan-out — SNS → multiple SQS subscriptions, each gets copy 8. FIFO — .fifo suffix, content-based dedup, group ID for ordering

Example

# SNS → SQS fan-out (one event, multiple processors) aws sns create-topic --name order-events aws sqs create-queue --queue-name inventory-queue aws sqs create-queue --queue-name email-queue # Subscribe both queues to topic aws sns subscribe --topic-arn arn:aws:sns:...:order-events \ --protocol sqs --notification-endpoint arn:aws:sqs:...:inventory-queue // Lambda consuming SQS with batch export const handler = async (event: { Records: SQSRecord[] }) => { for (const record of event.Records) { const body = JSON.parse(record.body); await processOrder(body); } }; # DLQ config — move to dlq after 5 failed receives aws sqs set-queue-attributes --queue-url URL --attributes \ RedrivePolicy='{"deadLetterTargetArn":"arn:aws:sqs:...:dlq","maxReceiveCount":"5"}'

Common Problems

  • ⚠Poison messages without DLQ — block queue forever, always configure DLQ
  • ⚠FIFO throughput limit — 300 msgs/sec without batching, 3000 with
  • ⚠SNS → SQS JSON wrapper — message wrapped in SNS envelope, parse body.Message

Best Practices

  • ✓Always set DLQ — prevents poison messages from blocking the queue
  • ✓Use SQS batch (10 msgs) for Lambda — reduces invocations 10x, lower cost
  • ✓Idempotent consumers — SQS is at-least-once, dedupe by message ID

FAQ

What is AWS SQS & SNS Messaging and what problem does it solve?

SQS queues for decoupling and SNS pub/sub for fan-out — at-least-once delivery, scales to infinite. It lets developers deploy, store, and scale without self-managing infrastructure — a core reference for cloud service selection.

How do I get started with AWS SQS & SNS Messaging in 5 minutes?

Two steps to run it: (1) Create queue — aws sqs create-queue --queue-name my-queue; (2) Send — aws sqs send-message --queue-url URL --message-body '{"job":"x"}'. Validate on the free tier first, then wire into production.

What is the #1 pitfall with AWS SQS & SNS Messaging?

The most frequent issue is: Poison messages without DLQ — block queue forever, always configure DLQ. Run a pre-launch checklist item by item, and add policy/config scanning in CI.

What are the best practices for AWS SQS & SNS Messaging?

Best practices for AWS SQS & SNS Messaging include: Always set DLQ — prevents poison messages from blocking the queue; Use SQS batch (10 msgs) for Lambda — reduces invocations 10x, lower cost; Idempotent consumers — SQS is at-least-once, dedupe by message ID. The first one — "Always set DLQ — prevents poison messages from blocking the queue" — should be a team default baked into your IaC templates.

How do I control cost and avoid bill surprises with AWS SQS & SNS Messaging?

Three ways to prevent overspend: (1) tag every resource with env/team and enable billing alarms; (2) set budget thresholds to trigger Slack/email alerts; (3) review idle resources in Cost Explorer regularly and clean them up. Most AWS SQS & SNS Messaging options offer a free tier — validate within free quota before scaling.