How to future proof these possible requirement changes (swaping primary key columns) with a dynamodb table design?

Viewed 38

I have the following data structure

item_id String
version String
_id     String 
data    String

_id is simply a UUID to identify the item. There is no need to search for a row by this field yet.

As of now, item_id, an id generated by an external system, is the a primary key. i.e. Given the item_id, I want to be able retrieve version, _id and data from the dynamodb table.

item_id -> (version, _id, data)

Therefore I am setting item_id as the partition key.

I have two questions for future-proofing (evolution of) the above "schema":

  1. In the future, if I want to incorporate version (version number of the item) into the primary key, can I just modify the table and add it to be the partition key?

  2. If I also want to make the data searchable by _id, is it feasible modify the table to assign _id to be the partition key (It is a unique value because it is a UUID) and reassign item_id to be a search key?

I want to avoid creation of new dynamodb table and data migration to create new key structures, because it may lead to down time.

1 Answers

You cannot update primary keys in DynamoDB. From the docs:

You cannot use UpdateItem to update any primary key attributes. Instead, you will need to delete the item, and then use PutItem to create a new item with new attributes.

If you wanted to make data searchable by _id, you could introduce a secondary index with the _id field as the partition key of the index.

For example, let's say your data looked like this:

enter image description here

If you defined a secondary index on _id, the index would look like this (same data as the previous example, just a different logical view):

enter image description here

DynamoDB doesn't currently have any native versioning functionality, so you'll have to incorporate that into your data model. Fortunately, there's lots of discussion about this use case on the web. AWS has a document of DynamoDB "Best Practices", including an example of versioning.

Related