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/utilities/idempotency.md
+27-38
Original file line number
Diff line number
Diff line change
@@ -77,31 +77,16 @@ If you're not [changing the default configuration for the DynamoDB persistence l
77
77
???+ tip "Tip: You can share a single state table for all functions"
78
78
You can reuse the same DynamoDB table to store idempotency state. We add `module_name` and [qualified name for classes and functions](https://peps.python.org/pep-3155/){target="_blank"} in addition to the idempotency key as a hash key.
79
79
80
-
```yaml hl_lines="5-13 21-23" title="AWS Serverless Application Model (SAM) example"
81
-
Resources:
82
-
IdempotencyTable:
83
-
Type: AWS::DynamoDB::Table
84
-
Properties:
85
-
AttributeDefinitions:
86
-
- AttributeName: id
87
-
AttributeType: S
88
-
KeySchema:
89
-
- AttributeName: id
90
-
KeyType: HASH
91
-
TimeToLiveSpecification:
92
-
AttributeName: expiration
93
-
Enabled: true
94
-
BillingMode: PAY_PER_REQUEST
95
-
96
-
HelloWorldFunction:
97
-
Type: AWS::Serverless::Function
98
-
Properties:
99
-
Runtime: python3.9
100
-
...
101
-
Policies:
102
-
- DynamoDBCrudPolicy:
103
-
TableName: !Ref IdempotencyTable
104
-
```
80
+
=== "sam.yaml"
81
+
82
+
```yaml hl_lines="6-14 24-31" title="AWS Serverless Application Model (SAM) example"
???+ warning "Warning: Large responses with DynamoDB persistence layer"
107
92
When using this utility with DynamoDB, your function's responses must be [smaller than 400KB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html#limits-items){target="_blank"}.
@@ -148,7 +133,7 @@ You can quickly start by initializing the `DynamoDBPersistenceLayer` class and u
148
133
149
134
=== "Example event"
150
135
151
-
```json
136
+
```json
152
137
{
153
138
"username": "xyz",
154
139
"product_id": "123456789"
@@ -334,10 +319,12 @@ In this example, we have a Lambda handler that creates a payment for a user subs
334
319
335
320
Imagine the function executes successfully, but the client never receives the response due to a connection issue. It is safe to retry in this instance, as the idempotent decorator will return a previously saved response.
336
321
337
-
**What we want here** is to instruct Idempotency to use `user` and `product_id` fields from our incoming payload as our idempotency key. If we were to treat the entire request as our idempotency key, a simple HTTP header change would cause our customer to be charged twice.
322
+
**What we want here** is to instruct Idempotency to use `user` and `product_id` fields from our incoming payload as our idempotency key.
323
+
If we were to treat the entire request as our idempotency key, a simple HTTP header change would cause our customer to be charged twice.
338
324
339
325
???+ tip "Deserializing JSON strings in payloads for increased accuracy."
340
-
The payload extracted by the `event_key_jmespath` is treated as a string by default. This means there could be differences in whitespace even when the JSON payload itself is identical.
326
+
The payload extracted by the `event_key_jmespath` is treated as a string by default.
327
+
This means there could be differences in whitespace even when the JSON payload itself is identical.
341
328
342
329
To alter this behaviour, we can use the [JMESPath built-in function](jmespath_functions.md#powertools_json-function){target="_blank"} `powertools_json()` to treat the payload as a JSON object (dict) rather than a string.
343
330
@@ -410,15 +397,17 @@ Imagine the function executes successfully, but the client never receives the re
410
397
???+ note
411
398
This is automatically done when you decorate your Lambda handler with [@idempotent decorator](#idempotent-decorator).
412
399
413
-
To prevent against extended failed retries when a [Lambda function times out](https://aws.amazon.com/premiumsupport/knowledge-center/lambda-verify-invocation-timeouts/){target="_blank"}, Powertools for AWS Lambda (Python) calculates and includes the remaining invocation available time as part of the idempotency record.
400
+
To prevent against extended failed retries when a [Lambda function times out](https://aws.amazon.com/premiumsupport/knowledge-center/lambda-verify-invocation-timeouts/){target="_blank"},
401
+
Powertools for AWS Lambda (Python) calculates and includes the remaining invocation available time as part of the idempotency record.
414
402
415
403
???+ example
416
404
If a second invocation happens **after** this timestamp, and the record is marked as `INPROGRESS`, we will execute the invocation again as if it was in the `EXPIRED` state (e.g, `expire_seconds` field elapsed).
417
405
418
406
This means that if an invocation expired during execution, it will be quickly executed again on the next retry.
419
407
420
408
???+ important
421
-
If you are only using the [@idempotent_function decorator](#idempotent_function-decorator) to guard isolated parts of your code, you must use `register_lambda_context` available in the [idempotency config object](#customizing-the-default-behavior) to benefit from this protection.
409
+
If you are only using the [@idempotent_function decorator](#idempotent_function-decorator) to guard isolated parts of your code,
410
+
you must use `register_lambda_context` available in the [idempotency config object](#customizing-the-default-behavior) to benefit from this protection.
422
411
423
412
Here is an example on how you register the Lambda context in your handler:
424
413
@@ -698,14 +687,14 @@ When using DynamoDB as a persistence layer, you can alter the attribute names by
698
687
699
688
Idempotent decorator can be further configured with**`IdempotencyConfig`**as seen in the previous example. These are the available options for further configuration
|**event_key_jmespath**|`""`| JMESPath expression to extract the idempotency key from the event record using [built-in functions](/utilities/jmespath_functions){target="_blank"} |
704
-
|**payload_validation_jmespath**|`""`| JMESPath expression to validate whether certain parameters have changed in the event while the event payload |
705
-
|**raise_on_no_idempotency_key**|`False`| Raise exception if no idempotency key was found in the request |
706
-
|**expires_after_seconds**|3600| The number of seconds to wait before a record is expired |
707
-
|**use_local_cache**|`False`| Whether to locally cache idempotency results |
708
-
|**local_cache_max_items**|256| Max number of items to store in local cache |
|**event_key_jmespath**|`""`| JMESPath expression to extract the idempotency key from the event record using [built-in functions](/utilities/jmespath_functions){target="_blank"} |
693
+
|**payload_validation_jmespath**|`""`| JMESPath expression to validate whether certain parameters have changed in the event while the event payload |
694
+
|**raise_on_no_idempotency_key**|`False`| Raise exception if no idempotency key was found in the request |
695
+
|**expires_after_seconds**|3600| The number of seconds to wait before a record is expired |
696
+
|**use_local_cache**|`False`| Whether to locally cache idempotency results |
697
+
|**local_cache_max_items**|256| Max number of items to store in local cache |
709
698
|**hash_function**|`md5`| Function to use for calculating hashes, as provided by [hashlib](https://docs.python.org/3/library/hashlib.html){target="_blank"} in the standard library. |
710
699
711
700
### Handling concurrent executions with the same payload
0 commit comments