Best practices for implementing partial batch responses - AWS Prescriptive Guidance

Best practices for implementing partial batch responses

This section provides best practices for configuring partial batch responses for Amazon SQS event sources.

  • Configure a dead-letter queue to avoid creating a snowball anti-pattern in your serverless application’s architecture. For more information, see the Avoiding snowball anti-patterns section.

  • Configure your Lambda function event source mapping to make only failed messages visible. To do this, add ReportBatchItemFailures to the FunctionResponseTypes list when you configure event source mapping. Your Lambda function, when invoked by SQS, should implement partial batch responses. Consider using the Powertools for AWS Lambda Batch Processing utility, which processes SQS messages with built-in partial-batch support.

Batch processing

Powertools for AWS Lambda Batch Processing Utility

Idempotency

Powertools for AWS Lambda Idempotency Utility

Metrics

  • To incorporate business metrics in your function to track job details and failed jobs, consider using aws-embedded-metrics or the Powertools for AWS Lambda Metrics Utility, which emits operational and business metrics while processing SQS events in embedded metric format.

Powertools for AWS Lambda Metrics Utility

  • If you use a First-In-First-Out (FIFO) queue, your function should stop processing messages after the first failure and return all failed and unprocessed messages in batchItemFailures. This helps preserve the message order of your queue.

Note

To track the overall performance of an application that uses partial-batch processing, code-level performance tracking is required. After batch processing is configured, Lambda function invocations usually succeed regardless of the processing result.

Avoiding snowball anti-patterns

Lambda and Amazon SQS can’t control the messages that upstream microservices write to an SQS queue. If there are messages that can’t be processed, Lambda returns those unprocessed messages to the source SQS queue unless a separate dead-letter queue is configured. Unprocessed messages are then retried by the Lambda function. If no dead-letter queue exists, the number of unprocessed messages returned to the Amazon SQS queue eventually outnumbers the queue's valid messages.

This type of snowball anti-pattern—where each successive Lambda invocation worsens the problem—can cause the following issues:

  • Poor user experience because the jobs take longer than usual to process, or they don't process at all

  • Increased cost proportional to the exponentially increasing number of messages in the Amazon SQS queue and message retries

  • Reduced Lambda computing capacity for the application or AWS account if the function doesn’t have a limit on its invocation requests

To avoid creating a snowball anti-pattern when configuring partial batch responses, it’s best to also create a dead-letter queue. This separate queue can store messages that aren’t processed successfully and help you better manage the lifecycle of your application’s unprocessed messages.

For more information, see Configure a dead-letter queue using the Amazon SQS console in the Amazon SQS Developer Guide.