DynamoDB
AWS
partition key
sort key
data retrieval

DynamoDb How to retrieve the first item by sort key for each of a given list of partition keys

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction to DynamoDB

Amazon DynamoDB is a fully managed NoSQL database service offered by AWS that enables the fast and predictable performance of applications. It is designed for applications requiring high throughput and millisecond response times, and it is suitable for both small-scale single-table operations and complex distributed applications. DynamoDB provides the redundancy and durability of data across multiple Availability Zones, ensuring reliability.

In DynamoDB, data is organized into tables, and each table contains items. Each item is uniquely identified by a primary key, which can be either a partition key or a combination of a partition key and a sort key. This article will focus on accessing items using partition and sort keys, specifically learning how to retrieve the first item (by sort key) for each of a given list of partition keys.

Understanding Primary Key Structure

Before diving into the specifics of item retrieval, it is crucial to understand the primary key structure in DynamoDB:

  • Partition Key: Also referred to as the hash key, it determines the partition where the item is stored. Every partition key value must be unique within a table if no sort key is used.
  • Sort Key: Also known as the range key, the sort key allows storage of multiple items with the same partition key. Items with the same partition key can be ordered based on the sort key, which facilitates more complex queries.

Querying DynamoDB for Specific Items

When querying DynamoDB for items, you can take advantage of the primary key structure to perform efficient searches. To retrieve the first item (by sort key) for multiple partition keys, we will perform the following steps:

  1. Define the Key Conditions: Use the partition key to specify the items' logical collection within a table. You’ll query each partition key separately.
  2. Sort Key Optimization: Leverage the sort key to retrieve the first item in ascending order. Set limits on results to fetch only the top entry for each partition key.
  3. Batch Processing: Utilize batch processing for efficiency, parallelizing queries against different partition keys.

Detailed Technical Steps

1. Table and Data Setup

Assume you have a table named Orders, with a composite primary key consisting of the partition key CustomerID (a string) and the sort key OrderDate (a string). This setup tracks orders per customer by the order date.

Original data sample:

 
CustomerIDOrderDateOrderIDDetails
C12023-02-011001"Order Detail1"
C12023-03-051002"Order Detail2"
C22023-01-101003"Order Detail3"
C22023-02-151004"Order Detail4"

2. Query Code Example

Here's a Python script using boto3, the AWS SDK for Python, to accomplish this task:

python
1import boto3
2
3def get_first_order_for_keys(dynamodb_table_name, partition_keys):
4    dynamodb = boto3.resource('dynamodb')
5    table = dynamodb.Table(dynamodb_table_name)
6    
7    results = []
8
9    # Iterate over each Partition Key
10    for key in partition_keys:
11        response = table.query(
12            KeyConditionExpression=boto3.dynamodb.conditions.Key('CustomerID').eq(key),
13            Limit=1,  # Only retrieve the first item by OrderDate
14            ScanIndexForward=True  # Ascending order for the sort key
15        )
16        
17        if response['Items']:
18            results.append(response['Items'][0])
19    
20    return results
21
22# Define the list of CustomerIDs for which you want the first order
23partition_keys = ['C1', 'C2']
24first_orders = get_first_order_for_keys('Orders', partition_keys)
25
26for order in first_orders:
27    print(order)
28
29### 3. Explanation
30
31* **`KeyConditionExpression`:** This parameter specifies that we're filtering items by `CustomerID`. It uses the `eq()` method to state equality.
32* **`Limit=1`:** Restriction to retrieve only the top result when sorted by `OrderDate` (ascending by default when `ScanIndexForward=True`).
33* **Batch Requests:** Since the function iterates over the list of `partition_keys`, each key results in its own API call. Consider AWS limits and account for network latency when scaling this solution.
34
35## Key Points Summary Table
36
FeatureDescription
Partition KeyUnique identifier that determines item's storage partition
Sort KeyOrders items with the same partition key
Query FilterFilters results based on equality
Limit ParameterRestricts the number of items returned
Sort OrderResults ordered by sort key in specified direction

Additional Considerations

  • Error Handling: Ensure that error handling is in place for scenarios, such as network exceptions or AWS rate limiting.
  • Pagination: Consider implementing logic to handle DynamoDB pagination if retrieving substantial data beyond single queries.
  • Cost and Performance: Incorporate DynamoDB’s billing and performance characteristics into the design, ensuring an understanding of the costs associated with multiple read operations.

By understanding the intricacies in querying with partition and sort keys, and by leveraging the capabilities provided by AWS through the SDKs, you can efficiently retrieve sorted data subsets across multiple logical collections in DynamoDB.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design