Avoiding verbs in URLs in Rest API - Time & Space Complexity
When designing REST APIs, the way URLs are structured affects how the server handles requests.
We want to understand how the choice of URL style impacts the number of operations the server performs.
Analyze the time complexity of handling requests with URLs that avoid verbs.
GET /users/123
POST /users
PUT /users/123
DELETE /users/123
This code snippet shows typical REST API URLs using nouns and HTTP methods instead of verbs in the URL path.
Look at what the server does when it receives these requests.
- Primary operation: Searching or updating user data by ID.
- How many times: Once per request, the server looks up the user or modifies data.
The server work depends on how many users exist and how it finds them.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 users | About 10 checks to find one user |
| 100 users | About 100 checks |
| 1000 users | About 1000 checks |
Pattern observation: The time to find or update a user grows as the number of users grows.
Time Complexity: O(n)
This means the server work grows linearly with the number of users when searching or updating.
[X] Wrong: "Using verbs in URLs makes the server faster because it knows the action upfront."
[OK] Correct: The server still needs to find or update data; the URL style does not reduce the search or update work.
Understanding how URL design affects server work helps you build clear and efficient APIs, a skill valued in many projects.
"What if the server used an index to find users instead of searching all? How would the time complexity change?"