Amazon DynamoDB Primary Key, Partition Key and Sort Key — How to Choose Right Key for DynamoDB

In this article, we are going to learn Amazon DynamoDB and Main Core Concepts of AWS DynamoDB which’s are Tables, Items, Attributes and Indexes.

When we create a table, we have to specify the primary key of the table in addition to the table name. By the end of the article, we will learn How to Choose the Right Partition Key for DynamoDB.

Amazon DynamoDB Primary Key, Partition Key and Sort Key

We can say that primary key uniquely identifies each item in the table, so no two items can have the same key.

DynamoDB supports two different kinds of primary keys:
1- Partition key
2- Partition key and sort key

So primary key can consist of Partition key and sort key. That means primary key can be only partition key or combination of partition key and sort key. You can see at the beginning of article, the Product table primary key is combination of partition key and sort key with ProductId and Type columns.

Partition Key

DynamoDB uses the partition key for creating an internal hash function. The output from the hash function determines the partition which is physical storage in DynamoDB that means where the item will be stored. In a DynamoDB table if the table has only a partition key, then no two items can have the same partition key value.

Think that we have People table is an example of a table with a simple primary key which is PersonID. Primary key consist only partition key and there is no sort key. We can directly access any item in the People table by providing the PersonId for that item.

Partition key and Sort Key

This is also called as a composite primary key, this type of key is composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key. When we use partition key in DynamoDB, it uses input to an internal hash function and this partition key provide to determines which location the item will be stored. All items with the same partition key value are stored together, in sorted order by sort key value.

So if we provide a sort key into primary key, we can query on that table. Like give me the items of table with the sort key. In our e-commerce application, we will use sort key queries into Order table. Order Table will have userName partition key and orderDate sort key that we can query table with orderDate sort key. If DynamoDB table has a partition key and a sort key, it’s possible for multiple items to have the same partition key value. However, those items must have different sort key values.

We will use both way when designing our e-commerce application. We will have Product, Basket and Ordering DynamoDB table. Product and Basket table will have only Partition Key, but Order table will have both Partition key and sort keys in order to querying order data with order date sort key.

Step by Step Design AWS Architectures w/ Course

In this course, we will learn almost all the AWS Serverless Services with all aspects. We are going to build serverless applications with using AWS Lambda, Amazon API Gateway, Amazon DynamoDB, Amazon Cognito, Amazon S3, Amazon SNS, Amazon SQS, Amazon EventBridge, AWS Step Functions, DynamoDB and Kinesis Streams. This course will be 100% hands-on, and you will be developing a real-world application with hands-on labs together and step by step.

Source Code

— Clone or fork this repository, if you like don’t forget the star. If you find or ask anything you can directly open issue on repository.




AWS Serverless with AWS Lambda, API Gateway, Amazon DynamoDB, Cognito, S3, SNS, SQS, EventBridge, Step Functions, DynamoDB and Kinesis Streams, CloudFormation, SAM, CDK. We will develop Lambda-based event-driven application integrate to all AWS Serverless Services.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mehmet Ozkaya

Software Architect | Udemy Instructor | AWS Community Builder | Cloud-Native and Serverless Event-driven Microservices