2020-11-07 - DynamoDB Conditional Expressions to Protect Against Race Conditions
Overview
If we query a record from the database, perform logic, then update that record, there is the chance other processes made changes to the record in the time between requesting the record and updating it. To protect against this we can use DynamoDB Conditional Expressions when performing the database update.
Conditional Expressions will check the existing value in the data, and only perform the update if the value matches what was expected. The condition could be applied to a status field, timestamp field, or a special field only for protecting against race conditions.
If the field is not as expected we could query the record again and re-try the activity with the updated data.
Understand DynamoDB locks the record during the conditional update so we can be confident there is no split second timing difference, allowing a race condition to occur.
Conditional Expression on nested data
Can be performed on data that is nested deep within the record being updated