0
0
AWScloud~10 mins

Partition key and sort key in AWS - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Partition key and sort key
Start: Insert Item
Extract Partition Key
Hash Partition Key
Find Partition
Check if Sort Key Exists?
NoStore Item in Partition
Yes
Sort Items in Partition by Sort Key
Store Item in Sorted Order
End
When adding data, DynamoDB uses the partition key to find the partition. If a sort key is present, it orders items within that partition by the sort key.
Execution Sample
AWS
PutItem({PartitionKey: 'User#123', SortKey: 'Order#456', Data: '...'});
This stores an item with partition key 'User#123' and sort key 'Order#456' in DynamoDB.
Process Table
StepActionPartition KeySort KeyResult
1Receive item to storeUser#123Order#456Item ready for processing
2Extract partition keyUser#123Order#456Partition key identified
3Hash partition keyUser#123Order#456Hash computed for partition placement
4Locate partitionUser#123Order#456Partition found based on hash
5Check for sort keyUser#123Order#456Sort key present
6Sort items in partitionUser#123Order#456Items ordered by sort key
7Store item in sorted orderUser#123Order#456Item stored successfully
8End processUser#123Order#456Item available for queries
💡 Process ends after item is stored in the correct partition and sorted position.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
PartitionKeyN/AUser#123User#123User#123User#123
SortKeyN/AOrder#456Order#456Order#456Order#456
PartitionHashN/AN/AHashValue123HashValue123HashValue123
PartitionLocationN/AN/AN/APartitionNode5PartitionNode5
Key Moments - 3 Insights
Why do we hash the partition key before storing the item?
Hashing the partition key helps DynamoDB quickly find the right partition to store the item, as shown in step 3 of the execution_table.
What happens if there is no sort key?
If no sort key exists, the item is stored directly in the partition without sorting, as indicated by the branch in the concept_flow.
How does the sort key affect item storage?
The sort key orders items within the same partition, so items with the same partition key are sorted by the sort key before storage, as shown in steps 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the action at step 4?
AHash partition key
BStore item in sorted order
CLocate partition
DExtract partition key
💡 Hint
Check the 'Action' column in execution_table row with Step '4'.
At which step does DynamoDB check if a sort key exists?
AStep 2
BStep 5
CStep 7
DStep 3
💡 Hint
Look for 'Check for sort key' in the execution_table.
If the sort key was missing, which step would be skipped?
AStep 6
BStep 4
CStep 2
DStep 7
💡 Hint
Refer to concept_flow where the branch skips sorting if no sort key.
Concept Snapshot
Partition key is used to find the partition by hashing.
Sort key orders items within the same partition.
Items with same partition key are stored together.
Without sort key, items are stored unordered in partition.
This design enables fast lookups and sorted queries.
Full Transcript
When you add an item to DynamoDB, it first looks at the partition key. It hashes this key to find the right partition where the item will live. If the item has a sort key, DynamoDB sorts all items in that partition by this key. This helps keep related items together and in order. If there is no sort key, the item is just stored in the partition without sorting. This process ensures quick access and organized storage.