DynamoDB Primary Keys - Complete Guide
Why DynamoDB Primary Keys Are Important
Primary keys are fundamental to DynamoDB because they uniquely identify each item in a table and determine how data is distributed across partitions. Understanding primary keys is essential for designing efficient, scalable DynamoDB tables and is a frequently tested topic on the AWS Developer Associate exam.
What Are DynamoDB Primary Keys?
DynamoDB supports two types of primary keys:
1. Simple Primary Key (Partition Key Only)
- Consists of a single attribute called the partition key (also known as hash key)
- Each item must have a unique partition key value
- DynamoDB uses the partition key's value as input to an internal hash function to determine which partition stores the item
- Example: A Users table with UserID as the partition key
2. Composite Primary Key (Partition Key + Sort Key)
- Consists of two attributes: partition key and sort key (also known as range key)
- Multiple items can share the same partition key, but must have different sort keys
- Items with the same partition key are stored together, sorted by the sort key value
- Example: An Orders table with CustomerID as partition key and OrderDate as sort key
How Primary Keys Work
Partition Key Mechanism:
- DynamoDB uses the partition key value with an internal hash function
- The hash output determines which physical partition stores the data
- This enables horizontal scaling and even data distribution
Sort Key Mechanism:
- When using a composite key, items are grouped by partition key
- Within each partition key group, items are ordered by sort key
- This enables efficient range queries on the sort key attribute
Key Characteristics to Remember
- Partition keys should have high cardinality (many unique values) to distribute data evenly
- Sort keys enable queries using comparison operators (begins_with, between, greater than, less than)
- Primary key attributes must be defined at table creation and cannot be changed
- Primary key attributes support only String, Number, or Binary data types
Common Use Cases
Simple Primary Key:
- User profiles (UserID)
- Product catalog (ProductID)
- Session management (SessionID)
Composite Primary Key:
- Order history (CustomerID + OrderTimestamp)
- Forum posts (ForumName + PostDateTime)
- Game scores (GameID + PlayerID)
Exam Tips: Answering Questions on DynamoDB Primary Keys
1. Identify the access pattern first - If the question mentions querying by a single unique attribute, think simple primary key. If it mentions querying ranges or multiple items per entity, think composite key.
2. Remember the terminology - Partition key = Hash key, Sort key = Range key. AWS uses these interchangeably in exam questions.
3. Understand query limitations - You MUST specify the partition key in a Query operation. Sort key conditions are optional but useful for filtering.
4. Watch for hot partition scenarios - If a question describes uneven access patterns or throttling, the answer often involves choosing a better partition key with higher cardinality.
5. Composite keys enable efficient queries - When asked about retrieving multiple related items efficiently, composite primary keys are typically the answer.
6. GetItem requires the full primary key - For simple keys, provide partition key. For composite keys, provide both partition and sort key.
7. Scan vs Query - Questions about accessing data with unknown partition keys typically involve Scan operations, which are less efficient.
8. Data type constraints - Primary key attributes must be scalar types (String, Number, Binary) - never lists, maps, or sets.