Soru

Zorluk: Çok zorMessage-Based Integration using Amazon SQS and SNS

A financial services company uses an Amazon SNS topic to publish transaction notifications. One of the subscribers is an Amazon SQS standard queue that triggers an AWS Lambda function to write transaction details to an Amazon DynamoDB table. The Lambda function is configured with a timeout of 2525 seconds. The event source mapping is configured with a Batch Size of 2020 messages and a Maximum Batching Window (`MaximumBatchingWindowInSeconds`) of 1515 seconds. During high-traffic periods, the DynamoDB table experiences temporary write throttling, which in turn causes the Lambda function to be throttled. The developer observes that many transactions are processed and written to the database multiple times, creating duplicate entries. The current Visibility Timeout of the SQS queue is set to 4545 seconds. Which of the following is the root cause of these duplicate entries, and what is the correct configuration to resolve the issue?

  1. The queue's Visibility Timeout is too short, allowing messages to become visible to other function instances while Lambda retries throttled invocations. The developer should increase the SQS queue's Visibility Timeout to at least 165165 seconds.Cevap
  2. B
    The Lambda function is experiencing execution context reuse and timing out due to accumulated stateful connection pools. The developer should increase the Lambda timeout to 165165 seconds and configure the function to re-initialize the DynamoDB client inside the handler, keeping the queue's Visibility Timeout at 4545 seconds.
  3. C
    The Lambda function's DynamoDB SDK client is initialized with hardcoded AWS access keys that expire periodically. When credentials expire, the SDK fails to write and Lambda retries, causing duplicate processing. The developer should remove the hardcoded keys, use the Lambda execution role, and decrease the SQS Visibility Timeout to 2525 seconds to match the Lambda timeout.
  4. D
    The Lambda function is deployed in a private subnet without a NAT Gateway or VPC Endpoint to access DynamoDB, causing the database writes to fail silently and trigger retries. The developer should deploy a NAT Gateway in the public subnet and set the queue's Visibility Timeout to 4040 seconds, which is the sum of the Lambda timeout and the batching window.

Cevap

The queue's Visibility Timeout is too short, allowing messages to become visible to other function instances while Lambda retries throttled invocations. The developer should increase the SQS queue's Visibility Timeout to at least 165165 seconds.
The correct configuration is to increase the SQS queue's Visibility Timeout to at least 165165 seconds. Under the AWS Lambda integration model for SQS, the visibility timeout should be configured to at least 6 times the Lambda function's timeout (6×25=1506 \times 25 = 150 seconds) plus the maximum batching window (1515 seconds), resulting in 165165 seconds. This configuration provides a sufficient buffer for the Lambda service to retry throttled invocations before the SQS messages become visible to other consumer instances.

Adım Adım Çözüm

1
Analyze the relationship between SQS Visibility Timeout and Lambda Timeout.
Identify that AWS recommends setting the SQS Visibility Timeout to at least 6 times the Lambda Timeout to allow for retries in case of function throttling.
Throttled Lambda invocations are retried automatically, and the visibility timeout must remain in effect during these retries to prevent other instances from processing the same messages.
2
Incorporate the Maximum Batching Window into the calculation.
The minimum Visibility Timeout is calculated as: Visibility Timeout6×Lambda Timeout+MaximumBatchingWindowInSeconds\text{Visibility Timeout} \geq 6 \times \text{Lambda Timeout} + \text{MaximumBatchingWindowInSeconds}.
Messages can spend up to the maximum batching window length in the queue before Lambda starts processing them, which reduces the active visibility period.
3
Perform the calculation using the given values (2525 seconds Lambda timeout, 1515 seconds batching window).
6×25+15=150+15=1656 \times 25 + 15 = 150 + 15 = 165 seconds.
Determines the exact threshold required to solve the duplicate processing issue.

Anahtar Kavram

Amazon SQS Visibility Timeout calculation for AWS Lambda event source mapping with batching windows
Tahmini Süre:3m 0s
Bu soruyu puanla