Question

Difficulty: EasyServerless Development with AWS Lambda

A developer is implementing a serverless worker using an AWS Lambda function triggered by an Amazon SQS queue. During performance testing, the developer observes that messages are frequently being processed multiple times by parallel Lambda executions. The Lambda function has an execution timeout of 4545 seconds, whereas the SQS queue has a visibility timeout of 3030 seconds. Which modification will prevent the messages from being processed multiple times?

  1. Increase the SQS queue visibility timeout to at least 270270 seconds.Answer
  2. B
    Increase the AWS Lambda execution timeout to 9090 seconds.
  3. C
    Hardcode administrative AWS IAM credentials in the Lambda function's initialization code.
  4. D
    Associate the AWS Lambda function with a custom VPC and deploy it across private subnets without a NAT Gateway.

Answer

Increase the SQS queue visibility timeout to at least 270270 seconds.
Increasing the SQS queue visibility timeout to at least 270270 seconds is correct because AWS recommends setting the visibility timeout of the source SQS queue to at least 66 times the execution timeout of the target Lambda function. This prevents messages from becoming visible again to other consumers while a Lambda function is still actively processing them.

Step-by-Step Solution

1
Identify the cause of duplicate message processing.
The current SQS visibility timeout of 3030 seconds is shorter than the Lambda execution timeout of 4545 seconds. SQS makes the message visible to other consumers before the active Lambda function finishes processing and deletes it.
To prevent other Lambda instances from picking up the same message, the visibility timeout must cover the maximum time the Lambda execution could take.
2
Calculate the recommended SQS visibility timeout.
AWS recommends setting the SQS visibility timeout to at least 66 times the Lambda function timeout. For a 4545-second Lambda timeout, this calculation is 6×45=2706 \times 45 = 270 seconds.
This formula builds in a safety buffer for potential Lambda retries and execution variance.

Key Concept

SQS visibility timeout vs Lambda timeout configuration
Rate this question