I want to know how to choose a primary key for my Amazon DynamoDB table.
Short description
Each item in a DynamoDB table must have a unique primary key. You can specify either a partition key or a composite primary key. To help you determine the correct primary key to use, see Best practices for designing and using partition keys effectively in DynamoDB.
Use one of the following methods to create your primary key:
- Use a high-cardinality attribute
- Use composite attributes
- Append random numbers to your partition key
Resolution
Use a high-cardinality attribute
If your table has an attribute with a unique value for each item, such as user IDs, then the attribute is a good primary key. In this case, the primary key is a partition key.
Use composite attributes
If your table doesn't have an attribute that's unique for each item, then you can combine different attributes to make a unique primary key. For example, you have an orders table that has attributes for customer ID and order ID. You combine the two ID values to get a partition key that's more likely to have many unique values. Then, you use another column, such as order date, as the sort key. In this case, you have a composite primary key.
Append random numbers to your partition key
If you have a write-heavy use case, then append random numbers within a predetermined range to your partition key to increase the key's randomness. For more information, see Using write sharding to distribute workloads evenly in your DynamoDB table.
Related information
Choosing the right DynamoDB partition key
Why is my Amazon DynamoDB provisioned table throttled?
Effective data sorting with Amazon DynamoDB