Question

Difficulty: EasyServerless Development with AWS Lambda

A developer is configuring a serverless workflow where an Amazon SQS queue triggers an AWS Lambda function to process inventory updates. The Lambda function has its timeout set to 2 minutes. During testing, the developer notices that some messages are being processed multiple times by different Lambda invocations. Which configuration change will resolve this issue?

  1. A
    Decrease the Lambda function's timeout to 10 seconds to ensure the function finishes before the SQS visibility timeout.
  2. Configure the SQS queue's visibility timeout to be at least 12 minutes (six times the Lambda function's timeout).Answer
  3. C
    Enable long polling on the SQS queue by setting the ReceiveMessageWaitTimeSeconds parameter to 20 seconds.
  4. D
    Increase the DelaySeconds configuration on the SQS queue to match the Lambda function's execution time.

Answer

Configure the SQS queue's visibility timeout to be at least 12 minutes (six times the Lambda function's timeout).
The correct option proposes configuring the SQS queue's visibility timeout to be at least 12 minutes (six times the Lambda function's timeout of 2 minutes). AWS best practices dictate that the visibility timeout of the source queue must be set to at least 6 times the timeout of the Lambda function to prevent messages from reappearing in the queue and being processed again while the current Lambda invocation is still running or retrying.

Step-by-Step Solution

1
Analyze the relationship between the Lambda execution duration and SQS visibility timeout.
The Lambda function is configured to run for up to 2 minutes, but the SQS visibility timeout determines how long a message is hidden from other consumers after being polled.
If the visibility timeout is shorter than the Lambda execution time (or not sufficiently long to account for retries and batches), the message will reappear in the queue and be polled by another execution thread before the first one finishes.
2
Apply the AWS-recommended formula for SQS visibility timeout with Lambda.
The visibility timeout should be at least 6 times the Lambda function's timeout (6×2 minutes=12 minutes6 \times 2 \text{ minutes} = 12 \text{ minutes}).
Setting the visibility timeout to at least six times the function's timeout allows the Lambda service to handle retries, batch processing, and potential throttling events without exposing the messages to other consumers too early.

Key Concept

SQS visibility timeout integration with AWS Lambda
Rate this question