Soru

Zorluk: OrtaMessage-Based Integration using Amazon SQS and SNS

A developer is building a document conversion system that uses an Amazon SQS standard queue to trigger an AWS Lambda function. The Lambda function processes documents in batches of 1010. During testing, the developer observes that if a single document in a batch fails to process due to a timeout, all 1010 documents in that batch are retried, causing duplicate processing for the successfully completed documents. Which configuration change should the developer make to ensure only the failed documents are reprocessed?

  1. A
    Decrease the SQS queue's visibility timeout to be lower than the Lambda function's timeout so that messages are returned to the queue faster.
  2. B
    Store the IDs of successfully processed documents in a global variable in the Lambda function's execution context to prevent them from being reprocessed when the function is reused.
  3. Configure the event source mapping with ReportBatchItemFailures in the FunctionResponseTypes, and update the Lambda function to return the IDs of the failed messages in the response payload.Cevap
  4. D
    Initialize the Amazon SQS client within the Lambda function handler using hardcoded long-term AWS credentials of a user with SQS permissions, and manually delete each message as it finishes processing.

Cevap

Configure the event source mapping with ReportBatchItemFailures in the FunctionResponseTypes, and update the Lambda function to return the IDs of the failed messages in the response payload.
By enabling ReportBatchItemFailures in the FunctionResponseTypes of the Lambda event source mapping, the Lambda service understands when only a subset of messages in a batch fails. The function returns a JSON payload containing the identifiers of the failed messages, allowing SQS to make only those specific messages visible again in the queue while successfully processed messages are deleted from the queue automatically.

Adım Adım Çözüm

1
Identify the cause of batch reprocessing in SQS-Lambda integrations.
When a Lambda function returns an error or times out, the default behavior of the SQS event source mapping is to treat the entire batch as failed, returning all messages to the queue.
This is necessary to understand why successful messages are being reprocessed.
2
Select the native AWS mechanism for handling partial batch failures.
The ReportBatchItemFailures option allows the function to report specific message failures rather than failing the entire batch.
This avoids duplicate processing by letting the Lambda service delete the successfully processed messages.
3
Configure the Lambda function response format.
Return a JSON structure containing an array of object structures with the itemIdentifier key pointing to the failed message's MessageId.
This is the contract required by AWS Lambda when ReportBatchItemFailures is enabled.

Anahtar Kavram

Handling partial batch failures in SQS-Lambda event source mappings using ReportBatchItemFailures
Bu soruyu puanla