Soru

Zorluk: OrtaMessage-Based Integration using Amazon SQS and SNS

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?

  1. A
    Initialize 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.
  2. B
    Decrease the Amazon SQS queue's visibility timeout to 15 seconds to ensure that any failed messages are retried immediately by another Lambda invocation.
  3. 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.Cevap
  4. D
    Increase 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.

Cevap

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.

Adım Adım Çözüm

1
Analyze the relationship between batch processing time and the SQS visibility timeout.
The batch processing time (120 seconds) is longer than the SQS visibility timeout (30 seconds), meaning messages become visible in the queue before processing completes.
This mismatch causes other Lambda instances to fetch and process the same messages, resulting in duplicate executions.
2
Determine the necessary Lambda timeout configuration.
The Lambda function timeout must be set to a value greater than the average processing time of 120 seconds (e.g., 150 seconds) to ensure the batch can finish processing.
If the Lambda function times out before processing completes, the messages will not be deleted from the queue.
3
Apply the AWS recommendation for SQS-Lambda visibility timeout configuration.
The SQS queue's visibility timeout must be set to at least 6 times the Lambda function's timeout.
This safety margin prevents messages from returning to the queue while the Lambda function is still executing or during retries.

Anahtar Kavram

SQS Visibility Timeout configuration when integrated as an AWS Lambda event source.
Bu soruyu puanla