Resource-based design thinking in Rest API - Time & Space Complexity
When designing REST APIs using resource-based thinking, it's important to understand how the number of resources affects the work the server does.
We want to know how the time to handle requests grows as the number of resources increases.
Analyze the time complexity of the following REST API endpoint that lists all resources.
GET /api/resources
// Server code example:
function getAllResources() {
const resources = database.getAll();
return resources.map(resource => format(resource));
}
This code fetches all resources from the database and formats each one before sending the response.
Look for repeated actions in the code.
- Primary operation: Looping over all resources to format each one.
- How many times: Once for each resource in the database.
As the number of resources grows, the server does more work formatting each one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 formatting operations |
| 100 | 100 formatting operations |
| 1000 | 1000 formatting operations |
Pattern observation: The work grows directly with the number of resources.
Time Complexity: O(n)
This means the time to handle the request grows in a straight line as the number of resources increases.
[X] Wrong: "Fetching all resources is always fast because databases are optimized."
[OK] Correct: Even if the database is fast, the server still processes each resource one by one, so more resources mean more work and longer time.
Understanding how your API's work grows with data size shows you can design efficient and scalable services, a key skill in real projects.
"What if we added pagination to this API? How would the time complexity change?"
Practice
resource-based design thinking in REST APIs?Solution
Step 1: Understand the concept of resource-based design
Resource-based design means APIs are built around resources, which are things like users, products, or orders.Step 2: Compare options to the concept
Only Organizing APIs around things like users or products correctly describes organizing APIs around things (resources). Other options describe incorrect or unrelated ideas.Final Answer:
Organizing APIs around things like users or products -> Option DQuick Check:
Resource-based design = Organizing APIs around resources [OK]
- Confusing resource-based design with database design
- Thinking all requests must be POST
- Ignoring the concept of resources
Solution
Step 1: Recall HTTP methods and their common uses
GET retrieves data, POST creates new data, PUT updates existing data, DELETE removes data.Step 2: Identify the method for updating
PUT is the standard method used to update an existing resource in REST APIs.Final Answer:
PUT -> Option AQuick Check:
Update resource = PUT [OK]
- Using POST to update instead of create
- Confusing GET with update
- Using DELETE for updating
DELETE /products/123, what is the expected result?Solution
Step 1: Analyze the HTTP method and URL
DELETE method means remove. The URL targets the product resource with ID 123.Step 2: Determine the action
DELETE /products/123 means delete the product with ID 123 from the system.Final Answer:
Delete product with ID 123 -> Option CQuick Check:
DELETE + resource ID = delete that resource [OK]
- Thinking DELETE retrieves data
- Confusing DELETE with POST or PUT
- Ignoring the resource ID in URL
POST /users/45. What is wrong with this request in resource-based design?Solution
Step 1: Understand POST usage in REST
POST is used to create new resources. The URL should point to the collection, not a specific resource ID.Step 2: Analyze the request URL
Including an ID (like /users/45) with POST is incorrect because the server assigns IDs when creating new resources.Final Answer:
The URL should be/userswithout ID for creating -> Option AQuick Check:
POST to collection URL without ID = correct [OK]
- Using POST with resource ID in URL
- Thinking POST updates existing resource
- Confusing POST and PUT URLs
Solution
Step 1: Identify the resource and action
The resource is the specific item (ID 7) inside order 42. Updating quantity means modifying this resource.Step 2: Choose correct HTTP method and URL
PUT is used to update existing resources. The URL should point to the specific item resource: /orders/42/items/7.Final Answer:
PUT /orders/42/items/7 with updated quantity in body -> Option BQuick Check:
Update nested resource = PUT to specific item URL [OK]
- Using POST to update existing item
- Using DELETE to update data
- Using GET with query to change data
Ready to prove it?
Pass the test and this pattern moves unknown
