Soru

Zorluk: OrtaServerless Development with AWS Lambda

A developer is implementing a serverless integration where an Amazon Simple Queue Service (Amazon SQS) queue triggers an AWS Lambda function to process batch data. Each batch of messages takes approximately 4 minutes to process, and the Lambda function's timeout is configured to 5 minutes. During testing, the developer notices that many messages are being received and processed multiple times by separate Lambda execution environments. Which action should the developer take to prevent these duplicate executions?

  1. A
    Configure the SQS queue's visibility timeout to 3 minutes to match the average batch processing time.
  2. Increase the Amazon SQS queue's visibility timeout to at least 30 minutes.Cevap
  3. C
    Increase the AWS Lambda function's timeout to 30 minutes to allow ample time for all retries.
  4. D
    Update the AWS Lambda function's IAM execution role trust policy to grant the sqs:DeleteMessage permission.

Cevap

Increase the Amazon SQS queue's visibility timeout to at least 30 minutes.
To prevent duplicate processing of SQS messages by Lambda, the SQS queue's visibility timeout must be set to at least 6 times the Lambda function's timeout. Since the Lambda function timeout is 5 minutes, the visibility timeout must be configured to at least 30 minutes (6×5=306 \times 5 = 30). This ensures messages remain invisible while the Lambda function processes the batch and handles any potential retries.

Adım Adım Çözüm

1
Identify the cause of duplicate message processing.
The issue occurs because the SQS visibility timeout is too short, allowing other execution environments to pull the message before the active Lambda function completes and deletes it.
Since each batch takes 4 minutes and the Lambda timeout is 5 minutes, SQS must keep the messages hidden long enough to cover execution and retry attempts.
2
Calculate the recommended SQS visibility timeout.
Using the formula Visibility Timeout6×Lambda Timeout\text{Visibility Timeout} \ge 6 \times \text{Lambda Timeout}, we calculate 6×5 minutes=30 minutes6 \times 5\text{ minutes} = 30\text{ minutes}.
AWS recommends a visibility timeout of at least 6 times the Lambda function's timeout to prevent duplicate processing during retries.
3
Configure the SQS queue with the calculated value.
Setting the SQS queue's visibility timeout to at least 30 minutes resolves the duplicate processing issue.
This guarantees that messages remain invisible in the queue for the entire duration of the Lambda execution and potential retries.

Anahtar Kavram

Amazon SQS visibility timeout configuration for AWS Lambda integrations
Bu soruyu puanla