DynamoDB
BatchGet
Key Order
AWS
NoSQL

DynamoDB BatchGet Get results in same order as provided 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

DynamoDB BatchGet: Retrieve Results in the Same Order as Provided Keys

Amazon DynamoDB is a managed NoSQL database service designed to deliver fast and predictable performance with seamless scalability. It automatically distributes data across multiple servers and scales up or down to handle the load without any intervention from the user. One of the popular features of DynamoDB is the BatchGetItem operation, which allows you to retrieve multiple items from one or more tables in a single request.

In this article, we'll explore how to use the BatchGetItem operation and how you can ensure the results are returned in the same order as the keys provided.

Understanding BatchGetItem Operation

The BatchGetItem operation can return up to 100 items, or 16 MB of data, in a single request. It allows you to make requests to multiple tables, combining responses within a single operation. This can drastically reduce the number of API calls, minimizing latency and reducing costs.

Syntax

Here is the basic syntax of a BatchGetItem request:

json
1{
2  "RequestItems": {
3    "TableName1": {
4      "Keys": [
5        {
6          "PrimaryKeyAttributeName": {"S": "KeyAttributeValue1"},
7          "SortKeyAttributeName": {"N": "SortKeyAttributeValue1"}
8        },
9        {
10          "PrimaryKeyAttributeName": {"S": "KeyAttributeValue2"},
11          "SortKeyAttributeName": {"N": "SortKeyAttributeValue2"}
12        }
13      ],
14      "AttributesToGet": [
15        "AttributeName1",
16        "AttributeName2"
17      ]
18    },
19    "TableName2": {
20      "Keys": [
21        {
22          "PrimaryKeyAttributeName": {"S": "KeyAttributeValue3"}
23        }
24      ]
25    }
26  }
27}

Key Points

  • Batch Size Limitations: The maximum number of items you can retrieve in a single batch request is 100.
  • Size Limitations: The total size of all items retrieved cannot exceed 16 MB.

Challenges of Ordering Results

The BatchGetItem API does not guarantee the order of items returned in the response matches the order the keys are provided. This is because DynamoDB operates on a distributed architecture, where data might be retrieved concurrently from different partitions.

Maintaining the Order of Results

To maintain the order of the results as per the input keys, you can use a client-side technique. After retrieving the data, reorder the results in your application code based on the order of the requested keys.

Here is a sample Python code using the AWS SDK for Python (Boto3) to demonstrate how you can achieve this:

python
1import boto3
2
3def batch_get_items(table_name, keys):
4    dynamodb = boto3.resource('dynamodb')
5    table = dynamodb.Table(table_name)
6    
7    # Prepare the keys for the request
8    request_items = {
9        table_name: {
10            'Keys': keys
11        }
12    }
13
14    response = dynamodb.batch_get_item(RequestItems=request_items)
15    
16    # Extract items from response
17    items = response['Responses'].get(table_name, [])
18    
19    # Reorder items in the order of the requested keys
20    key_to_item = {tuple(item[key] for key in keys[0].keys()): item for item in items}
21    ordered_items = [key_to_item.get(tuple(key.values())) for key in keys]
22    
23    return ordered_items
24
25# Example usage
26keys = [
27    {"PrimaryKeyAttributeName": "KeyAttributeValue1"},
28    {"PrimaryKeyAttributeName": "KeyAttributeValue2"}
29]
30
31ordered_results = batch_get_items('TableName', keys)
32print(ordered_results)

Summary Table

To summarize the key points and best practices for BatchGetItem operations:

Feature/ConstraintDescription
Maximum Batch SizeUp to 100 items per request
Maximum Data SizeUp to 16 MB of data can be retrieved per request
Order GuaranteeNo guarantee of order; client-side reordering is necessary
Multi-table RequestsBatchGetItem can retrieve from multiple tables in a single request
PerformanceReduces network calls and improves efficiency
Error HandlingUnprocessedKeys are returned for retry, often due to exceeding throughput

Additional Considerations

  • Error Handling: If some keys are not processed due to insufficient throughput (i.e., exceeding provisioned capacity), they are returned in the UnprocessedKeys section of the response. These keys can be retried in subsequent requests.
  • Cost Efficiency: While BatchGetItem is cost-efficient by minimizing round trips, be mindful of the provisioned capacity limits and throttle rates to avoid additional read costs.

By utilizing the BatchGetItem operation effectively and managing your DynamoDB resources according to best practices, you can achieve performance gains and maintain logical order without compromising efficiency.


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

All Rights Reserved.