Retry and failure handling in Rest API - Time & Space Complexity
When working with retry and failure handling in REST APIs, it is important to understand how the time taken grows as retries increase.
We want to know how retry attempts affect the total execution time.
Analyze the time complexity of the following retry logic.
max_retries = 3
for attempt in range(max_retries):
response = send_request()
if response.success:
break
This code tries to send a request up to a maximum number of retries until it succeeds.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sending the request (send_request function)
- How many times: Up to
max_retriestimes, depending on success
The number of attempts grows linearly with the maximum retries allowed.
| Input Size (max_retries) | Approx. Operations (send_request calls) |
|---|---|
| 1 | 1 |
| 3 | Up to 3 |
| 10 | Up to 10 |
Pattern observation: The total operations increase directly with the number of retries allowed.
Time Complexity: O(n)
This means the time grows linearly with the number of retry attempts allowed.
[X] Wrong: "Retries happen instantly and do not add to total time."
[OK] Correct: Each retry involves waiting for a response, so total time adds up with each attempt.
Understanding how retries affect time helps you design efficient and reliable APIs, a skill valued in real-world projects.
"What if we added exponential backoff delays between retries? How would the time complexity change?"