The whole reason why DynamoDB is fast and scalable is based on the fact that it is eventually consistent. But at the same time, it comes with this ConsistentRead option for operations like get, batchGet, and query which helps you make sure that the data you are reading is the latest one.
My question is about the update operation. First of all, it does not have the ConsistentRead option (one reason would be, update is not a read!). But at the same time, you can update a record in an atomic manner with ConditionExpression, like this:
await docClient.update({
TableName: 'SomeTable',
Key: {id},
UpdateExpression: "set #status = :new_status",
ConditionExpression: '#status = :old_status',
ExpressionAttributeNames: {
"#status": "status",
},
ExpressionAttributeValues: {
":old_status": "available",
":new_status": "done",
},
}).promise()
This will make sure that at the time of update, the old value is available and if it isn't, the operation will fail with an exception thrown. So, in a sense, you can say that update is strongly consistent.
But my question is about a scenario in which you need to make sure the record exists. Let's say that you have one function which inserts a record. And another one that updates the same record (given its id). My concern is what if by the time the update operation is executed, because of eventually consistency of DynamoDB, there's no record matched and the update fails. As said before, the update operation does not come with a ConsistentRead option to make it strongly consistent.
Is this a valid concern? Is there anything I can do to help this?