examples icon indicating copy to clipboard operation
examples copied to clipboard

[BUG] The function in aws-node-express-dynamodb-api is not idempotent

Open Nsupyq opened this issue 2 years ago • 4 comments

The function of writing username in index.js under aws-node-express-dynamodb-api is not idempotent and may cause data inconsistency.

try {
  await dynamoDbClient.put(params).promise();
  res.json({ userId, name });
}

Assuming two concurrent functions want to change the user name with the same userId. When one of them fails after executing dynamodb_client.put, AWS Lambda will retry it and change the user name again. Then the client will see the username is changed between two different names repeatedly even if there are only two functions changing the name. This should not happen if every function is executed only once and causes data inconsistency.

This can be fixed by making the function idempotent. DynamoDB has provided an idempotent write operation called TransactWriteItems. Replacing dynamodb_client.put with it can fix the idempotence bug. Users need to provide a unique client token to be the parameter of TransactWriteItems API. which helps DynamoDB to determine whether the incoming write is a retry.

Nsupyq avatar Nov 02 '21 08:11 Nsupyq

Hello @Nsupyq, thanks for reporting, would you like to submit a PR with such improvement?

pgrzesik avatar Nov 02 '21 08:11 pgrzesik

Ok, I will submit a PR soon.

Nsupyq avatar Nov 02 '21 09:11 Nsupyq

Thank you @Nsupyq :bow:

pgrzesik avatar Nov 02 '21 09:11 pgrzesik

Hello @pgrzesik, I have submitted a PR #658 with such improvement.

Nsupyq avatar Nov 02 '21 12:11 Nsupyq