SkillSprout
Chapter 9 · Lesson 1
Intermediate
7 min read

SQS

Overview

Amazon Simple Queue Service (SQS) is a fully managed message queue. A producer puts a message on the queue, and a consumer pulls it off later and processes it. The queue sits in the middle and stores messages durably until somebody is ready to handle them.

Think of a busy restaurant kitchen. Waiters (producers) clip order tickets to a rail, and cooks (consumers) take tickets when they have capacity. If a rush hits, tickets pile up on the rail instead of waiters standing at the pass shouting at the cooks. SQS is that rail: it decouples the two sides so each can scale, fail and be deployed independently.

SQS is one of the oldest AWS services and is a favourite on the SAA-C03 exam. Whenever a question says "tightly coupled", "traffic spikes", "lost messages" or "process asynchronously", SQS is probably in the answer.

Key Concepts

  • SQS is a pull-based, regional, serverless queue with virtually unlimited throughput and no capacity to provision.
  • Standard queues give at-least-once delivery and best-effort ordering; FIFO queues give exactly-once processing and strict ordering (per message group).
  • Messages can be 1 byte to 1 MiB (raised from 256 KB in 2025); retention is 60 seconds to 14 days (default 4 days).
  • Visibility timeout hides a received message from other consumers (default 30 s, max 12 hours); an undeleted message reappears.
  • Long polling (WaitTimeSeconds 1-20 s) reduces empty responses and cost compared with short polling.
  • A dead-letter queue (DLQ) collects messages that exceed maxReceiveCount; a FIFO queue needs a FIFO DLQ.
  • Delay queues postpone delivery up to 15 minutes; per-message timers work on standard queues.
  • An Auto Scaling group can scale on queue backlog per instance, and Lambda can poll SQS via event source mapping.

Detailed Notes

Why decouple with a queue?

In a tightly coupled design, a web tier calls a processing tier directly. If processing is slow or down, the web tier fails too. With SQS between them, the web tier simply enqueues work and returns. The worker fleet drains the queue at its own pace, and no message is lost if workers crash because unprocessed messages stay in the queue.

Standard vs. FIFO

FeatureStandardFIFO
ThroughputNearly unlimited300 msg/s per API action (3,000 with batching); high throughput mode scales to tens of thousands/s
DeliveryAt-least-once (duplicates possible)Exactly-once processing (5-minute dedup window)
OrderingBest-effortStrict First-In-First-Out within a MessageGroupId
NameAnyMust end in .fifo
In-flight limit~120,000~20,000

FIFO deduplication uses a MessageDeduplicationId or content-based deduplication (SHA-256 of the body). Messages that share a MessageGroupId are ordered, and different groups are processed in parallel by different consumers.

Message lifecycle

  1. Producer calls SendMessage (single or batch up to 10).
  2. Consumer calls ReceiveMessage (up to 10 messages per call). The message becomes invisible for the visibility timeout.
  3. Consumer processes and calls DeleteMessage. If it fails to delete, the message reappears and is retried.
  4. After maxReceiveCount failed receives, the message moves to the DLQ for investigation. DLQ redrive moves messages back to the source queue once fixed.

Set the visibility timeout longer than the worst-case processing time. For Lambda consumers AWS recommends at least six times the function timeout.

Polling

  • Short polling queries a subset of servers and may return empty results.
  • Long polling waits up to 20 seconds for a message, reducing API calls and cost. Enable it per queue (ReceiveMessageWaitTimeSeconds) or per request.

Security and encryption

  • Access is controlled by IAM policies and queue (resource) policies – the latter is needed for cross-account access and to let SNS or S3 send messages.
  • SSE-SQS (SQS-managed keys) is on by default; use SSE-KMS for audit and key control. TLS protects data in transit; VPC interface endpoints keep traffic private.
json
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": { "Service": "sns.amazonaws.com" },
    "Action": "sqs:SendMessage",
    "Resource": "arn:aws:sqs:us-east-1:111122223333:orders-queue",
    "Condition": { "ArnEquals": { "aws:SourceArn": "arn:aws:sns:us-east-1:111122223333:orders-topic" } }
  }]
}

Common architecture patterns

  • Buffer a database or API during spikes (write-behind, load levelling).
  • Auto Scaling on queue depth: publish a custom CloudWatch metric ApproximateNumberOfMessagesVisible / running instances and scale on that target.
  • Fan-out: SNS topic -> many SQS queues (see the SNS lesson).
  • Large payloads: the SQS Extended Client Library stores the body in S3 and sends a pointer (up to 2 GB).

Creating and using a queue

bash
aws sqs create-queue --queue-name orders-queue \
  --attributes ReceiveMessageWaitTimeSeconds=20,VisibilityTimeout=60,MessageRetentionPeriod=345600
aws sqs send-message --queue-url $QUEUE_URL --message-body '{"orderId":42}'
aws sqs receive-message --queue-url $QUEUE_URL --wait-time-seconds 20

SQS vs. similar services

NeedUse
One consumer processes each message, durable bufferSQS
Push the same message to many subscribersSNS (+SQS fan-out)
Ordered replayable stream, multiple readersKinesis Data Streams
Content-based routing from many event sourcesEventBridge

Real World Example

Scenario — an online photo-printing company handles Black Friday.

Customers upload photos and place orders through a web front end. Previously the web servers called the image-rendering service synchronously, and on Black Friday the renderers saturated, causing timeouts and lost orders.

The team introduces an SQS standard queue called render-jobs. The web tier writes the order to DynamoDB, enqueues a job message containing the S3 key of the photo, and immediately shows "Order received". A fleet of EC2 Spot instances in an Auto Scaling group polls the queue using long polling and renders the images. A CloudWatch alarm on backlog per instance adds workers when the queue grows and removes them when it drains.

A DLQ with maxReceiveCount = 5 captures corrupted files, and an alarm on the DLQ depth pages the on-call engineer. Result: zero lost orders, the front end stays fast during the spike, and rendering costs fall because Spot capacity scales to the queue, not to peak.

Architecture Diagram

SQS decoupling with Auto Scaling workers and a DLQ
Architecture
AWS Cloud
Customers
Region
ALB
Web tierproducer
Main queuestandard
Dead-letter queue
Worker ASGconsumers
Queue depth alarm

Producers enqueue; a worker fleet pulls messages; failures land in a dead-letter queue.

  1. 1The web tier receives a request and sends a message to the SQS queue, then replies to the user immediately.
  2. 2Workers in the Auto Scaling group long-poll the queue and receive messages, which become invisible for the visibility timeout.
  3. 3A worker deletes each message after successful processing; failures reappear and are retried.
  4. 4After maxReceiveCount failures a message moves to the DLQ, while CloudWatch scales the worker fleet on queue depth.

Best Practices

  • ✓Make consumers idempotent. Standard queues deliver at-least-once, so processing the same message twice must be harmless.
  • ✓Enable long polling (20 seconds) on every queue to cut empty receives and cost.
  • ✓Configure a DLQ with a sensible maxReceiveCount (3-5) and alarm on ApproximateNumberOfMessagesVisible for the DLQ.
  • ✓Tune the visibility timeout to exceed the processing time, and extend it with ChangeMessageVisibility for long jobs.
  • ✓Batch send, receive and delete calls (up to 10) to reduce cost and increase throughput.
  • ✓Scale consumers on backlog per instance, not CPU, so capacity follows real demand.
  • ✓Use FIFO only when ordering or exactly-once is required; use message groups to keep parallelism.
  • ✓Encrypt and restrict: keep SSE enabled, use least-privilege queue policies and VPC endpoints for private access.

Common Interview Questions

Exam Tips

SAA-C03 focus

  • "Decouple tiers", "absorb traffic spikes" or "don't lose messages" -> SQS.
  • Need ordering or exactly-once -> FIFO queue (name ends in .fifo). Need max throughput -> standard.
  • Retention: 4 days default, 14 days max. Visibility timeout: 30 s default, 12 h max. Delay: up to 15 minutes.
  • Reduce cost of empty polls -> long polling (up to 20 s).
  • Messages failing repeatedly -> dead-letter queue. A FIFO queue requires a FIFO DLQ.
  • Scale web/worker tier on load -> Auto Scaling based on queue length (SQS-driven) rather than CPU.
  • SQS is pull-based; SNS is push-based. To have one event reach several queues use SNS fan-out.
  • Payload bigger than the limit -> Extended Client Library with S3.

Finished this lesson?

0 of 64 lessons done · 0% of the course