TransactWriteItems in DynamoDB - Time & Space Complexity
When using TransactWriteItems in DynamoDB, it is important to understand how the time to complete the operation changes as you add more items to write.
We want to know how the total work grows when we write multiple items in a single transaction.
Analyze the time complexity of the following code snippet.
const params = {
TransactItems: [
{ Put: { TableName: 'MyTable', Item: { id: '1', data: 'A' } } },
{ Update: { TableName: 'MyTable', Key: { id: '2' }, UpdateExpression: 'set data = :val', ExpressionAttributeValues: { ':val': 'B' } } },
{ Delete: { TableName: 'MyTable', Key: { id: '3' } } }
]
};
dynamodb.transactWriteItems(params).promise();
This code sends a transaction with three write operations: one put, one update, and one delete.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Each write action inside the TransactItems array is processed one by one.
- How many times: The number of write actions equals the length of the TransactItems array (n).
As you add more write actions to the transaction, the total work grows roughly in direct proportion to the number of actions.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 write actions processed |
| 100 | About 100 write actions processed |
| 1000 | About 1000 write actions processed |
Pattern observation: The total work increases linearly as you add more items to the transaction.
Time Complexity: O(n)
This means the time to complete the transaction grows directly with the number of write actions included.
[X] Wrong: "All writes in TransactWriteItems happen instantly no matter how many items there are."
[OK] Correct: Each write action takes some time, so more actions mean more total time. The transaction does not magically run all writes at once.
Understanding how transaction size affects performance helps you design better database operations and shows you can think about efficiency in real systems.
"What if we split a large TransactWriteItems call into multiple smaller transactions? How would the time complexity change?"