Working With DynamoDB

Notes and hints with DynamoDB

Working With DynamoDB

Table Design

GSI Consideration

Some notes on what to consider when adding a new GSI to a DynamoDB table, particularly one that is already being used in production:

  • If the GSI is based on a string attribute that is already being used, make sure your code is not trying to store the blank string. DynamoDB will refuse to store the attribute value. Not setting the attribute at all is fine — I assume they just won't get put into the index — but blank values will not work.

  • Backfilling begins and will run asynchronously once the GSI is created, using CloudFormation or otherwise (see docs).

Getting and Setting Data

Using BatchGetItem

The BatchGetItem API call takes, as the key for RequestItems the table name.

out, err := p.client.BatchGetItem(ctx, &dynamodb.BatchGetItemInput{
    RequestItems: map[string]types.KeysAndAttributes{
        "table-name": {
            Keys: myKeys,
        },
    },
})

BatchGetItem returns up to 100 items or 16 MB. If the request is larger than 100 items, the request will fail. Any unreturned items are returned as "unprocessed keys" in the response. You can use this item calculator to get an estimate.

Detecting Empty Results

The best way to detect when a GetItem call to DynamoDB returns no values is to check that the returned Item field is nil:

out, err := p.client.GetItem(ctx, &dynamodb.GetItemInput{
    Key:       key,
    TableName: tableName,
})
if err != nil {
    // Error getting the item
    return nil, err
} else if out.Item == nil {
    // No item found
    return nil, nil
}

// Do the thing with out.Item

Deploying Via CloudFormation

GSI Considerations

Some notes about deploying GSIs with CloudFormation.

  • The only properties of an existing GSI that you can change are "Provisioned Throughput" and "Contributor Insights Specification". Anything else will require a new GSI.

  • A CloudFormation delta can only have on GSI "lifecycle" change (creation or deletion) per table. If you need to do something like create two GSIs on a table, or replace one GSI with another one, you will need to break them out into two separate deltas.

Last updated