NoSQL · Data models beyond tables
How do you choose a DynamoDB partition key?
Short answer
Choose a partition key with high cardinality and even access, and add a sort key when you need ranges or item collections. The pair determines how data is distributed and how you query it.
DynamoDB hashes the partition key to place an item in a partition. Items with the same partition key and different sort keys form an item collection and can be queried together. A low-cardinality key concentrates traffic and creates hot partitions.
{
"TableName": "Orders",
"KeySchema": [
{ "AttributeName": "customerId", "KeyType": "HASH" },
{ "AttributeName": "orderId", "KeyType": "RANGE" }
],
"AttributeDefinitions": [
{ "AttributeName": "customerId", "AttributeType": "S" },
{ "AttributeName": "orderId", "AttributeType": "S" }
]
}What to watch
- Cardinality: many distinct partition key values, not a status or a boolean
- Access skew: a few huge tenants can still create hot partitions
- Sort key design: encode the query dimension, such as date plus id
- Item size: the maximum is 400 KB, which limits large embedded values
- Secondary indexes: global secondary indexes have their own partition keys
DynamoDB has adaptive capacity that can move traffic away from a hot partition, but it is not a substitute for a good key. A time-based key such as today's date concentrates all current writes into one partition and is a classic design mistake.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is an item collection?
- 2Why is a timestamp a risky partition key?
- 3How does a sort key improve query flexibility?
- 4What happens if one tenant is much larger than the others?
Say it in the interview
I choose a partition key with high cardinality and even access, then add a sort key for the range or grouping the query needs. Items with the same partition key form an item collection, which is useful for one-to-many reads. I watch for skew, because adaptive capacity helps but does not fix a fundamentally bad key. I also remember that the partition key cannot be changed in place, and the 400 KB item limit constrains how much I embed.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live NoSQL mock interview and get scored feedback on depth, clarity and confidence.