Search code examples
amazon-web-servicesamazon-dynamodbdynamo-local

DynamoDB PutItem creating multiple items with same partition key when it should be replacing the item


I am using DynamoDB locally with NoSQL Workbench and an Express API.

I have a table generated as such:

const params = {
  AttributeDefinitions: [
    {
      AttributeName: 'id',
      AttributeType: 'S',
    },
  ],
  KeySchema: [
    {
      AttributeName: 'id',
      KeyType: 'HASH',
    }
  ]
  TableName: table.name,
  ProvisionedThroughput: {
    ReadCapacityUnits: 1,
    WriteCapacityUnits: 1
  },
}

dynamo.createTable(params).promise() // ... simplified version

I update an item using the DocumentClient as such:

const db = new AWS.DynamoDB.DocumentClient(configOptions);
const data = await db.put({
  TableName,
  Item,
}) // ... interact with result

Occasionally, it creates a new item in DynamoDB with the exact same id value even though I created it as a HASH and made it the partition key.

However, it doesn't happen every time. I can't seem to recreate it consistently.

Is there something I'm doing wrong with how I'm creating this table or updating this item that is leading to this?

See items 97/98 here, this image is of my NoSQL Workbench and you can see there are two items with the same id

See items 97/98 here, this image is of my NoSQL Workbench and you can see there are two items with the same id


Solution

  • After getting in touch with AWS Support, it turns out this is a known issue with using DynamoDB locally. They provided the following guidance for fixing it:

    As you mentioned that you are using the docker image for deploying DynamoDB local [1], please go ahead and remove the '-optimizeDbBeforeStartup' flag given in step 2 ('Docker tab') (and then save it as docker-compose.yml):

    1. Removing '-optimizeDbBeforeStartup' from the argument list: ========== .. .. command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data" .. .. ==========

    2. Create tables and insert item

    3. docker-compose down

    4. docker-compose up