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/features/idempotency.md
+44-79Lines changed: 44 additions & 79 deletions
Original file line number
Diff line number
Diff line change
@@ -50,49 +50,20 @@ classDiagram
50
50
51
51
## Getting started
52
52
53
-
We use Amazon DynamoDB as the default persistence layer in the documentation. If you prefer to use a cache based persistence layer, you can learn more from [this section](#cache-service).
53
+
!!! tip
54
+
Throughout the documentation we use Amazon DynamoDB as the default persistence layer. If you prefer to use a cache based persistence layer, you can learn more in the [cache database](#cache-database) and [`CachePersistenceLayer`](#cachepersistencelayer) sections.
54
55
55
-
### Installation
56
-
57
-
Install the library in your project
56
+
### Amazon DynamoDB
58
57
59
58
```shell
60
59
npm i @aws-lambda-powertools/idempotency @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb
61
60
```
62
61
63
-
While we support Amazon DynamoDB as a persistence layer out of the box, you need to bring your own AWS SDK for JavaScript v3 DynamoDB client.
64
-
65
-
???+ note
66
-
This utility supports **[AWS SDK for JavaScript v3](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/){target="_blank"} only**. If you are using the `nodejs18.x` runtime or newer, the AWS SDK for JavaScript v3 is already installed and you can install only the utility.
67
-
68
-
### IAM Permissions
69
-
70
-
Your Lambda function IAM Role must have `dynamodb:GetItem`, `dynamodb:PutItem`, `dynamodb:UpdateItem` and `dynamodb:DeleteItem` IAM permissions before using this feature. If you're using one of our examples: [AWS Serverless Application Model (SAM)](#required-resources) or [Terraform](#required-resources) the required permissions are already included.
Your Lambda function IAM Role must have `dynamodb:GetItem`, `dynamodb:PutItem`, `dynamodb:UpdateItem` and `dynamodb:DeleteItem` IAM permissions before using this feature. If you're using one of our examples below the required permissions are already included.
90
65
91
-
</div>
92
-
93
-
Before getting started, you need to create a persistent storage layer where the idempotency utility can store its state - your lambda functions will need read and write access to it.
94
-
95
-
#### DynamoDB table
66
+
#### Table configuration
96
67
97
68
Unless you're looking to use an [existing table or customize each attribute](#dynamodbpersistencelayer), you only need the following:
98
69
@@ -101,8 +72,7 @@ Unless you're looking to use an [existing table or customize each attribute](#dy
101
72
| Partition key |`id`| The id of each idempotency record which a combination of `functionName#hashOfPayload`. |
102
73
| TTL attribute name |`expiration`| This can only be configured after your table is created if you're using AWS Console. |
103
74
104
-
???+ tip "Tip: You can share a single state table for all functions"
105
-
You can reuse the same DynamoDB table to store idempotency state. We add the Lambda function name in addition to the idempotency key as a hash key.
75
+
You **can** use a single DynamoDB table for all functions using this utility. We use the function name in addition to the idempotency key as a hash key.
106
76
107
77
=== "AWS Cloud Development Kit (CDK) example"
108
78
@@ -122,50 +92,51 @@ Unless you're looking to use an [existing table or customize each attribute](#dy
???+ warning "Warning: Large responses with DynamoDB persistence layer"
126
-
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"}.
95
+
##### Limitations
127
96
128
-
Larger items cannot be written to DynamoDB and will cause exceptions.
97
+
***DynamoDB restricts [item sizes to 400KB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html#limits-items)**. This means that your idempotent function's response must be smaller than 400KB, otherwise your function will fail. Consider using the [cache persistence layer](#cache-database) if you need to store larger responses.
98
+
***Expect 2 WCUs per non-idempotent call**. During the first invocation, we use `PutItem` for locking and `UpdateItem` for completion. Consider reviewing [DynamoDB pricing documentation](https://aws.amazon.com/dynamodb/pricing/) to estimate cost.
99
+
***Expect 1 RCU per idempotent calls**. On subsequent invocations, we use `PutItem` to optimistically attempt to lock the record using `ConditionExpression` and `ReturnValuesOnConditionCheckFailure` to return the record if it exists. This is a single read operation.
129
100
130
-
???+ info "Info: DynamoDB"
131
-
Each function invocation will make only 1 request to DynamoDB by using DynamoDB's [conditional expressions](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ConditionExpressions.html){target="_blank"} to ensure that we don't overwrite existing records,
132
-
and [ReturnValuesOnConditionCheckFailure](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html#DDB-PutItem-request-ReturnValuesOnConditionCheckFailure){target="_blank"} to return the record if it exists.
133
-
See [AWS Blog post on handling conditional write errors](https://aws.amazon.com/blogs/database/handle-conditional-write-errors-in-high-concurrency-scenarios-with-amazon-dynamodb/) for more details.
134
-
For retried invocations, you will see 1WCU and 1RCU.
135
-
Review the [DynamoDB pricing documentation](https://aws.amazon.com/dynamodb/pricing/){target="_blank"} to estimate the cost.
101
+
### Cache database
136
102
137
-
#### Cache service
103
+
Depending on the persistence layer you want to use, install the library and the corresponding peer dependencies.
138
104
139
-
We recommend starting with a managed cache service, such as [Amazon ElastiCache for Valkey and for Redis OSS](https://aws.amazon.com/elasticache/redis/){target="_blank"} or [Amazon MemoryDB](https://aws.amazon.com/memorydb/){target="_blank"}.
105
+
=== "Valkey"
106
+
```shell
107
+
npm i @aws-lambda-powertools/idempotency@valkey/valkey-glide
108
+
```
140
109
141
-
In both services, you'll need to configure [VPC access](https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html){target="_blank"} to your AWS Lambda.
110
+
=== "Redis OSS"
111
+
```shell
112
+
npm i @aws-lambda-powertools/idempotency@redis/client
113
+
```
142
114
143
-
##### Cache IaC examples
115
+
We recommend starting with a managed cache service, such as [Amazon ElastiCache for Valkey and for Redis OSS](https://aws.amazon.com/elasticache/redis/){target="_blank"} or [Amazon MemoryDB](https://aws.amazon.com/memorydb/){target="_blank"}.
144
116
145
-
!!! tip inline end "Prefer AWS Console/CLI?"
117
+
In both services, you'll need to configure [VPC access](https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html){target="_blank"} to your AWS Lambda and permissions for writing and reading from the cache.
146
118
147
-
Follow the official tutorials for [Amazon ElastiCache for Redis](https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/LambdaRedis.html){target="_blank"} or [Amazon MemoryDB for Redis](https://aws.amazon.com/blogs/database/access-amazon-memorydb-for-redis-from-aws-lambda/){target="_blank"}
1. Replace the Security Group ID and Subnet ID to match your VPC settings.
137
+
1. You can use the same template for Redis OSS, just replace the engine to `redis`.
165
138
2. Replace the Security Group ID and Subnet ID to match your VPC settings.
166
-
167
-
168
-
Once setup, you can find a quick start example for using a cache in [the persistent layers section](#cachepersistencelayer).
139
+
3. Replace the Security Group ID and Subnet ID to match your VPC settings.
169
140
170
141
### MakeIdempotent function wrapper
171
142
@@ -630,42 +601,36 @@ When using DynamoDB as a persistence layer, you can alter the attribute names by
630
601
631
602
The `CachePersistenceLayer` enables you to use Valkey, Redis OSS, or any Redis-compatible cache as the persistence layer for idempotency state. You need to provide your own cache client.
632
603
633
-
We recommend using [valkey-glide](https://valkey.io/valkey-glide/#__tabbed_2_2){target="_blank"} for Valkey or [redis-client](https://www.npmjs.com/package/@redis/client){target="_blank"} for Redis. However, any Redis-compatible client can be used.
604
+
We recommend using [`@valkey/valkey-glide`](https://www.npmjs.com/package/@valkey/valkey-glide){target="_blank"} for Valkey or [`@redis/client`](https://www.npmjs.com/package/@redis/client){target="_blank"} for Redis. However, any Redis OSS-compatible client should work.
634
605
635
606
???+ info
636
607
Make sure your cache client is configured and connected before using it with `CachePersistenceLayer`.
0 commit comments