Lesson 3 of 3 AWS Lambda and Amazon SQS Operations

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

Match the pairs

SQS settings

Exam skills 2.2.2 · 3.2.2

Match each SQS setting with what it controls.

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

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.

ProblemCauseFix
Messages processed more than onceVisibility timeout shorter than processing timeVisibility timeout at least 6x the function timeout; idempotent code
Whole batches retried for one bad messageNo partial batch responseEnable ReportBatchItemFailures
Poison messages block progressNo redrive policyConfigure a DLQ with maxReceiveCount
Database overwhelmed by consumersUnlimited polling concurrencySet maximum concurrency on the mapping or reserved concurrency
Messages expire before processingRetention too short or consumers too slowIncrease retention; scale consumers

The following diagram shows a batch with one failed message.

Lambda reading from SQS. The event source mapping polls the queue and invokes the function with a batch; messages are deleted when the invocation succeeds. With ReportBatchItemFailures, only the failed messages return to the queue after the visibility timeout, which should be at least six times the function timeout. Messages that keep failing move to the dead-letter queue.
The batch size and message numbers are illustrative. Without partial batch responses, all ten messages would return to the queue.
Troubleshooting scenario

Queue processing problems

Exam skills 2.2.2 · 1.2.1

Fix problems with Lambda functions that read from SQS.

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

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.

Check yourself

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.