A developer is implementing a serverless data import service. An AWS Lambda function is triggered by an Amazon SQS queue to process batches of user records. The Lambda function is configured with a batch size of 10, and processing a single batch takes an average of 120 seconds. The SQS queue has a default visibility timeout of 30 seconds. During testing, the developer observes that many user records are processed multiple times by parallel Lambda invocations. Which of the following changes will prevent this duplicate processing?
- AInitialize the AWS SDK SQS client inside the Lambda function using hardcoded access keys to bypass IAM role credential resolution overhead and speed up message processing.
- BDecrease the Amazon SQS queue's visibility timeout to 15 seconds to ensure that any failed messages are retried immediately by another Lambda invocation.
- Increase the Amazon SQS queue's visibility timeout to a value that is at least 6 times the Lambda function's timeout, and set the Lambda function's timeout to exceed the maximum batch processing time.Answer
- DIncrease only the Lambda function's timeout configuration to 180 seconds, as the SQS visibility timeout automatically expands to match the duration of the active Lambda execution context.
Answer
Increasing the SQS queue's visibility timeout to at least 6 times the Lambda function's timeout and configuring the Lambda timeout to exceed the maximum batch processing time.
To prevent duplicate processing of messages in an SQS-triggered Lambda function, the queue's visibility timeout must be set to at least 6 times the Lambda function's timeout. Additionally, the Lambda function's timeout must be configured to be greater than the maximum expected processing time for a batch of messages. Since processing takes 120 seconds, setting the Lambda timeout to at least 150 seconds and setting the SQS visibility timeout to at least 6 times that value (900 seconds) allows the Lambda function sufficient time to complete execution and delete the messages from the queue before they become visible to other invocations.
Step-by-Step Solution
Key Concept
SQS Visibility Timeout configuration when integrated as an AWS Lambda event source.