BatchWriteItem in DynamoDB - Time & Space Complexity
When using BatchWriteItem in DynamoDB, it's important to understand how the time it takes grows as you add more items to write.
We want to know: how does the execution time change when we increase the number of items in the batch?
Analyze the time complexity of the following code snippet.
const params = {
RequestItems: {
'MyTable': [
{ PutRequest: { Item: { id: '1', data: 'A' } } },
{ PutRequest: { Item: { id: '2', data: 'B' } } },
// ... up to 25 items
]
}
};
const result = await dynamodb.batchWrite(params).promise();
This code sends a batch write request to DynamoDB to insert up to 25 items in one call.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Writing each item in the batch to the database.
- How many times: Once per item in the batch, up to 25 items per request.
As you add more items to the batch, the time to process grows roughly in direct proportion to the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 write operations |
| 25 | About 25 write operations (maximum per batch) |
| 50 | Two batches of 25 writes each, so about 50 write operations |
Pattern observation: The time grows linearly with the number of items, but each batch can only hold up to 25 items.
Time Complexity: O(n)
This means the time to complete the batch write grows directly with the number of items you want to write.
[X] Wrong: "BatchWriteItem writes all items instantly regardless of batch size."
[OK] Correct: Each item still requires a write operation, so more items mean more work and more time.
Understanding how batch operations scale helps you design efficient database interactions and shows you can think about performance in real applications.
"What if we split a batch of 50 items into two parallel BatchWriteItem calls? How would the time complexity change?"
Practice
What is the main purpose of the BatchWriteItem operation in DynamoDB?
Solution
Step 1: Understand BatchWriteItem functionality
The BatchWriteItem operation allows you to write (add or delete) multiple items in one request to save time and reduce network calls.Step 2: Compare options with BatchWriteItem purpose
Only To add or delete multiple items in a single request correctly describes adding or deleting multiple items in one request, which matches BatchWriteItem's purpose.Final Answer:
To add or delete multiple items in a single request -> Option CQuick Check:
BatchWriteItem = add/delete many items [OK]
- Confusing BatchWriteItem with BatchGetItem
- Thinking it updates items instead of add/delete
- Assuming it creates tables
Which of the following is the correct structure for a BatchWriteItem request in DynamoDB?
{
"RequestItems": {
"TableName": [
{ "PutRequest": { "Item": { ... } } },
{ "DeleteRequest": { "Key": { ... } } }
]
}
}Solution
Step 1: Review BatchWriteItem request format
The RequestItems key maps table names to arrays of write requests, each being either PutRequest or DeleteRequest.Step 2: Match options to correct format
RequestItems contains table names as keys and arrays of PutRequest or DeleteRequest objects as values correctly describes the structure: table names as keys, arrays of PutRequest/DeleteRequest as values.Final Answer:
RequestItems contains table names as keys and arrays of PutRequest or DeleteRequest objects as values -> Option BQuick Check:
RequestItems = table name keys + Put/Delete arrays [OK]
- Using array of table names instead of object with table keys
- Omitting DeleteRequest option
- Not nesting PutRequest/DeleteRequest inside arrays
Given the following BatchWriteItem request, what will be the result if one item fails due to exceeding provisioned throughput?
{
"RequestItems": {
"Products": [
{ "PutRequest": { "Item": { "ID": { "S": "101" }, "Name": { "S": "Pen" } } } },
{ "PutRequest": { "Item": { "ID": { "S": "102" }, "Name": { "S": "Notebook" } } } }
]
}
}Solution
Step 1: Understand BatchWriteItem error handling
If some items exceed provisioned throughput, DynamoDB returns those items in UnprocessedItems for retry, not failing the entire batch.Step 2: Analyze the given scenario
One item fails due to throughput, so only that item is unprocessed and returned for retry, while others succeed.Final Answer:
Only the failed item is returned in UnprocessedItems for retry -> Option DQuick Check:
UnprocessedItems = failed items for retry [OK]
- Assuming all items fail if one fails
- Thinking BatchWriteItem throws syntax error on throughput issues
- Believing all items succeed regardless of errors
Identify the error in this BatchWriteItem request snippet:
{
"RequestItems": {
"Orders": [
{ "PutRequest": { "Item": { "OrderId": "123", "Amount": 50 } } }
]
}
}Solution
Step 1: Check attribute value format in BatchWriteItem
DynamoDB requires attribute values to specify data types like {"S": "string"} or {"N": "number"} in BatchWriteItem requests.Step 2: Identify the error in the snippet
Attributes "OrderId" and "Amount" lack data type wrappers, so the request is invalid.Final Answer:
The attribute values are missing DynamoDB data types -> Option AQuick Check:
Attribute values need data types like {"S": "text"} [OK]
- Omitting data types for attribute values
- Misunderstanding table name structure
- Placing PutRequest incorrectly
You want to write 50 items to a DynamoDB table using BatchWriteItem. Since the API limits 25 items per batch, how should you handle this in your application?
Solution
Step 1: Recognize BatchWriteItem limits
BatchWriteItem supports a maximum of 25 items per request, so 50 items must be split into multiple batches.Step 2: Implement batching and retry logic
Split 50 items into two batches of 25 each. After each batch, check UnprocessedItems and retry those to ensure all items are written.Final Answer:
Split the 50 items into two batches of 25 and retry unprocessed items after each batch -> Option AQuick Check:
BatchWriteItem max 25 items; split and retry [OK]
- Sending more than 25 items in one batch
- Ignoring unprocessed items and not retrying
- Using BatchGetItem to write items
Ready to prove it?
Pass the test and this pattern moves unknown
