databaseamazon-dynamodbidempotent

AWS DynamoDB Idempotent on Conditional Writes


The documentation on AWS DynamoDB writes that:

Conditional write idempotence Conditional writes can be idempotent if the conditional check is on the same attribute that is being updated. This means that DynamoDB performs a given write request only if certain attribute values in the item match what you expect them to be at the time of the request.

For example, suppose that you issue an UpdateItem request to increase the Price of an item by 3, but only if the Price is currently 20. After you send the request, but before you get the results back, a network error occurs, and you don't know whether the request was successful. Because this conditional write is idempotent, you can retry the same UpdateItem request, and DynamoDB updates the item only if the Price is currently 20.

The last sentence is confusing for me. What if during the error, another unconditional request was sent to server and the Price has been updated, for example, to increase the Price by 1. So, the Price increases from 20 to 21. Also suppose that the unconditional update request is sent AFTER the conditional request to increase the Price by 3 is issued.

In such case, as I understand the documentation says that the retry conditional request should not increase the Price to 23 because the current Price is now 21 NOT 20. However, if there's no error then we should expect the Price to be 24 and not 23.

So, suppose my understanding is correct, we would have two different end results regarding the same set of operations, i.e. one is conditional and one is unconditional, under two scenarios, one without errors and one with error?

Clear my confusion of the idempotent operation on AWS DynamoDB.


Solution

  • But you get your desired outcome regardless.

    You wanted to increment by 1 but only if the value is currently 20. You succeed if it is, but fail if it isn't, regardless of retries.

    The fact that you send a non conditional update to mutate the item changes the scenario, but in an expected way.

    Your conditional check request was still idempotent that it would only ever execute if the Price is 20.