Partition Keys and Sort Keys in DynamoDB

The different between DynamoDB's Partition and Sort keys

Posted on September 10, 2020

When creating atable in Dynamo DB you’re required to add a Primary Key of the table. You have two options – add a Partition Key or a Partition key and Sort Key.

The Primary Key uniquely identifies each item in the table and no two items can have the same Partition Key.

Here’s a simple explanation of the two and what they do.

Partition Key

  • A simple primary key is composed of just one known attribute e.g. and id
  • In a table that has only a partition key, no two items can have the same partition key value.

Partition key and sort key

  • Also referred to as a composite primary key, this type of key is composed of two attributes – a Partition Key and a Sort Key.
  • The partition key is used for partitioning the data, so data with the same partition key is stored together.
    For example you have could have multiple item types in your database such as type=post and type=page.

    In this case you could have the Partition Key as type – therefor all items with a type of ‘post’ would be grouped and all items with a type of ‘page’ would be grouped.

    If you have, for example, just a list of articles that didn’t need to be grouped like this, then you could set the partition key as id.
  • Items with the same partition key value are stored together, in sorted order by sort key value. e.g. a sort key of createdDate would return items in order of createdDate.