DynamoDB primary keys
DynamoDB primary keys are fundamental to how data is organized and accessed in Amazon DynamoDB. There are two types of primary keys you need to understand for the AWS Certified Developer - Associate exam. **Partition Key (Simple Primary Key):** A single attribute that uniquely identifies each item in the table. DynamoDB uses the partition key value as input to an internal hash function, which determines the physical partition where the item will be stored. For example, a 'UserId' could serve as a partition key in a users table. **Composite Primary Key (Partition Key + Sort Key):** This consists of two attributes working together. The partition key determines the partition location, while the sort key orders items within that partition. This allows multiple items to share the same partition key as long as they have different sort keys. For instance, a table storing orders might use 'CustomerId' as the partition key and 'OrderDate' as the sort key. **Key Characteristics:** - Partition keys must be unique for simple primary keys - For composite keys, the combination of partition and sort key must be unique - Partition keys should be chosen to distribute data evenly across partitions - Sort keys enable range queries and efficient data retrieval patterns **Best Practices:** - Select high-cardinality attributes for partition keys to ensure even data distribution - Consider your access patterns when designing primary keys - Avoid hot partitions by not using attributes with limited values **Query Capabilities:** - You must specify the exact partition key value when querying - Sort key conditions allow for flexible queries using operators like begins_with, between, greater than, and less than Understanding primary key design is crucial for building performant DynamoDB applications and optimizing read/write throughput distribution.
DynamoDB Primary Keys - Complete Guide
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.