Reputation: 1
I need to update quantity attribute of a row in dynamo using a lambda function, if two request comes at same time, they will get same quantity. After update the updated will not be the expected value. Let's say current qty is 4. If two update come with qty 2 at same time then updated value 4+2 for each request. But what the expected qty should be 4+2+2.
I could not find out any solution for this.
Upvotes: 0
Views: 69
Reputation: 19893
DynamoDB writes are all serialized. Meaning that if you have 2 concurrent writes which update a value, the end state of that value will consider all updates that happened.
Consider an item which has a counter with a value of 4: count=4
.
Not two concurrent processes update this value to increase it by the value of 2 each: count+=2
The outcome would be that the value of count would be 6 when all processes have completed: count=6
.
However, do be aware that this is not idempotent, meaning that if your lambda happened to retry for any given reason your counter could even up over value.
To overcome this you can either use conditional checks on the count attribute or implement versioning:
Upvotes: 2