I am trying to update a counter in my simple database (one key, one numeric value). Atomic counters seem the natural solutions, but I need thread safe updates, i.e. I need to make sure that updates do not interfere with each other.
This answer seem to suggest that I need to double check that the data was not modified by another update before performing the actual write.
My question is, how do I reference the current value in the db in a ConditionExpression
?
Assuming table
is dynamodb.Table
object (Python API), this is the code I wrote for update_item
:
table.update_item(
Key={
'key': my_key
},
UpdateExpression="set my_value = my_value + :inc",
ConditionExpression="my_value = ?????" # should be value in the db now
ExpressionAttributeValues={
':inc': my_increment,
},
ReturnValues="UPDATED_NEW"
)