Operating Amazon SQS Queues and Lambda Event Source Mappings
This lesson explains how Amazon SQS decouples producers from consumers, absorbing bursts and surviving failures, and how Lambda event source mappings consume it.
Learning outcomes
After completing this lesson, you will be able to:
- Choose queue types and key settings such as visibility timeout.
- Isolate poison messages with dead-letter queues and redrive.
- Configure event source mappings and partial batch responses.
- Prevent duplicate processing.
Queue types and key settings
Standard queues offer nearly unlimited throughput with at-least-once delivery and best-effort ordering. FIFO queues guarantee order within a message group and exactly-once processing within a deduplication interval, with high throughput mode for higher rates. Important settings are the visibility timeout (how long a received message is hidden from other consumers; default 30 seconds, maximum 12 hours), message retention (1 minute to 14 days, default 4 days), delivery delay, receive message wait time (long polling up to 20 seconds, which reduces empty responses and cost) and maximum message size.
30s
Default visibility timeout
12hours
Maximum visibility timeout
4 / 14days
Default and maximum message retention
20s
Maximum long polling wait time
SQS settings
Dead-letter queues and redrive
A redrive policy moves a message to a dead-letter queue (DLQ) after it has been received maxReceiveCount times without being deleted, isolating "poison" messages that always fail. The DLQ must be the same type (standard or FIFO) as the source queue, and its retention should be longer than the source queue's because the original enqueue timestamp is kept. After fixing the consumer, DLQ redrive moves messages back to the source queue. Alarm on the DLQ's ApproximateNumberOfMessagesVisible and on the source queue's ApproximateAgeOfOldestMessage.
Lambda event source mappings for SQS
With an event source mapping, Lambda polls the queue, invokes the function with batches (up to 10 messages for FIFO, up to 10,000 for standard with a batching window), and deletes messages when the invocation succeeds. If the function fails, the whole batch becomes visible again after the visibility timeout unless the function reports partial batch failures (ReportBatchItemFailures), in which case only failed messages return. AWS recommends a queue visibility timeout of at least six times the function timeout. Maximum concurrency on the mapping limits how many concurrent invocations the queue can drive, protecting downstream systems, and the execution role needs sqs:ReceiveMessage, sqs:DeleteMessage and sqs:GetQueueAttributes.
| Problem | Cause | Fix |
|---|---|---|
| Messages processed more than once | Visibility timeout shorter than processing time | Visibility timeout at least 6x the function timeout; idempotent code |
| Whole batches retried for one bad message | No partial batch response | Enable ReportBatchItemFailures |
| Poison messages block progress | No redrive policy | Configure a DLQ with maxReceiveCount |
| Database overwhelmed by consumers | Unlimited polling concurrency | Set maximum concurrency on the mapping or reserved concurrency |
| Messages expire before processing | Retention too short or consumers too slow | Increase retention; scale consumers |
The following diagram shows a batch with one failed message.

Queue processing problems
Duplicate orders
An order processor reads from an SQS standard queue with a Lambda function whose timeout is 60 seconds. During peaks some orders are created twice. The queue's visibility timeout is the default 30 seconds, so when processing takes longer than 30 seconds the message becomes visible again and a second invocation processes it. The operator sets the visibility timeout to 360 seconds (six times the function timeout), enables partial batch responses, adds a DLQ with maxReceiveCount of 5, and asks the developers to make the handler idempotent with a conditional write on the order ID, because standard queues can still deliver a message more than once.
When does a redrive policy move a message to the DLQ?
Show answer
After it has been received maxReceiveCount times without being deleted.
Exam Tips
Remember for the exam
- Visibility timeout too short causes duplicate processing; use at least 6x the Lambda timeout.
- Redrive policies move messages to a DLQ after maxReceiveCount receives.
- Use partial batch responses so one failure does not retry the whole batch.
- Long polling (up to 20 seconds) reduces empty receives and cost.
- Alarm on ApproximateAgeOfOldestMessage and DLQ depth.