AWS Builder Center

Building a More Reliable Queue with Amazon SQS and AWS Lambda

A practical introduction to using Amazon SQS and AWS Lambda together, with tips for handling duplicate messages, batch failures, and messages that repeatedly fail.

Building a More Reliable Queue with Amazon SQS and AWS Lambda

Imagine an online store that needs to resize product images after each upload. The upload should finish quickly, even if image processing takes longer. A queue can separate those two steps: the application records a task in Amazon Simple Queue Service (Amazon SQS), and an AWS Lambda function processes it later.
This pattern also helps when work arrives in bursts. The queue holds messages until the consumer can process them.

How the pieces fit together

  1. A producer sends a message to an SQS queue. The message describes work to do, such as an image location and the requested output size.
  1. A Lambda event source mapping polls the queue and invokes the function with one or more messages.
  1. When the function successfully processes the batch, Lambda deletes those messages from the queue.  AWS Lambda and SQS 
The producer and processor can now scale and fail independently. If the processor is temporarily busy, messages wait in the queue.

Plan for retries and duplicates

A queue-based workflow needs to handle retries. Lambda processes SQS messages at least once, so the same message can be delivered more than once. Design the function to be idempotent: processing a duplicate should not create an unwanted second result. For example, the function might check whether the output already exists before creating it.
Visibility timeout matters too. While a message is being processed, SQS hides it from other consumers. If it becomes visible again before processing finishes, another invocation could process it. For Lambda event source mappings, AWS recommends setting the queue’s visibility timeout to at least six times the function timeout.  SQS event source configuration 

Handle batch failures carefully

Lambda may send several messages in one batch. By default, if processing fails, the whole batch can be retried—including messages that already succeeded.
For workloads where some messages may fail independently, configure partial batch responses. The function can report only the failed messages, allowing successful ones to be removed while failed ones are retried.  Handling errors for an SQS event source 

Give persistent failures a place to go

A message that fails repeatedly can block progress or consume processing capacity. Configure a dead-letter queue (DLQ) so messages that exceed the retry limit can be isolated and inspected. A DLQ is useful for finding patterns in failures, such as malformed input or a downstream service problem.
Before putting a DLQ into production, decide who will monitor it and how messages will be investigated and safely retried.

A reliability checklist

  • Make the handler safe to run more than once.
  • Set the visibility timeout with the full function and batch processing time in mind.
  • Use partial batch responses when messages in a batch can succeed or fail independently.
  • Configure a DLQ and decide how to handle its messages.
  • Monitor queue depth and failures so growing backlogs are visible.
  • Test what happens when a message is malformed or a downstream service is unavailable.
SQS and Lambda can make background work easier to scale and recover. The queue provides a buffer; careful retry, duplicate, and failure handling makes the workflow more dependable.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article