What happens if a Lambda function fails to process an SQS message before the visibility timeout
When an SQS message is received by a Lambda function, the message’s visibility timeout starts. This means the message is invisible to other consumers for that duration.
However, if the Lambda function fails to process the message before the visibility timeout expires, a few things will happen:
- The message will reappear in the SQS queue and become available for other consumers. This is to prevent messages from being lost if a Lambda function fails.
- By default, the message will be sent to the dead letter queue (DLQ) if it is not successfully processed within the visibility timeout plus the number of receive attempts specified in the redrive policy. The DLQ allows you to further inspect and handle failed messages.
- The Lambda function execution will result in an error, and you can use CloudWatch Logs to troubleshoot the issue. You may need to update your Lambda function code to properly handle the message.
- You have the option to extend the visibility timeout of a message from within the Lambda function using the ChangeMessageVisibility API. This can give your Lambda function more time to process the message before it reappears in the queue.
- If a message fails processing multiple times, you can either fix the underlying issue or delete the problematic message from the queue/DLQ.
In summary, it is important to set an appropriate visibility timeout for your use case. The timeout should be long enough to allow for normal processing but short enough to detect failures quickly and re-attempt message processing. Extending timeouts on a per-message basis from within the Lambda function can also help optimize throughput.
Sources
- https://stackoverflow.com/questions/69546106/aws-sqs-and-lambda-function-how-to-fail-a-lambda-function-and-force-message-ba
- https://stackoverflow.com/questions/71626759/how-to-manage-message-visibility-timeout-effectively
- https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-visibility-timeout.html
Leave a Comment