429 Too Many Requests in Rest API - Time & Space Complexity
When a server limits how many requests you can send, it affects how fast your program can work.
We want to understand how this limit changes the time your program takes as you send more requests.
Analyze the time complexity of the following code snippet.
// Pseudocode for sending multiple API requests with rate limiting
for (int i = 0; i < n; i++) {
response = sendRequest();
if (response.status == 429) {
waitForReset();
i--;
}
}
This code sends n requests but waits and retries if the server replies with 429 Too Many Requests.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sending API requests inside a loop.
- How many times: Up to n times, but retries increase if 429 occurs.
As you try to send more requests, the total time depends on how often the server limits you.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 requests plus possible waits |
| 100 | 100 requests plus more waits if rate limited |
| 1000 | 1000 requests plus many waits if limits hit often |
Pattern observation: The more requests you send, the more waits you might have, making total time grow roughly linearly.
Time Complexity: O(n)
This means the time grows roughly in direct proportion to the number of requests, but waiting due to 429 can add extra delays.
[X] Wrong: "The program always finishes in time proportional to n without delays."
[OK] Correct: If the server limits requests, the program must wait and retry, which adds extra time beyond just n requests.
Understanding how rate limits affect request timing shows you can think about real-world API behavior and handle delays gracefully.
"What if the server allowed bursts of requests before limiting? How would that change the time complexity?"