Skip to content

Idempotency bug in index.js #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Nsupyq opened this issue Oct 31, 2021 · 1 comment
Open

Idempotency bug in index.js #7

Nsupyq opened this issue Oct 31, 2021 · 1 comment

Comments

@Nsupyq
Copy link

Nsupyq commented Oct 31, 2021

The function in index.js is not idempotent and may insert multiple duplicate records into database.

item[PRIMARY_KEY] = uuidv4();
const params = {
  TableName: TABLE_NAME,
  Item: item
}
try {
  await dynamoDb.put(params).promise()
  return processResponse(IS_CORS);
}

When the function fails after executing dynamoDb.put(params).promise() because runtime error or other reasons, AWS Lambda will retry the function. Then uuidv4() will return another different id and dynamoDb.put(params).promise() will be executed again, which creates another new record in database.

This can be fixed by making the function idempotent. First, the primary key should be provided by users as a parameter. This can avoid generating a different new primary key on retry. Second, before executing dynamoDb.put(params).promise(), the function should check whether the record with the same primary key have been in the database. If so, that means the current execution is a retry and the function should not write the database again.

Please let me know if I've missed something or if my understanding of the code is incorrect. Thanks!

@Nsupyq
Copy link
Author

Nsupyq commented Nov 3, 2021

Sorry, I think it is unnecessary to check whether the record with the same primary key have been in the database. We just need to ensure there will be only one new record in database. I have modified my PR #8 and removed unnecessary check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant