DynamoDB
BatchWrite
Boto3
Database
AWS

How many records i can insert using DynamoDb BatchWrite by Boto3

System Design practice on Codemia

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

Practice system design

Amazon DynamoDB is a fast and flexible NoSQL database service designed to provide single-digit millisecond performance at any scale. When using Boto3 to interact with DynamoDB, one key feature is the ability to perform batch operations. The BatchWriteItem API allows you to efficiently insert or delete multiple items in a single request. This article will detail how many records you can insert in a single batch write operation using DynamoDB and Boto3, along with technical explanations, examples, and additional subtopics to give a comprehensive overview.

Understanding DynamoDB BatchWriteItem

Limits of BatchWriteItem

DynamoDB's BatchWriteItem API imposes certain limitations on the number of items you can process in a single batch write operation. These limitations are crucial for ensuring optimal performance and preventing throttling:

  • Maximum Items: Each BatchWriteItem call can write up to 25 items.
  • Item Size: Each item can have a maximum size of 400 KB.
  • Total Request Size: The total size of all items written in a single request must not exceed 16 MB.

These limitations ensure that a batch write operation remains efficient and manageable for both the client and DynamoDB.

Practical Implications

In practice, these limits mean that while you can process up to 25 writes in one request, if your items are large, you may hit the 16 MB limit before reaching the 25-item limit. As such, it's important to calculate the size of your data beforehand.

Using Boto3 to Perform Batch Writes

Boto3 is the AWS SDK for Python, and it provides a simple way to interact with DynamoDB. Below is an example of how you can perform a batch write using Boto3:

python
1import boto3
2
3# Initialize a session using Amazon DynamoDB
4session = boto3.Session()
5dynamodb = session.resource('dynamodb')
6
7# Specify the table you want to write to
8table = dynamodb.Table('YourTableName')
9
10# Define the items you want to insert
11items_to_insert = [
12    {
13        'PutRequest': {
14            'Item': {
15                'PrimaryKey': '123',
16                'DataField': 'data1'
17            }
18        }
19    },
20    {
21        'PutRequest': {
22            'Item': {
23                'PrimaryKey': '456',
24                'DataField': 'data2'
25            }
26        }
27    },
28    # Add more items here
29]
30
31# Perform batch write operation
32with table.batch_writer() as batch:
33    for item in items_to_insert:
34        batch.put_item(Item=item['PutRequest']['Item'])

Error Handling and Retries

When performing batch writes, it's vital to manage errors and retries gracefully. DynamoDB can return unprocessed items, often due to throttling, which means these items must be retried. The BatchWriteItem response provides a list of unprocessed items, which you can loop over and resend in a subsequent request.

python
1response = table.batch_write_item(RequestItems={'YourTableName': items_to_insert})
2
3unprocessed_items = response.get('UnprocessedItems')
4while unprocessed_items:
5    response = table.batch_write_item(RequestItems=unprocessed_items)
6    unprocessed_items = response.get('UnprocessedItems')

Key Considerations

Throughput and Costs

Using BatchWriteItem, although efficient, still consumes write capacity units (WCUs) similar to individual writes. Each item write counts against your provisioned or on-demand throughput capacity. Monitoring capacity usage and scaling appropriately can help manage costs and performance efficiently.

Conditional Writes

The BatchWriteItem API does not support conditional writes or updates. If you need conditional updates or deletes, you will need to use individual PutItem or DeleteItem operations instead.

Use with Care

Remember that batch operations are atomic only at the item level, not at the request level. Hence, not all items might be processed in the case of an error. Always check for unprocessed items as highlighted and handle them accordingly.

Summary Table

FeatureLimit / Description
Maximum Items per Batch25
Maximum Item Size400 KB
Maximum Request Size16 MB
Supports Conditional OpsNo
Error Retry MechanismCheck UnprocessedItems and retry
Throughput ConsumptionConsumes WCUs for each item written

By understanding and utilizing these features and limitations of the BatchWriteItem API, you can optimize your DynamoDB interactions using Boto3 for performant, cost-effective data operations.


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