Retry-After header in Rest API - Time & Space Complexity
When using the Retry-After header in REST APIs, it's important to understand how the server handles repeated requests over time.
We want to know how the number of retries affects the server's processing time.
Analyze the time complexity of the following code snippet.
// Pseudocode for handling Retry-After header
function handleRequest(request) {
if (serverIsBusy()) {
return response(503, { 'Retry-After': 10 });
} else {
return processRequest(request);
}
}
This code checks if the server is busy and, if so, tells the client to retry after 10 seconds. Otherwise, it processes the request immediately.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The client retries sending the request after waiting the specified time.
- How many times: Depends on how many times the server responds with Retry-After before processing succeeds.
Each retry causes the server to handle the request again, increasing total processing time linearly with the number of retries.
| Number of Retries (n) | Approx. Server Handling Operations |
|---|---|
| 1 | 1 request handling |
| 5 | 5 request handlings |
| 10 | 10 request handlings |
Pattern observation: The total work grows directly with the number of retries.
Time Complexity: O(n)
This means the total time the server spends grows linearly with the number of retry attempts.
[X] Wrong: "The Retry-After header makes the server handle all retries instantly without extra cost."
[OK] Correct: Each retry is a new request the server must process, so the total work adds up with each retry.
Understanding how retry mechanisms affect server load helps you design APIs that handle traffic smoothly and avoid overload.
What if the Retry-After header used exponential backoff instead of a fixed time? How would the time complexity change?