What is a composite primary key in DynamoDB?
Learn what a composite primary key is in DynamoDB, how partition and sort keys combine for uniqueness, and why it powers range queries and single-table design.
Expected Interview Answer
A composite primary key in DynamoDB is a two-part key made of a partition key plus a sort key, which together uniquely identify each item and allow many items to share one partition key.
DynamoDB supports two primary key types: a simple key (partition key only) and a composite key (partition key and sort key). With a composite key, uniqueness is enforced on the combination, so items with the same partition key must differ by sort key. This lets you store an item collection under one partition key and query it as a range, which is the foundation of one-to-many modeling and single-table design in DynamoDB.
- Uniquely identifies items by a partition and sort key pair
- Allows multiple items to share a partition key
- Enables efficient range queries within a partition
- Underpins one-to-many and single-table design patterns
- Keeps related items co-located for fast, cheap reads
AI Mentor Explanation
A composite key is like identifying a delivery by (over number, ball number) together. Over three alone isn't unique because it has six balls, but over three ball four points to exactly one delivery. DynamoDB pairs a partition key with a sort key the same way to name one precise item.
Step-by-Step Explanation
Step 1
Choose a partition key
Pick a high-cardinality attribute that groups related items, such as customerId.
Step 2
Add a sort key
Declare a second key attribute, such as orderDate, marked as the RANGE key.
Step 3
Enforce combined uniqueness
DynamoDB treats the partition-key plus sort-key pair as the unique identifier for each item.
Step 4
Store an item collection
Many items can share the partition key as long as their sort keys differ.
Step 5
Query the collection
Fetch all or a range of items under one partition key, ordered by the sort key.
What Interviewer Expects
- Defines a composite key as partition key plus sort key
- Knows uniqueness is enforced on the combination, not each part alone
- Contrasts it with a simple (partition-key-only) primary key
- Explains it enables item collections and range queries
- Connects it to one-to-many and single-table design
Common Mistakes
- Thinking the partition key alone must be unique with a composite key
- Confusing a composite primary key with a secondary index
- Assuming a composite key requires a third attribute
- Believing the sort key changes which partition stores the item
- Forgetting that both key attributes must be defined in AttributeDefinitions
Best Answer (HR Friendly)
“A composite primary key uses two values together — a main grouping value and a second ordering value — to uniquely name each record. It lets many records share the first value and be told apart by the second, like all of one customer's orders separated by date.”
Code Example
import boto3
client = boto3.client('dynamodb')
client.create_table(
TableName='Reviews',
KeySchema=[
{'AttributeName': 'productId', 'KeyType': 'HASH'}, # partition key
{'AttributeName': 'reviewId', 'KeyType': 'RANGE'} # sort key
],
AttributeDefinitions=[
{'AttributeName': 'productId', 'AttributeType': 'S'},
{'AttributeName': 'reviewId', 'AttributeType': 'S'}
],
BillingMode='PAY_PER_REQUEST'
)
# Same productId, different reviewId -> two distinct items
table = boto3.resource('dynamodb').Table('Reviews')
table.put_item(Item={'productId': 'p-100', 'reviewId': 'r-1', 'stars': 5})
table.put_item(Item={'productId': 'p-100', 'reviewId': 'r-2', 'stars': 4})Follow-up Questions
- What is the difference between a simple and a composite primary key?
- How does a composite key enable single-table design?
- Can two items share the same partition key and sort key?
- What is an item collection and how big can it get?
- How do local and global secondary indexes relate to the primary key?
MCQ Practice
1. A composite primary key in DynamoDB consists of:
A composite primary key combines a partition (HASH) key with a sort (RANGE) key; the pair uniquely identifies each item.
2. With a composite key, uniqueness is enforced on:
Two items may share a partition key as long as their sort keys differ; the combination must be unique.
3. Why use a composite key instead of a simple key?
A composite key lets many items share a partition key and be queried as a sorted range, powering one-to-many models.
Flash Cards
What is a composite primary key? — A primary key made of a partition key plus a sort key, unique on the combination.
Simple vs composite key? — Simple is partition-key only (one item per key); composite adds a sort key so many items share a partition key.
What enforces uniqueness with a composite key? — The partition-key plus sort-key pair together, not either part alone.
Why use a composite key? — To store item collections and run efficient range queries, enabling one-to-many and single-table design.
Continue Learning
Related Interview Questions
What is the difference between a partition key and a sort key in DynamoDB?
medium
What is single-table design in DynamoDB and why is it recommended?
hard
What is a partition key in DynamoDB and how does it distribute data?
medium
How do you model one-to-many and many-to-many relationships in DynamoDB?
hard