Rate limit error responses in Rest API - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When a server limits how many requests you can send, it must check each request against the limit.
We want to know how the time to check changes as more requests come in.
Analyze the time complexity of the following code snippet.
// Pseudocode for rate limit check
function checkRateLimit(userId) {
const requests = getRequestsForUser(userId);
const now = currentTime();
const windowStart = now - timeWindow;
const recentRequests = requests.filter(r => r.timestamp >= windowStart);
if (recentRequests.length > maxAllowed) {
return errorResponse("Rate limit exceeded");
}
return successResponse();
}
This code checks how many requests a user made recently and returns an error if they went over the limit.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Filtering the user's request list to find recent requests.
- How many times: Once per check, but the filter checks each past request in the list.
As the number of past requests grows, the filter checks more items each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of stored requests.
Time Complexity: O(n)
This means the time to check grows in a straight line with how many past requests there are.
[X] Wrong: "The rate limit check is always fast and constant time."
[OK] Correct: Because it looks at all past requests, more requests mean more work, so it can slow down.
Understanding how checking rate limits scales helps you design APIs that stay fast even with many users.
"What if we stored only the count of requests in the time window instead of all timestamps? How would the time complexity change?"
Practice
Solution
Step 1: Understand HTTP status codes for errors
HTTP status codes in the 400 range indicate client errors. Among them, 429 specifically means too many requests.Step 2: Identify the code for rate limiting
The 429 status code is defined to signal that the user has sent too many requests in a given time.Final Answer:
429 -> Option BQuick Check:
Rate limit error = 429 [OK]
- Confusing 429 with 404 (not found)
- Using 500 which is server error
- Choosing 401 which means unauthorized
Solution
Step 1: Identify headers related to retry timing
TheRetry-Afterheader is designed to tell clients how long to wait before retrying a request.Step 2: Confirm the correct header for rate limit retry
Other headers like Content-Type or Authorization do not indicate retry timing.Final Answer:
Retry-After -> Option AQuick Check:
Retry timing header = Retry-After [OK]
- Choosing Content-Type which describes data format
- Confusing Authorization with retry info
- Selecting User-Agent which identifies client software
HTTP/1.1 429 Too Many Requests
Retry-After: 120
Content-Type: application/json
{"error": "Rate limit exceeded. Try again later."}Solution
Step 1: Analyze the status code and headers
Status 429 means too many requests. The Retry-After header with value 120 means wait 120 seconds before retrying.Step 2: Interpret the JSON error message
The message confirms the rate limit was exceeded and advises to try again later.Final Answer:
The client sent too many requests and should wait 120 seconds before retrying -> Option DQuick Check:
429 + Retry-After = wait before retry [OK]
- Thinking client can retry immediately
- Confusing 429 with unauthorized error
- Assuming server error from 429
HTTP/1.1 429 Too Many Requests
Content-Type: application/json
{"error": "Too many requests"}
What is missing to improve client handling?Solution
Step 1: Identify missing headers for rate limit response
The response lacks the Retry-After header, which helps clients know when to retry.Step 2: Understand why Retry-After is important
Without Retry-After, clients may retry too soon, causing more errors or confusion.Final Answer:
A Retry-After header indicating when to retry -> Option AQuick Check:
Retry-After header missing = add it [OK]
- Changing status code to 500 which is wrong
- Adding Authorization header unrelated to rate limit
- Removing error message reduces clarity
Solution
Step 1: Choose correct status code for rate limiting
Status 429 is the standard code for rate limit errors, signaling client to slow down.Step 2: Include Retry-After header and clear message
Retry-After header tells client how long to wait. JSON message improves clarity and user experience.Step 3: Evaluate other options
403 is forbidden, not rate limit. 200 means success, which is misleading. 500 is server error, not client rate limit.Final Answer:
Return status 429 with a Retry-After header and a JSON message explaining the limit -> Option CQuick Check:
429 + Retry-After + clear message = best practice [OK]
- Using wrong status codes like 403 or 500
- Returning 200 status for errors
- Omitting Retry-After header
