Skip to Content

Amazon SQS

SQS standard queues, how producers send and consumers receive and delete messages, scaling consumers with Auto Scaling, security, visibility timeout and long polling.

What it is

Amazon SQS (Simple Queue Service) is a fully managed message queue. Producers put messages in; consumers take them out and process them. It's AWS's oldest service (over 20 years old) and the standard way to decouple applications.

An SQS queue: many producers send messages to the queue, which stores them; many consumers poll the queue, process messages and delete them An SQS queue: many producers send messages to the queue, which stores them; many consumers poll the queue, process messages and delete them

Producers send; consumers poll, process and delete.

Standard queues

  • Nearly unlimited throughput, and an unlimited number of messages in the queue.
  • Messages are kept 4 days by default, up to 14 days.
  • Low latency: under 10 ms to send and receive.
  • Each message can be up to 1 MiB. For bigger payloads, store the data in S3 and send a pointer (the SQS Extended Client Library does this automatically).
  • At-least-once delivery: a message can occasionally be delivered more than once.
  • Best-effort ordering: messages can arrive out of order. (For strict order, use FIFO queues.)

Updated since the slides

For years the maximum message size was 256 KB. AWS raised it to 1 MiB in 2025, which the slides already show. Older practice questions may still say 256 KB.

Producing messages

  • Producers send messages with the SDK's SendMessage API (or SendMessageBatch, up to 10 at once).
  • The message is stored in SQS until a consumer deletes it (or it expires after the retention period).
  • Example: send an order to be processed, with the order ID, customer ID and any attributes needed.

Consuming messages

  1. Consumers (on EC2 instances, on-premises servers, or AWS Lambda) poll SQS for messages, receiving up to 10 at a time.
  2. They process each message, for example inserting the order into an RDS database.
  3. They delete each message with the DeleteMessage API, so no other consumer handles it again.

Several consumers can read the same queue in parallel: each receives different messages. If processing is too slow, add more consumers (scale horizontally).

Scaling consumers with an Auto Scaling group

Scaling consumers on queue length: EC2 instances in an Auto Scaling group poll an SQS queue; CloudWatch tracks the queue length metric ApproximateNumberOfMessages, and when it is too high an alarm makes the group scale out Scaling consumers on queue length: EC2 instances in an Auto Scaling group poll an SQS queue; CloudWatch tracks the queue length metric ApproximateNumberOfMessages, and when it is too high an alarm makes the group scale out

More messages waiting, more instances working.

Run the consumers in an Auto Scaling group, and scale on the queue length: the CloudWatch metric ApproximateNumberOfMessages. When the backlog grows past a threshold, an alarm makes the group add instances; when it shrinks, the group removes them. (AWS recommends refining this into a backlog per instance target, using a custom metric.)

Security

  • Encryption:
    • in flight, using the HTTPS API;
    • at rest, with SQS-managed keys (SSE-SQS) or KMS keys (SSE-KMS);
    • client-side, if the client wants to encrypt and decrypt itself.
  • Access control: IAM policies control who can call the SQS API.
  • SQS access policies (resource policies, like S3 bucket policies) are useful for:
    • cross-account access to a queue;
    • letting other services such as SNS or S3 write to the queue.

Visibility timeout

The visibility timeout: when a consumer receives a message, it is hidden from other consumers for the visibility timeout, 30 seconds by default, so other receive requests do not return it; if it is not deleted before the timeout ends, it becomes visible and is returned again The visibility timeout: when a consumer receives a message, it is hidden from other consumers for the visibility timeout, 30 seconds by default, so other receive requests do not return it; if it is not deleted before the timeout ends, it becomes visible and is returned again

A received message is hidden for a while, not removed.
  • When a consumer receives a message, the message becomes invisible to other consumers for the visibility timeout: 30 seconds by default (up to 12 hours).
  • That's the time the consumer has to process and delete it.
  • If the timeout ends before the message is deleted, the message becomes visible again and another consumer may receive it: it's processed twice.
  • A consumer that needs more time can call ChangeMessageVisibility to extend it.
  • Set it with care:
    • too high (hours): if a consumer crashes, the message waits a long time before anyone retries it;
    • too low (seconds): messages are handed out again while still being worked on, causing duplicates.

Long polling

  • When a consumer asks for messages and the queue is empty, it can wait for messages to arrive instead of getting an empty answer straight away. This is long polling.
  • It reduces the number of API calls (and cost) and reduces latency: a message is returned as soon as it arrives.
  • The wait time is 1 to 20 seconds (20 is best).
  • Long polling is preferable to short polling. Turn it on for the whole queue (receive message wait time) or per request with WaitTimeSeconds.

Decoupling application tiers

A front-end web app receives requests (for example "process this video") and sends a message to SQS instead of doing the heavy work itself. A back-end fleet receives the messages and does the processing. Each tier has its own Auto Scaling group and scales on its own metric; the queue is effectively infinitely scalable in between.

Real-world examples

  • An image site queues resize jobs in SQS; EC2 workers in an Auto Scaling group scale with the queue length.
  • An order system sends orders to SQS so a database outage never loses an order: they wait in the queue until the database is back.
  • A Lambda function is triggered by an SQS queue to process each batch of messages, with no servers to run.

Exam tips

  • "Decouple tiers", "buffer requests", "handle traffic spikes" → SQS.
  • "Scale workers on the number of messages waiting" → Auto Scaling on ApproximateNumberOfMessages (or backlog per instance).
  • "Messages are processed twice" → visibility timeout too short; raise it or call ChangeMessageVisibility.
  • "Reduce empty responses and API costs" → long polling (WaitTimeSeconds up to 20).
  • "Let S3 or SNS send to a queue", "cross-account access" → SQS access policy.
  • "Messages larger than 1 MiB" → S3 plus a pointer (Extended Client Library).
  • "Keep messages up to 14 days" → set the retention period.

Common traps

  • "SQS standard guarantees order and exactly-once delivery." It's best-effort order and at-least-once; use FIFO for strict order.
  • "Consumers don't need to delete messages." Undeleted messages reappear after the visibility timeout.
  • "SQS pushes messages to consumers." Consumers poll (Lambda polls on the function's behalf).

Hands-on lab: send, receive, delete

Goal: create a queue, send a message, and watch the visibility timeout in action.

Costs money

SQS charges per million requests; a few dozen requests cost far less than a cent. Any Region works.

Create a queue

SQS → Create queue. Type Standard, name saa-orders. Set Visibility timeout to 30 seconds and Receive message wait time to 20 seconds (long polling). Keep the default encryption (SSE-SQS). Create.

Send a message

Open the queue → Send and receive messages. Body {"orderId": 1, "item": "pencil"}. Send. The queue shows 1 message available.

Receive without deleting

Choose Poll for messages. The message appears, with a receive count of 1. Wait 30 seconds and poll again: it comes back, receive count 2. The visibility timeout ended and it became visible again.

Delete it

Select the message and Delete. Poll again: the queue is empty (the poll waits up to 20 seconds, which is long polling).

Expected result: a message that keeps coming back until it's deleted.

Clean up: delete the saa-orders queue (or keep it for the next lab).

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt