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?"