You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/content/index.mdx
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@ sam init --location https://github.com/aws-samples/cookiecutter-aws-sam-python
29
29
*[Metrics](./core/metrics) - Custom Metrics created asynchronously via CloudWatch Embedded Metric Format (EMF)
30
30
*[Bring your own middleware](./utilities/middleware_factory) - Decorator factory to create your own middleware to run logic before, and after each Lambda invocation
31
31
*[Parameters utility](./utilities/parameters) - Retrieve parameter values from AWS Systems Manager Parameter Store, AWS Secrets Manager, or Amazon DynamoDB, and cache them for a specific amount of time
32
-
*[Batch utility](./utilities/batch) - Batch processing for AWS SQS, with a middleware allow custom record handling
Copy file name to clipboardExpand all lines: docs/content/utilities/batch.mdx
+69-34Lines changed: 69 additions & 34 deletions
Original file line number
Diff line number
Diff line change
@@ -5,14 +5,15 @@ description: Utility
5
5
6
6
importNotefrom"../../src/components/Note"
7
7
8
-
The batch utility provides an abstraction to process a batch event. Useful for lambda integrations with [AWS SQS](https://aws.amazon.com/sqs/), [AWS Kinesis](https://aws.amazon.com/kinesis/) and [AWS DynamoDB Streams](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.html).
9
-
It also provides base classes (`BaseProcessor`, `BasePartialProcessor`) allowing you to create your **own** batch processor.
8
+
One very attractive feature of Lambda functions is the ability to integrate them with a plethora of internal and external [event sources][1]. Some of these event providers allows a feature called "Batch processing" in which [predefined number][2] of events is sent to lambda function at once.
9
+
10
+
The proposed batch utility aims to provide an abstraction to process batch events, providing base classes (`BaseProcessor`, `BasePartialProcessor`) allowing you to create your **own** batch processor.
11
+
It also provides a useful implementation to handle partial batch failures from the SQS provider.
10
12
11
13
**Key Features**
12
14
13
-
* Run batch processing logic with a clean interface;
14
-
* Middleware and context to handle a batch event;
15
-
* Removal of successful messages for [AWS SQS](https://aws.amazon.com/sqs/) batch - in case of partial failure.
15
+
* Removal of successful messages for [AWS SQS](https://aws.amazon.com/sqs/) batch - in case of partial failure;
16
+
* Build your own batch processor using the base classes.
A special batch processor which aims to `clean` your SQS:Queue if one or more (not all) records of the batch fails.
28
-
A batch's partial failure sends back all the records to the queue, reprocessing this batch until all records succed.
29
-
This processor exists to improve performance in such cases, deleting successful messages of a batch with partial failure.
28
+
SQS integration with Lambda is one of the most well established ones and pretty useful when building asynchronous applications. One common approach to maximize the performance of this integration is to enable the batch processing feature, resulting in higher throughput with less invocations.
29
+
30
+
As any function call, you may face errors during execution, in one or more records belonging to a batch. SQS's native behavior is to redrive the **whole** batch to the queue again, reprocessing all of them again, including successful ones. This cycle can happen multiple times depending on your [configuration][3], until the whole batch succeeds or the maximum number of attempts is reached. Your application may face some problems with such behavior, especially if there's no idempotency.
31
+
32
+
A *naive* approach to solving this problem is to delete successful records from the queue before redriving's phase. The `PartialSQSProcessor` class offers this solution both as context manager and middleware, removing all successful messages from the queue case one or more failures ocurred during lambda's execution. Two examples are provided below, displaying the behavior of this class.
33
+
34
+
**Examples:**
35
+
36
+
```python:title=context_manager.py
37
+
from aws_lambda_powertools.utilities.batch import batch_processor, PartialSQSProcessor
38
+
39
+
defrecord_handler(record):
40
+
return record["body"]
41
+
42
+
deflambda_handler(event, context):
43
+
records = event["Records"]
44
+
45
+
# highlight-start
46
+
with processor(records, record_handler):
47
+
result = processor.process()
48
+
# highlight-end
30
49
31
-
### Middleware
50
+
return result
51
+
```
32
52
33
-
```python:title=app.py
53
+
```python:title=middleware.py
34
54
from aws_lambda_powertools.utilities.batch import batch_processor, PartialSQSProcessor
0 commit comments