SQS queues

Intermediate · 29 min

Standard and FIFO queues, the visibility timeout, long polling, dead-letter queues, redrive and fair queues, and what at-least-once delivery asks of your code.

Before thisHow API requests are signed, throttled and retriedLambda functions and the execution model

Be A Better Dev · 29 min

SQS queues

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Be A Better DevReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Receiving a message does not remove it. It hides the message for the visibility timeout, and you delete it only after the work is done.
  • Standard queues deliver at least once with best-effort ordering, so handlers must be idempotent.
  • FIFO queues order messages within a message group and drop repeated sends inside a 5-minute deduplication window. They do not make your side effects happen exactly once.
  • A redrive policy moves a message to a dead-letter queue after maxReceiveCount receives. On standard queues its retention clock still counts from the original send.
  • Use long polling. A short poll samples only some servers and can come back empty while messages are waiting.
  • With Lambda, set the queue's visibility timeout to at least six times the function timeout and report partial batch failures.

The problem with calling directly

Your checkout service calls the email service over HTTP. The email service is slow today, so checkout is slow. It goes down, so checkout fails, even though the order itself was fine. The two are tied together in time: both have to be healthy at the same moment.

A queue unties them. The producer writes a message and moves on. A consumer reads it when it can. Think of the rail of order tickets in a restaurant kitchen: waiters clip tickets up at whatever pace customers arrive, cooks pull them down at whatever pace they can cook. A rush makes the rail longer instead of making waiters stand still. The general idea is covered in message queues and async communication. This lesson is about how Amazon SQS behaves.

SQS is an API with no servers for you to run. You create a queue, then call SendMessage, ReceiveMessage and DeleteMessage.

Receiving is borrowing

The part people get wrong: receiving a message doesn’t remove it. SQS hides it for the visibility timeout, which defaults to 30 seconds and can be anything up to 12 hours. The consumer does the work and then deletes the message. If it crashes first, the timeout runs out and the message becomes visible again for someone else.

That one rule gives you crash recovery without the queue knowing anything about your workers. It also means a timeout shorter than your processing time produces duplicates. For long or unpredictable jobs, call ChangeMessageVisibility while you work, like a heartbeat. The 12-hour ceiling counts from the first receive and extending doesn’t reset it.

Here is the cycle against moto, a local emulator, with a 2-second timeout and a dead-letter queue that takes over after 2 receives. The script waits 2.5 seconds between attempts and never deletes:

receive 1: {"orderId":"1001"}   ApproximateReceiveCount=1
  again at once: None
receive 2: {"orderId":"1001"}   ApproximateReceiveCount=2
  again at once: None
receive 3: None
dlq: {"orderId":"1001"}

The message is invisible right after each receive, comes back when the timeout passes, and on the third attempt has moved to the dead-letter queue.

Standard and FIFO

A standard queue is the default. It scales to very high request rates, delivers each message at least once, and keeps order only on a best-effort basis. A duplicate is rare but legal, so your handler has to be idempotent: processing the same message twice must end in the same state as once. The usual tool is a conditional write keyed on a business ID, such as “insert this payment unless its ID exists”.

A FIFO queue (its name ends in .fifo) adds two things:

  • Message groups. Every send carries a MessageGroupId. Messages in one group come out in order, and the next one waits until the current one is deleted or reappears. Different groups proceed in parallel.
  • Deduplication. A repeated send with the same deduplication ID within 5 minutes is accepted but not enqueued again. The ID is either yours or a hash of the body.

On moto, sending the body hello twice with content-based deduplication left ApproximateNumberOfMessages at 1, and a send without a group ID was rejected with MissingParameter.

FIFO does not mean your email goes out exactly once. If the consumer sends the email and dies before deleting the message, the message is delivered again. FIFO also has throughput quotas that standard queues don’t; high throughput mode raises them by spreading groups over partitions, so many distinct group IDs matter.

Polling well

ReceiveMessage returns up to 10 messages. By default it short polls: it asks a sample of SQS’s servers and answers immediately, so it can come back empty while messages exist. Set WaitTimeSeconds (or the queue’s ReceiveMessageWaitTimeSeconds) up to 20 for long polling. You get fewer empty responses and pay for fewer calls.

Other numbers worth knowing, checked in November 2026: a message can be up to 1 MiB (many older materials say 256 KB), retention is 4 days by default and at most 14, and a delay before first delivery can be up to 15 minutes. For bigger payloads, put the object in S3 and send a pointer.

Poison messages and redrive

Some messages will never succeed, for example malformed JSON. Without a limit they cycle forever. A redrive policy on the source queue names a dead-letter queue (same account and Region) and a maxReceiveCount. Too low and one transient error parks a good message.

Alarm on the dead-letter queue having anything in it. Watch one trap: on a standard queue the message keeps its original enqueue time, so set the dead-letter queue’s retention longer than the source’s. After fixing the bug, a redrive task moves messages back to the source queue or another queue of the same type.

Noisy neighbours

Put many customers in one queue and one of them can bury the rest. Fair queues address this on standard queues: producers set MessageGroupId to the tenant ID, and SQS favours other tenants when one holds a disproportionate number of in-flight messages. Consumers don’t change. Here the group ID is only a tenant label and orders nothing.

Lambda as the consumer

Lambda can poll for you through an event source mapping. Three settings matter. Make the visibility timeout at least six times the function timeout. Set maxReceiveCount to 5 or more. And turn on ReportBatchItemFailures, returning only the failed message IDs; otherwise one bad message sends the whole batch back.

About the video

The embed is Be A Better Dev’s “AWS SQS Overview For Beginners”, January 2022, 29 minutes. Its chapters cover core concepts, the processing workflow, why a queue beats a direct API call, standard versus FIFO, common patterns and important details. It predates fair queues and the move to a 1 MiB message size, so take limits from this page. I judged it from its chapters and description only, since transcripts couldn’t be fetched. English was judged from the “English (US) original” audio track. A longer October 2026 walkthrough is in deeper.

What was run: the redrive cycle, FIFO deduplication and the missing group ID error ran on moto 5.1.22, an emulator, so they show API behaviour and not real AWS timing. Limits, fair queues, dead-letter retention and the Lambda settings are from the AWS docs, checked 2026-11-29.

Where this fits

A queue hands each message to one consumer. When several systems need the same news, you put a topic in front.

Check yourself

  1. 01A worker takes 90 seconds per message and the queue has the default visibility timeout. What happens?

    The default is 30 seconds, so the message becomes visible again while the first worker is still busy, and another worker picks it up. Raise the timeout above the processing time, or extend it with ChangeMessageVisibility as a heartbeat.

  2. 02Your dead-letter queue keeps messages for 4 days, the same as the source queue. Why can a message vanish from it sooner than 4 days after arriving?

    On a standard queue expiry is based on the original enqueue time, which does not reset when the message moves. Give the dead-letter queue a longer retention than the source.

  3. 03You use a FIFO queue with one message group ID for every order in the shop. What did you build?

    A single-file line. Messages in a group are processed strictly one after another, so only one batch is in progress at a time. Use something like the order or customer ID as the group, so unrelated work runs in parallel.

  4. 04One tenant floods a shared standard queue. What can you change without touching the consumers?

    Have producers set MessageGroupId to the tenant ID. That turns on fair queues, which give priority to quieter tenants when one has a disproportionate share of messages in flight. It does not add ordering on a standard queue.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark