Soru

Zorluk: OrtaMessage-Based Integration using Amazon SQS and SNS

A developer is building a smart agriculture application. An Amazon SNS topic receives soil moisture alerts, which are subscribed to by an Amazon SQS queue. An AWS Lambda function is configured to process messages from this SQS queue. The Lambda function has a timeout of 30 seconds. The SQS queue has a Default Visibility Timeout of 15 seconds. During testing, the developer notices that some alerts are being processed multiple times, even though the Lambda function executes successfully in 20 seconds. Which action should the developer take to prevent these duplicate invocations?

  1. Increase the visibility timeout of the SQS queue to at least 180 seconds.Cevap
  2. B
    Decrease the SQS queue's visibility timeout to 5 seconds.
  3. C
    Increase the Lambda function's timeout to 120 seconds while keeping the SQS queue's visibility timeout at 15 seconds.
  4. D
    Modify the Lambda function's SDK client configuration to use hardcoded AWS credentials for deleting the processed messages.

Cevap

Increase the visibility timeout of the SQS queue to at least 180 seconds.
The correct action is to increase the SQS queue's visibility timeout to at least 180 seconds. AWS recommends setting the visibility timeout of an SQS queue to at least 6 times the timeout of the subscribing Lambda function (6 * 30 seconds = 180 seconds). This configuration ensures that the message is not visible to other consumers while the function is running or retrying.

Adım Adım Çözüm

1
Analyze the execution time of the Lambda consumer relative to the SQS visibility timeout.
The Lambda function takes 20 seconds to process a message, but the SQS queue's visibility timeout is only 15 seconds.
When a message is pulled from SQS, it becomes invisible for 15 seconds. Since the function takes 20 seconds to complete, the message becomes visible again after 15 seconds, allowing another execution context to retrieve it and cause duplicate processing.
2
Apply AWS best practices for SQS and Lambda integrations.
The visibility timeout of the source SQS queue must be set to at least 6 times the timeout of the Lambda function.
Setting the visibility timeout to at least 6 times the Lambda timeout (6 * 30 seconds = 180 seconds) ensures that if the function is throttled or needs to retry, other consumers do not pull the message concurrently.

Anahtar Kavram

AWS Lambda integration with Amazon SQS requires setting the SQS queue's visibility timeout to at least 6 times the Lambda function's timeout to prevent duplicate message processing during execution and retries.
Bu soruyu puanla