Interview Prep/AWS/RDS, Aurora and DynamoDB

Updates fail with `ConditionalCheckFailedException: The conditional request failed`. Is it a bug?

IntermediateTroubleshootingRDS, Aurora and DynamoDB
Answer

Likely causes → Usually it is working as designed: the ConditionExpression (for example, an optimistic lock on version, or attribute_not_exists(pk)) was false because another writer changed the item first, or the item already exists.

Diagnose →

aws dynamodb get-item --table-name orders --key '{"orderId":{"S":"o-1001"}}' --consistent-read
aws dynamodb update-item --table-name orders --key '{"orderId":{"S":"o-1001"}}' \
  --update-expression 'SET #s=:s, version=version+:one' --condition-expression 'version=:v' \
  --expression-attribute-names '{"#s":"status"}' \
  --expression-attribute-values '{":s":{"S":"PAID"},":v":{"N":"3"},":one":{"N":"1"}}' \
  --return-values-on-condition-check-failure ALL_OLD

Fix →

  1. Treat it as a conflict: re-read, merge, and retry with the new version
  2. Use ReturnValuesOnConditionCheckFailure=ALL_OLD to see the current item without a second read

Prevent → Handle the exception explicitly in code, and count it as a business metric, not an error.

aws All AWS questions Roadmap: AWS core services

More on RDS, Aurora and DynamoDB