Transaction limits in DynamoDB - Time & Space Complexity
When using transactions in DynamoDB, it is important to understand how the number of operations affects performance.
We want to know how the time to complete a transaction grows as we add more operations inside it.
Analyze the time complexity of the following DynamoDB transaction code.
const params = {
TransactItems: [
{ Put: { TableName: 'Orders', Item: order1 } },
{ Update: { TableName: 'Inventory', Key: itemKey, UpdateExpression: 'SET qty = qty - :dec', ExpressionAttributeValues: { ':dec': 1 } } },
// ... more operations ...
]
};
await dynamodb.transactWriteItems(params).promise();
This code runs a transaction with multiple write operations in DynamoDB.
Look at what repeats as the transaction size grows.
- Primary operation: Each write or update inside the transaction.
- How many times: Equal to the number of operations inside the TransactItems array.
As you add more operations to the transaction, the total work grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 5 | 5 write/update actions |
| 10 | 10 write/update actions |
| 25 | 25 write/update actions |
Pattern observation: Doubling the number of operations roughly doubles the work done.
Time Complexity: O(n)
This means the time to complete the transaction grows linearly with the number of operations inside it.
[X] Wrong: "Adding more operations inside a transaction does not affect performance much because it's one request."
[OK] Correct: Each operation inside the transaction adds work, so more operations mean more time and resources used.
Understanding how transaction size affects performance shows you can reason about real database limits and costs, a useful skill in many jobs.
"What if we split one large transaction into multiple smaller transactions? How would the time complexity change?"
Practice
TransactWriteItems request in DynamoDB?Solution
Step 1: Understand DynamoDB transaction limits
DynamoDB limits the number of operations in a transaction to ensure performance and consistency.Step 2: Recall the specific limit for TransactWriteItems
The limit forTransactWriteItemsis 10 operations per transaction.Final Answer:
10 operations -> Option DQuick Check:
TransactWriteItems max ops = 10 [OK]
- Confusing write and read transaction limits
- Assuming more than 10 operations are allowed in write transactions
- Mixing up TransactWriteItems with TransactGetItems limits
TransactWriteItems?Solution
Step 1: Identify the correct parameter name for transaction items
The DynamoDB API forTransactWriteItemsrequires the parameterTransactItemsto specify operations.Step 2: Match the syntax with the correct parameter
Only dynamodb.transactWriteItems({ TransactItems: [...] }) usesTransactItemscorrectly inside the method call.Final Answer:
dynamodb.transactWriteItems({ TransactItems: [...] }) -> Option BQuick Check:
Use TransactItems for write transactions [OK]
- Using 'Items' instead of 'TransactItems'
- Calling a non-existent method like 'transactWrite'
- Confusing parameter names for transaction operations
TransactGetItems with 30 get operations. What will happen when this transaction is executed?Solution
Step 1: Recall the operation limit for TransactGetItems
DynamoDB limitsTransactGetItemsto a maximum of 25 operations per transaction.Step 2: Analyze the effect of exceeding the limit
Submitting 30 operations exceeds this limit, causing the entire transaction to fail.Final Answer:
The transaction fails because it exceeds the 25 operation limit -> Option AQuick Check:
TransactGetItems max ops = 25 [OK]
- Assuming partial success with extra operations
- Thinking the limit is 30 or more
- Believing the transaction truncates results automatically
TransactWriteItems. The transaction fails. What is the most likely cause?Solution
Step 1: Check the operation count limit for TransactWriteItems
The maximum allowed operations inTransactWriteItemsis 10.Step 2: Identify the cause of failure with 12 operations
Since 12 exceeds 10, the transaction fails due to exceeding the operation limit.Final Answer:
Exceeded the maximum of 10 operations allowed in TransactWriteItems -> Option CQuick Check:
More than 10 ops in write transaction causes failure [OK]
- Confusing operation count with item size limits
- Assuming 25 ops limit applies to write transactions
- Ignoring parameter name errors as cause
Solution
Step 1: Understand separate transaction types and their limits
DynamoDB separates read and write transactions:TransactWriteItemsfor writes (max 10 ops),TransactGetItemsfor reads (max 25 ops).Step 2: Plan transactions to respect limits
Perform writes (8 ops) in oneTransactWriteItemsand reads (20 ops) in oneTransactGetItemsto stay within limits.Final Answer:
Use one TransactWriteItems with 8 writes and one TransactGetItems with 20 reads separately -> Option AQuick Check:
Separate read/write transactions to respect limits [OK]
- Trying to combine reads and writes in one transaction
- Exceeding operation limits by mixing types
- Assuming one transaction can handle both reads and writes
Ready to prove it?
Pass the test and this pattern moves unknown
