## Background on idempotent transactions in [[DynamoDB]]
> For the `TransactWriteItem` API, DynamoDB allows you to pass a `ClientRequestToken` parameter with your request. Including this parameter will allow you to ensure your request is _idempotent_ even if submitted multiple times.
> To see how this is helpful, imagine you are doing a `TransactWriteItem`request that includes some write requests to increment an attribute on an item. If you had a network issue where you didn’t know if this operation succeeded or failed, you could be in a bad state. If you assume the operation succeeded but it didn’t, the value of your attribute will be lower than it should be. If you assume the operation failed when it didn’t, you could submit the request again but the value of the attribute will be _higher_ than it should be.
> The `ClientRequestToken` handles this. If you submit a request with the same token and the same parameters within a 10 minute period, DynamoDB will ensure the request is idempotent. If the request succeeded when initially submitted, DynamoDB won’t execute it again. If it failed the first time, DynamoDB will apply the writes in the request.
> The `TransactWriteItems` API is the only DynamoDB API that allows for idempotency, so you could use the `TransactWriteItems` API even with a single item if you have a strong need for idempotency. [^adb1]
[^adb1]: [DynamoDB Transactions: Use Cases and Examples](https://www.alexdebrie.com/posts/dynamodb-transactions/#idempotency-with-transactional-requests) by [[@Alex DeBrie]]
## Operations where you should use a `ClientRequestToken` in a transaction
Certain mutative operations (such as `Put`s, `Delete`s or `Update`s with `SET` commands) are naturally idempotent.
However, the following operations would produce different behaviour if run 2+ times:
- Increments/Decrements to number attributes
- Array/Set pushes
#TODO are there other operations I've missed here?
If your transaction is performing one of these operations listed above, then you may want to use a `ClientRequestToken` to ensure your data doesn't get into a bad state.
---
tags: #DynamoDB