AWS Lambda SQS 触发器:未处理部分批次失败,处理失败后所有消息再次变为可见
AWS Lambda SQS trigger: partial batch failure not handled, all messages become visible again after processing failure
ID: cloud/aws-lambda-sqs-batch-partial-failure
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| AWS Lambda: runtime >= Node.js 18.x | active | — | — | — |
| AWS SDK: >= 3.300.0 | active | — | — | — |
| SQS: standard queue | active | — | — | — |
根因分析
当使用 SQS 作为 Lambda 触发器进行批处理时,如果 Lambda 函数未能处理某些消息但未使用 'ReportBatchItemFailures' 报告特定失败,则整个批次将被重试,导致成功处理的消息被重复处理。
English
When using SQS as a Lambda trigger with batch processing, if the Lambda function fails to process some messages but doesn't use 'ReportBatchItemFailures' to report specific failures, the entire batch is retried, causing duplicate processing of successful messages.
官方文档
https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#services-sqs-batchfailures解决方案
-
Implement 'ReportBatchItemFailures' in the Lambda function response. Example in Node.js: return { batchItemFailures: [ { itemIdentifier: failedMessage.messageId } ] }. Configure the event source mapping with 'FunctionResponseTypes: ["ReportBatchItemFailures"]'. -
Use a dead-letter queue (DLQ) on the SQS source to capture failed messages after max retries, and process them separately.
无效尝试
常见但无效的做法:
-
70% 失败
Reduces throughput significantly; doesn't solve the root cause of failure reporting, and if any message fails, the single message is still retried indefinitely.
-
90% 失败
Silently swallows errors, leading to data loss and no visibility into processing failures.
-
50% 失败
Doesn't address the partial failure reporting; successful messages may still be reprocessed after the timeout expires.