AWS Lambda and SQS: Why Retries Need More Than a Queue
How duplicate delivery and partial batch failures affect a serverless application.
A queue helps separate an application from its background work. But adding Amazon SQS and AWS Lambda does not automatically make that work safe to retry.
Consider an application that queues a request to generate a document. Lambda processes the request, saves the document, and then fails before the message is successfully acknowledged. The same request may be processed again.
That possibility changes how the worker should be designed.
1. Expect duplicate processing
Lambda’s SQS event source mapping processes events at least once. Duplicate processing can occur, so the worker should be idempotent: repeating the same request should not create an unintended additional effect.
For the document example, a stable request ID could identify the job across retries. The implementation must also handle concurrent attempts and failures between creating the document and recording completion. A simple “check whether it exists” is not enough if two workers can pass that check together.
2. Handle failures within a batch
Lambda can receive multiple SQS messages in one batch. By default, a failed batch can cause successfully processed messages to become available again.
Partial batch responses let the function identify failed messages so those messages can be retried separately. This requires enabling ReportBatchItemFailures on the event source mapping and returning the failed message identifiers correctly.
Partial batch responses reduce unnecessary reprocessing. They do not remove the need for idempotency.
3. Test the awkward failure
A useful test is not just “Does the function process a message?”
It is: “What happens if the work succeeds, but the function fails before completion is acknowledged?”
That scenario exposes whether a retry produces a duplicate document, notification, or other side effect.
The architecture lesson is straightforward: a queue separates components, but the application still needs to define what happens when the same work arrives again.
References
- Using Lambda with Amazon SQS: https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html
- Handling errors for an SQS event source: https://docs.aws.amazon.com/lambda/latest/dg/services-sqs-errorhandling.html
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article