DynamoDB interview question · Most asked
Explain partition keys, sort keys and the two kinds of primary key.
Summary
A primary key is either a simple partition key (each item has a unique partition key value) or a composite of partition key plus sort key (items share a partition key and are unique by sort key). The partition key is hashed to choose the storage partition; the sort key orders items within that partition.
Longer Answer
With a composite key, all items with the same partition key form an item collection, stored together and sorted by sort key. Query can then fetch the whole collection or a range of it (begins_with, between, <, >), which is how one-to-many relationships are modelled: PK = CUSTOMER#123, SK = ORDER#2026-10-17#987.
Good partition keys have many distinct values that spread requests evenly (user ID, order ID). Bad partition keys concentrate traffic (a status flag, today's date).
Code Example
PK SK attributes
CUSTOMER#123 PROFILE name, email
CUSTOMER#123 ORDER#2026-10-15#981 total, status
CUSTOMER#123 ORDER#2026-10-17#987 total, status
Key Points
- Simple PK or composite PK + SK.
- PK is hashed to a partition.
- SK sorts the item collection.
- Query by PK with SK conditions.
More most-asked DynamoDB questions
- What is DynamoDB, and what workloads is it a good fit for?
- How do conditional writes work, and how do you implement optimistic locking in DynamoDB?
- What is a hot partition, and how do you avoid one?
- How do you approach data modelling in DynamoDB, and how is it different from relational design?
- What are DynamoDB Streams, and how are they used?
- Global secondary index or local secondary index: what is the difference?
- Query or Scan: what is the difference, and why avoid Scan?
- DynamoDB or a relational database such as Aurora PostgreSQL: how do you decide?