0
0
DynamodbComparisonBeginner · 4 min read

Partition Key vs Sort Key in DynamoDB: Key Differences and Usage

In DynamoDB, the partition key uniquely identifies the partition where data is stored and determines data distribution, while the sort key organizes data within that partition and allows sorting and range queries. Together, they form a composite primary key that enables efficient querying and data retrieval.
⚖️

Quick Comparison

This table summarizes the main differences between the partition key and sort key in DynamoDB.

FactorPartition KeySort Key
PurposeDetermines data partition and distributionSorts and organizes data within a partition
UniquenessMust be unique per item if no sort keyCombined with partition key to ensure uniqueness
Data DistributionDistributes data across partitionsDoes not affect data distribution
QueryingUsed to locate partition quicklyUsed for range queries and sorting within partition
RequiredAlways requiredOptional, only for composite keys
Example UsageUser IDTimestamp of user activity
⚖️

Key Differences

The partition key is the primary attribute that DynamoDB uses to distribute data across multiple storage nodes. It ensures that data is spread evenly to optimize performance and scalability. Each item with the same partition key is stored together in the same partition.

The sort key is optional and works alongside the partition key to create a composite primary key. It allows multiple items to share the same partition key but be uniquely identified by the sort key. This enables sorting and querying items within the same partition based on the sort key's value.

Using both keys together allows efficient queries such as retrieving all items for a partition key within a certain range of sort key values, which is useful for time-series data or ordered records.

💻

Partition Key Example

This example shows how to create a DynamoDB table with only a partition key and add an item.

bash
aws dynamodb create-table \
    --table-name Users \
    --attribute-definitions AttributeName=UserID,AttributeType=S \
    --key-schema AttributeName=UserID,KeyType=HASH \
    --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

aws dynamodb put-item \
    --table-name Users \
    --item '{"UserID": {"S": "user123"}, "Name": {"S": "Alice"}}'

aws dynamodb get-item \
    --table-name Users \
    --key '{"UserID": {"S": "user123"}}'
Output
{ "Item": { "UserID": {"S": "user123"}, "Name": {"S": "Alice"} } }
↔️

Sort Key Equivalent

This example creates a DynamoDB table with a composite primary key using both partition and sort keys, then adds and queries items.

bash
aws dynamodb create-table \
    --table-name UserActivities \
    --attribute-definitions AttributeName=UserID,AttributeType=S AttributeName=Timestamp,AttributeType=N \
    --key-schema AttributeName=UserID,KeyType=HASH AttributeName=Timestamp,KeyType=RANGE \
    --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

aws dynamodb put-item \
    --table-name UserActivities \
    --item '{"UserID": {"S": "user123"}, "Timestamp": {"N": "1650000000"}, "Activity": {"S": "login"}}'

aws dynamodb query \
    --table-name UserActivities \
    --key-condition-expression "UserID = :uid and Timestamp > :ts" \
    --expression-attribute-values '{":uid":{"S":"user123"}, ":ts":{"N":"1640000000"}}'
Output
{ "Items": [ { "UserID": {"S": "user123"}, "Timestamp": {"N": "1650000000"}, "Activity": {"S": "login"} } ] }
🎯

When to Use Which

Choose a partition key when you need to distribute data evenly across storage and uniquely identify items if no sort key is used. It is essential for scaling and performance.

Choose a sort key when you want to store multiple related items under the same partition key and need to query or sort them efficiently, such as time-based logs or ordered events.

Use both keys together for complex queries that require filtering or sorting within a group of related items.

Key Takeaways

The partition key determines data distribution and partitions in DynamoDB.
The sort key organizes and sorts data within a partition for efficient queries.
Together, they form a composite key that uniquely identifies items.
Use partition key alone for simple unique items and add sort key for related, ordered data.
Choosing the right keys improves query performance and scalability.