serverless / serverless/examples

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

Open
#657 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
11.5k
Forks
4.4k
Avg merge
2h 55m
Merged PRs (30d)
3

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in aws-node-express-dynamodb-api/index.js and inspect the DynamoDB put call in the username-writing function. Read the linked Lambda retry and DynamoDB transaction documentation, then verify the behavior with concurrent updates and retries. Done means a retried write is recognized as the same request and does not repeatedly change the username.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, javascript
Domain
backend, cloud, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.