Lesson 1 of 3 Amazon SQS, SNS and EventBridge

Decoupling and Buffering with Amazon SQS

This lesson explains Amazon SQS, a fully managed message queue for decoupling and buffering, and the most common answer to exam questions about decoupling.

Learning outcomes

After completing this lesson, you will be able to:

  • Choose between standard and FIFO queues.
  • Explain visibility timeouts, retention and dead-letter queues.
  • Apply patterns such as load levelling.

Standard or FIFO

SQS offers two queue types. A standard queue supports nearly unlimited throughput but delivers each message at least once and in best-effort order, so a consumer occasionally receives a duplicate or an out-of-order message and must be idempotent. It suits most decoupling and buffering workloads.

A FIFO queue, whose name ends in .fifo, delivers messages in strict order within each message group and removes duplicates using deduplication IDs, giving exactly-once processing. Messages in different groups are processed in parallel, so using, for example, the customer ID as the group ID keeps each customer's messages in order while allowing concurrency. FIFO throughput is lower, though high-throughput mode raises it. The following table summarises the differences.

Standard
ThroughputNearly unlimited
DeliveryAt least once (occasional duplicates)
OrderingBest effort
FIFO (.fifo)
Throughput300 msg/s per action, 3,000 with batching, more in high-throughput mode
DeliveryExactly once via deduplication IDs
OrderingStrict within a message group

Message lifecycle settings

SQS messages are not pushed to consumers; consumers poll the queue, process each message and then delete it. Several settings govern this cycle. The visibility timeout hides a received message from other consumers while it is processed; if the consumer does not delete it in time, it becomes visible again and is processed twice, so the timeout must exceed the processing time. Long polling makes a receive request wait up to 20 seconds for messages instead of returning empty, which reduces empty responses and cost.

The retention period decides how long an unprocessed message is kept, from one minute to 14 days. A delay queue postpones the delivery of new messages for up to 15 minutes. A dead-letter queue receives messages that have failed more than maxReceiveCount times, so they stop blocking the queue and can be examined and redriven once the problem is fixed. The following table summarises the settings.

SettingValuePurpose
Visibility timeout30 s default, max 12 hHides a received message; set longer than processing or it reappears
Long pollingUp to 20 sFewer empty responses, lower cost
Retention1 min – 14 days (4 days default)How long unprocessed messages stay
Delay queueUp to 15 minPostpone delivery
Dead-letter queueAfter maxReceiveCountIsolate failing messages; redrive once fixed

Consumers poll, process, then delete. Large payloads use the Extended Client Library (S3). Encrypt with SSE-SQS or KMS; control access with queue policies.

The following diagram shows a message's path and the settings that act on it.

The life of an SQS message. A consumer polls the queue, with long polling waiting up to 20 seconds, and receives a message, which is then hidden from other consumers for the visibility timeout. If the consumer deletes it in time, it is gone; if not, it becomes visible again. After more than maxReceiveCount failed receives, it moves to the dead-letter queue.
Because a message that is not deleted in time is delivered again, consumers should process messages idempotently.

12hours

Maximum visibility timeout

20s

Maximum long-polling wait

14days

Maximum retention

15min

Maximum delivery delay

Troubleshooting scenario

Queue problems

Exam skills 2.1.3

Resolve common SQS processing problems.

Log in to try this activity Interactive activities are free with an account.

Patterns

Amazon SQS supports several common patterns. In load levelling, a queue sits in front of a back end with limited capacity, such as one that can process 100 orders per second: the queue accepts bursts far above that rate, and the back end works through them at its own pace, so no requests are lost.

In independent scaling, consumers scale with the queue's backlog: EC2 Auto Scaling groups scale on the backlog per instance, Lambda event source mappings scale automatically, and ECS services scale on queue metrics. SQS has no message priorities, so priority is implemented with separate high-priority and low-priority queues, polling the high-priority queue first or giving it more consumers.

Scenario questions

SQS patterns

Exam skills 2.1.3 · 3.2.1

Apply SQS patterns to each requirement.

Log in to try this activity Interactive activities are free with an account.
Worked example

A queue for an overloaded back end

An application's order processing sometimes cannot keep up, and orders are lost when the processing servers are overwhelmed.

Design choiceSetting
Decouple the web tier from processingWeb tier sends orders to an SQS queue
Never lose an orderMessages stay in the queue until processed and deleted (retention up to 14 days)
Each order processed in sequence per customerFIFO queue with the customer ID as message group
Bad orders do not block the queueDead-letter queue after 5 failed attempts
Scale processing with the backlogAuto Scaling on queue depth per instance

The queue turns a fragile synchronous chain into a buffered, scalable design: the web tier stays fast, and processing catches up at its own pace.

Check yourself

Which queue type guarantees order and removes duplicates?

Show answer

A FIFO queue, which preserves order within each message group and deduplicates messages sent within five minutes. Standard queues may deliver duplicates and out of order.

Exam Tips

Remember for the exam

  • To decouple tiers and absorb spikes, use SQS.
  • For strict ordering and no duplicates, use a FIFO queue with message group IDs.
  • When messages are processed twice, increase the visibility timeout beyond the processing time.
  • To isolate messages that repeatedly fail, use a dead-letter queue.
  • When a back end cannot handle bursts of traffic, put an SQS queue in front of it to level the load.
  • For message priorities, use a separate queue for each priority.