Error handling and retries in DynamoDB - Time & Space Complexity
When working with DynamoDB, handling errors and retrying requests is important to keep your app running smoothly.
We want to understand how retrying affects the time it takes to finish a request.
Analyze the time complexity of this retry logic for a DynamoDB request.
const maxRetries = 3;
let attempt = 0;
let success = false;
while (attempt < maxRetries && !success) {
try {
await dynamoDBClient.putItem(params);
success = true;
} catch (error) {
attempt++;
}
}
This code tries to write an item to DynamoDB, retrying up to 3 times if there is an error.
Look at what repeats in this code.
- Primary operation: The DynamoDB putItem request inside the loop.
- How many times: Up to maxRetries times (3 in this case) if errors happen.
Imagine the number of retries grows with how often errors occur.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 (no errors) | 1 request |
| 2 (1 error) | 2 requests |
| 3 (2 errors) | 3 requests |
Pattern observation: The number of requests grows linearly with the number of retries needed.
Time Complexity: O(r)
This means the time grows linearly with the number of retries attempted.
[X] Wrong: "Retries happen instantly and don't add to time."
[OK] Correct: Each retry is a full request that takes time, so more retries mean more total time.
Understanding how retries affect performance shows you can think about real-world system behavior, which is a valuable skill in interviews.
"What if we added exponential backoff delays between retries? How would that change the time complexity?"