API monitoring and alerting in Rest API - Time & Space Complexity
When monitoring APIs, we want to know how the time to check and alert changes as the number of API calls grows.
We ask: How does the work increase when more API requests happen?
Analyze the time complexity of the following code snippet.
// Pseudocode for API monitoring
function monitorAPIs(apiCalls) {
for (let call of apiCalls) {
if (call.status === 'error') {
sendAlert(call);
}
}
}
function sendAlert(call) {
// send alert for the failed API call
}
This code checks each API call for errors and sends an alert if needed.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each API call in the list.
- How many times: Once for every API call received.
As the number of API calls grows, the time to check each one grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of API calls.
Time Complexity: O(n)
This means the time to monitor grows in a straight line with the number of API calls.
[X] Wrong: "Checking all API calls happens instantly no matter how many there are."
[OK] Correct: Each API call needs to be checked one by one, so more calls mean more work and more time.
Understanding how monitoring scales helps you design systems that stay reliable as traffic grows. This skill shows you can think about real-world system behavior.
"What if we only checked API calls that failed instead of all calls? How would the time complexity change?"
Practice
Solution
Step 1: Understand API monitoring
API monitoring means checking the API often to see if it works well without errors.Step 2: Identify the main goal
The main goal is to ensure the API is available and responds correctly to requests.Final Answer:
To regularly check if the API is working correctly -> Option CQuick Check:
API monitoring = regular checks [OK]
- Confusing monitoring with API design
- Thinking monitoring stores data
- Mixing monitoring with coding new endpoints
Solution
Step 1: Understand alert conditions for downtime
An alert for downtime should trigger when the API fails or does not respond, such as status code 500 or no response.Step 2: Evaluate options
Trigger alert if API returns status code 500 or no response correctly triggers alert on error or no response. Other options either alert on success or unrelated events.Final Answer:
Trigger alert if API returns status code 500 or no response -> Option DQuick Check:
Alert on errors or no response = Trigger alert if API returns status code 500 or no response [OK]
- Alerting on successful responses
- Alerting on unrelated changes like docs
- Using wrong status codes for alerts
response = requests.get('https://api.example.com/data')
if response.status_code != 200:
send_alert('API error detected')What happens if the API returns status code 404?
Solution
Step 1: Analyze the condition in the script
The script sends an alert if the status code is not 200.Step 2: Check the status code 404
Since 404 is not 200, the condition is true and alert is sent.Final Answer:
An alert is sent with message 'API error detected' -> Option BQuick Check:
Status code 404 triggers alert [OK]
- Thinking 404 is success
- Assuming script crashes on 404
- Believing script retries automatically
if response.status_code = 500:
send_alert('Server error')Why does this rule not work as expected?
Solution
Step 1: Identify the operator used
The code uses '=' which is assignment, not comparison.Step 2: Correct operator for comparison
To compare values, '==' must be used instead of '='.Final Answer:
The comparison operator should be '==' not '=' -> Option AQuick Check:
Use '==' for comparison [OK]
- Using '=' instead of '==' in conditions
- Thinking 500 is not an error
- Ignoring syntax errors in conditions
Solution
Step 1: Understand 429 status meaning
Status 429 means too many requests temporarily; it may resolve quickly.Step 2: Choose alerting strategy to avoid false alarms
Alerting immediately on one 429 may cause false alarms; waiting for continuous 429s for 5 minutes reduces noise.Final Answer:
Alert only if 429 responses occur continuously for 5 minutes -> Option AQuick Check:
Delay alerts on temporary errors to reduce false alarms [OK]
- Alerting immediately on every 429
- Ignoring 429 which may hide real issues
- Alerting on 429 followed by success
Ready to prove it?
Pass the test and this pattern moves unknown
