Transaction error handling in DynamoDB - Time & Space Complexity
When using transactions in DynamoDB, it is important to understand how error handling affects the time it takes to complete operations.
We want to know how the time grows when errors happen and retries occur during transactions.
Analyze the time complexity of this DynamoDB transaction with error handling.
const params = {
TransactItems: [
{ Put: { TableName: 'Orders', Item: orderItem } },
{ Update: { TableName: 'Inventory', Key: itemKey, UpdateExpression: 'SET qty = qty - :dec', ExpressionAttributeValues: { ':dec': 1 } } }
]
};
try {
await dynamodb.transactWrite(params).promise();
} catch (error) {
if (error.code === 'TransactionCanceledException') {
// retry logic here
}
}
This code tries to write multiple items atomically and retries if the transaction is canceled due to conflicts.
Look for repeated actions that affect time.
- Primary operation: The transaction write call that may retry on error.
- How many times: The transaction can retry multiple times until success or failure limit.
Each retry repeats the transaction call, increasing total time.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 retry | 2 transaction calls |
| 3 retries | 4 transaction calls |
| 5 retries | 6 transaction calls |
Pattern observation: More retries mean more repeated transaction calls, so time grows linearly with retries.
Time Complexity: O(r)
This means the time grows linearly with the number of retries due to transaction errors.
[X] Wrong: "Transaction error handling does not affect overall time complexity because each transaction is constant time."
[OK] Correct: Retrying transactions on errors repeats the operation, so total time increases with retries, not just the single transaction time.
Understanding how retries affect time helps you explain real-world database reliability and performance during interviews.
"What if the retry logic included exponential backoff delays? How would that change the time complexity?"
Practice
Solution
Step 1: Understand transaction atomicity
Transactions in DynamoDB ensure that either all operations succeed or none do, preventing partial updates.Step 2: Identify the role of error handling
Error handling detects when a transaction fails and prevents inconsistent data states.Final Answer:
To ensure all changes in a transaction happen together or not at all -> Option AQuick Check:
Transaction atomicity = To ensure all changes in a transaction happen together or not at all [OK]
- Thinking transactions allow partial success
- Confusing error handling with performance tuning
- Assuming automatic backups happen on errors
Solution
Step 1: Check syntax for try-catch with async/await
try { await client.transactWrite(params); } catch (error) { if (error.code === 'TransactionCanceledException') { /* handle */ } } correctly uses try-catch with await and compares error.code with 'TransactionCanceledException'.Step 2: Identify syntax errors in other options
await client.transactWrite(params).catch(error => if error.code == 'TransactionCanceledException' { /* handle */ }) has incorrect arrow function syntax; try { client.transactWrite(params); } catch error { if error.code = 'TransactionCanceledException' { /* handle */ } } misses parentheses and uses wrong assignment; client.transactWrite(params).then().catch(error => if (error.code === 'TransactionCanceledException') { /* handle */ }) uses then() without await which is less clear.Final Answer:
try { await client.transactWrite(params); } catch (error) { if (error.code === 'TransactionCanceledException') { /* handle */ } } -> Option AQuick Check:
Correct try-catch syntax = try { await client.transactWrite(params); } catch (error) { if (error.code === 'TransactionCanceledException') { /* handle */ } } [OK]
- Missing parentheses in catch block
- Using assignment '=' instead of comparison '==='
- Incorrect arrow function syntax in catch
try {
await client.transactWrite(params);
console.log('Transaction succeeded');
} catch (error) {
if (error.code === 'TransactionCanceledException') {
console.log('Transaction failed: ' + error.message);
} else {
console.log('Other error');
}
}Solution
Step 1: Understand error handling flow
If the transaction fails due to a conditional check, DynamoDB throws TransactionCanceledException with a message indicating ConditionalCheckFailed.Step 2: Trace the catch block output
The catch block checks error.code and prints 'Transaction failed: ' plus the error message, so it outputs the failure message.Final Answer:
Transaction failed: ConditionalCheckFailed -> Option CQuick Check:
Error code check prints failure message = Transaction failed: ConditionalCheckFailed [OK]
- Assuming success message prints on failure
- Ignoring error.code check
- Confusing error messages with other exceptions
try {
client.transactWrite(params);
} catch (error) {
if (error.code === 'TransactionCanceledException') {
console.log('Transaction failed');
}
}Solution
Step 1: Check async call handling
client.transactWrite returns a promise; without await, errors won't be caught by try-catch.Step 2: Confirm error handling correctness
The error code string and console.log usage are correct; try-catch is valid for async with await.Final Answer:
Missing await before client.transactWrite causing unhandled promise -> Option DQuick Check:
Async calls need await for try-catch to work [OK]
- Omitting await on async functions
- Misnaming error codes
- Thinking console.log is disallowed in catch
Solution
Step 1: Understand selective retry conditions
Retry should happen only if error.code is 'TransactionCanceledException' and message indicates conditional check failure.Step 2: Implement retry with loop and max attempts
Use a loop with try-catch, check error details, and retry up to a limit to avoid infinite loops.Final Answer:
Use a loop with try-catch; inside catch, check if error.code is 'TransactionCanceledException' and error.message contains 'ConditionalCheckFailed', then retry up to max attempts -> Option BQuick Check:
Retry only on specific error with loop and checks = Use a loop with try-catch; inside catch, check if error.code is 'TransactionCanceledException' and error.message contains 'ConditionalCheckFailed', then retry up to max attempts [OK]
- Retrying on all errors causing infinite loops
- Not checking error.message for conditional failure
- Ignoring max retry limits
Ready to prove it?
Pass the test and this pattern moves unknown
