SLA and uptime tracking in Rest API - Time & Space Complexity
When tracking SLA and uptime via a REST API, it's important to know how the time to process requests grows as data increases.
We want to understand how the system handles more uptime records or checks.
Analyze the time complexity of the following code snippet.
GET /uptime-records
// Server fetches all uptime records from database
// Then filters records within the requested time range
// Finally calculates uptime percentage for that period
This code fetches and processes uptime data to calculate SLA compliance.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all uptime records to filter and calculate uptime.
- How many times: Once for each uptime record stored in the database.
As the number of uptime records grows, the time to process them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The operations grow directly with the number of records.
Time Complexity: O(n)
This means the time to calculate uptime grows linearly with the number of uptime records.
[X] Wrong: "The time to calculate uptime stays the same no matter how many records there are."
[OK] Correct: Each record must be checked to calculate uptime, so more records mean more work.
Understanding how data size affects API response time helps you design better systems and answer real-world questions confidently.
"What if the API cached pre-calculated uptime percentages? How would the time complexity change?"
Practice
Solution
Step 1: Understand SLA meaning
SLA is a contract that defines expected service performance and uptime.Step 2: Match SLA to options
Only "Service Level Agreement" correctly describes this contract.Final Answer:
Service Level Agreement -> Option AQuick Check:
SLA = Service Level Agreement [OK]
- Confusing SLA with system logs
- Thinking SLA measures latency
- Mixing SLA with server load
Solution
Step 1: Identify REST API methods for data retrieval
GET method is used to request data without changing server state.Step 2: Match method to uptime data retrieval
Since uptime data is fetched, GET is the correct method.Final Answer:
GET -> Option DQuick Check:
Retrieve data with GET [OK]
- Using POST to fetch data
- Confusing DELETE with retrieval
- Using PUT for reading data
{"uptime_percentage": 99.5, "downtime_minutes": 30}What is the actual uptime in minutes if the total period is 1440 minutes (1 day)?
Solution
Step 1: Calculate uptime from total and downtime
Uptime = Total minutes - Downtime minutes = 1440 - 30 = 1410 minutes.Step 2: Confirm with options
1410 minutes matches 1410 minutes. The percentage is a separate metric.Final Answer:
1410 minutes -> Option CQuick Check:
Uptime = 1440 - 30 = 1410 [OK]
- Using downtime as uptime
- Ignoring total period
- Confusing percentage with minutes
import requests
response = requests.get('https://api.example.com/uptime')
data = response.json()
uptime = data['up_time_percentage']
What is the error in this code?
Solution
Step 1: Check JSON key names
The API returns key 'uptime_percentage', but code uses 'up_time_percentage'.Step 2: Identify key mismatch error
This causes a KeyError because the key name is incorrect.Final Answer:
Incorrect key name 'up_time_percentage' instead of 'uptime_percentage' -> Option AQuick Check:
Key name must match JSON exactly [OK]
- Typos in key names
- Using wrong HTTP method
- Forgetting to import requests
{"uptime_percentage": 99.85}Which statement correctly describes the SLA status?
Solution
Step 1: Compare reported uptime to SLA target
The SLA requires at least 99.9%, but actual uptime is 99.85%, which is less.Step 2: Determine SLA compliance
Since 99.85% < 99.9%, the service fails the SLA.Final Answer:
Service fails SLA because uptime is below 99.9% -> Option BQuick Check:
Actual uptime < SLA target means failure [OK]
- Assuming close enough meets SLA
- Ignoring SLA threshold
- Confusing downtime with uptime
Ready to prove it?
Pass the test and this pattern moves unknown
