Graceful degradation in Rest API - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When building REST APIs, graceful degradation means the system still works even if some parts fail.
We want to see how the time cost changes when handling fallback or error cases.
Analyze the time complexity of the following REST API handler with graceful degradation.
async function fetchUserData(userId) {
try {
const profile = await fetch(`/api/profile/${userId}`);
if (!profile.ok) throw new Error('Profile error');
const data = await profile.json();
return data;
} catch {
return { name: 'Guest', status: 'Limited data' };
}
}
This code tries to get user profile data. If it fails, it returns default limited data instead.
Look for repeated or costly steps in the code.
- Primary operation: One network request to fetch user profile.
- How many times: Exactly once per function call, no loops or recursion.
The function makes one request regardless of input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 request |
| 100 | 1 request |
| 1000 | 1 request |
Pattern observation: The number of operations stays the same no matter how big the input is.
Time Complexity: O(1)
This means the time to run the function does not grow with input size; it stays constant.
[X] Wrong: "The time grows with the size of the userId or data returned."
[OK] Correct: The function only makes one request and handles one response, so time depends on that single call, not input size.
Understanding how graceful degradation affects time complexity helps you design APIs that stay efficient even when handling errors or fallback data.
"What if the function made multiple fallback requests in sequence? How would the time complexity change?"
Practice
graceful degradation in REST APIs?Solution
Step 1: Understand graceful degradation purpose
Graceful degradation means the system still works even if some parts fail.Step 2: Compare options with this meaning
Only To keep the API working even if some parts fail matches this idea by keeping the API working despite failures.Final Answer:
To keep the API working even if some parts fail -> Option AQuick Check:
Graceful degradation = keep working despite failure [OK]
- Thinking it stops the API on error
- Assuming errors are ignored without response
- Confusing with performance optimization
Solution
Step 1: Identify error handling syntax
Graceful degradation uses try-catch to handle errors and provide fallback data.Step 2: Match options to this pattern
try { return data } catch { return fallbackData } shows try-catch with fallback, others either stop or ignore errors.Final Answer:
try { return data } catch { return fallbackData } -> Option CQuick Check:
Use try-catch with fallback for graceful degradation [OK]
- Not catching errors properly
- Stopping API on first error
- Ignoring fallback responses
function getUserData() {
try {
return fetchUserFromDB();
} catch (error) {
return { name: "Guest", id: 0 };
}
}What will
getUserData() return if the database fetch fails?Solution
Step 1: Analyze try block behavior
IffetchUserFromDB()works, it returns user data.Step 2: Analyze catch block fallback
If an error occurs, catch returns default user object with name 'Guest' and id 0.Final Answer:
A default user object with name 'Guest' and id 0 -> Option DQuick Check:
Error fallback returns default user object [OK]
- Assuming function crashes on error
- Expecting null instead of fallback object
- Thinking error message is returned
function getData() {
try {
return fetchData();
} catch (error) {
fallbackData;
}
}What is the problem?
Solution
Step 1: Check catch block code
The catch block hasfallbackData;but does not return it.Step 2: Understand function return behavior
Withoutreturn, the function returns undefined on error, breaking graceful degradation.Final Answer:
The fallback data is not returned in the catch block -> Option BQuick Check:
Catch must return fallback data for graceful degradation [OK]
- Forgetting to return fallback data
- Misplacing try-catch blocks
- Assuming catch auto-returns value
Solution
Step 1: Understand graceful degradation in multi-part fetch
It means returning partial data if one part fails, not stopping all.Step 2: Evaluate options for partial fallback
If fetching posts fails, return profile with empty posts list returns profile and empty posts if posts fail, matching graceful degradation.Final Answer:
If fetching posts fails, return profile with empty posts list -> Option AQuick Check:
Partial data returned on failure = graceful degradation [OK]
- Stopping API on any failure
- Returning no data if one part fails
- Ignoring fallback for partial data
