|
1 | 1 | # AWS.Lambda.Powertools.BatchProcessing
|
2 |
| -... |
| 2 | + |
| 3 | +The batch processing utility handles partial failures when processing batches from Amazon SQS, Amazon Kinesis Data Streams, and Amazon DynamoDB Streams. |
3 | 4 |
|
4 | 5 | ## Key features
|
5 |
| -... |
| 6 | + |
| 7 | +* Reports batch item failures to reduce number of retries for a record upon errors |
| 8 | +* Simple interface to process each batch record |
| 9 | +* Bring your own batch processor |
| 10 | +* Parallel processing |
| 11 | + |
| 12 | +## Background |
| 13 | + |
| 14 | +When using SQS, Kinesis Data Streams, or DynamoDB Streams as a Lambda event source, your Lambda functions are triggered with a batch of messages. |
| 15 | + |
| 16 | +If your function fails to process any message from the batch, the entire batch returns to your queue or stream. This same batch is then retried until either condition happens first: a) your Lambda function returns a successful response, b) record reaches maximum retry attempts, or c) when records expire. |
| 17 | + |
| 18 | +This behavior changes when you enable Report Batch Item Failures feature in your Lambda function event source configuration: |
| 19 | + |
| 20 | +* [SQS queues](https://docs.powertools.aws.dev/lambda/dotnet/utilities/batch-processing/#sqs-standard). Only messages reported as failure will return to the queue for a retry, while successful ones will be deleted. |
| 21 | +* [Kinesis data streams](https://docs.powertools.aws.dev/lambda/dotnet/utilities/batch-processing/#kinesis-and-dynamodb-streams) and [DynamoDB streams](https://docs.powertools.aws.dev/lambda/dotnet/utilities/batch-processing/#kinesis-and-dynamodb-streams). Single reported failure will use its sequence number as the stream checkpoint. Multiple reported failures will use the lowest sequence number as checkpoint. |
6 | 22 |
|
7 | 23 | ## Read the docs
|
8 |
| -... |
| 24 | + |
| 25 | +For a full list of features go to [docs.powertools.aws.dev/lambda/dotnet/utilities/batch-processing/](https://docs.powertools.aws.dev/lambda/dotnet/utilities/batch-processing/) |
| 26 | + |
| 27 | +GitHub: https://github.com/aws-powertools/powertools-lambda-dotnet/ |
9 | 28 |
|
10 | 29 | ## Sample Function
|
11 |
| -... |
12 | 30 |
|
13 |
| -## Sample output |
14 |
| -... |
| 31 | +View the full example here: [github.com/aws-powertools/powertools-lambda-dotnet/tree/develop/examples/BatchProcessing](https://github.com/aws-powertools/powertools-lambda-dotnet/tree/develop/examples/BatchProcessing) |
| 32 | + |
| 33 | +```csharp |
| 34 | +[BatchProcessor(RecordHandler = typeof(CustomSqsRecordHandler))] |
| 35 | +public BatchItemFailuresResponse HandlerUsingAttribute(SQSEvent _) |
| 36 | +{ |
| 37 | + return SqsBatchProcessor.Result.BatchItemFailuresResponse; |
| 38 | +} |
| 39 | + |
| 40 | +public class CustomSqsRecordHandler : ISqsRecordHandler |
| 41 | +{ |
| 42 | + public async Task<RecordHandlerResult> HandleAsync(SQSEvent.SQSMessage record, CancellationToken cancellationToken) |
| 43 | + { |
| 44 | + /* |
| 45 | + Your business logic. |
| 46 | + If an exception is thrown, the item will be marked as a partial batch item failure. |
| 47 | + */ |
| 48 | + |
| 49 | + var product = JsonSerializer.Deserialize<JsonElement>(record.Body); |
| 50 | + |
| 51 | + if (product.GetProperty("Id").GetInt16() == 4) |
| 52 | + { |
| 53 | + throw new ArgumentException("Error on 4"); |
| 54 | + } |
| 55 | + |
| 56 | + return await Task.FromResult(RecordHandlerResult.None); |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
0 commit comments