Why REST APIs exist - Performance Analysis
We want to understand how the time it takes to use REST APIs changes as the amount of data or requests grows.
How does the work needed grow when more clients or data are involved?
Analyze the time complexity of the following REST API request handling.
GET /items
- Server fetches all items from database
- Server processes each item to prepare response
- Server sends response back to client
This code snippet shows a simple REST API endpoint that returns a list of items by fetching and processing them before sending back.
Look for repeated work that grows with input size.
- Primary operation: Processing each item in the list.
- How many times: Once for every item fetched from the database.
As the number of items grows, the work to process them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 processing steps |
| 100 | 100 processing steps |
| 1000 | 1000 processing steps |
Pattern observation: The work grows directly with the number of items; doubling items doubles the work.
Time Complexity: O(n)
This means the time to handle the request grows in a straight line with the number of items.
[X] Wrong: "The time to get all items stays the same no matter how many items there are."
[OK] Correct: Because the server must look at each item to prepare the response, more items mean more work and more time.
Understanding how REST API work time grows helps you explain real-world system behavior clearly and confidently.
"What if the server cached the items instead of fetching each time? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of REST APIs
REST APIs are designed to let different software programs talk to each other over the internet easily.Step 2: Compare options with this purpose
Only To allow different programs to communicate over the internet using simple rules correctly describes this purpose. Options A, B, and C do not relate to REST APIs' main goal.Final Answer:
To allow different programs to communicate over the internet using simple rules -> Option AQuick Check:
REST APIs exist for communication = D [OK]
- Thinking REST APIs create user interfaces
- Believing REST APIs store data locally only
- Assuming REST APIs replace programming languages
Solution
Step 1: Recall HTTP methods in REST APIs
GET is the method used to request and retrieve data from a server.Step 2: Match the method to the action
POST sends data, DELETE removes data, PUT updates data. Only GET retrieves data.Final Answer:
GET -> Option AQuick Check:
Retrieve data = GET [OK]
- Confusing POST with GET for data retrieval
- Using DELETE or PUT to get data
- Not knowing HTTP method purposes
GET /users/123
Assuming user 123 exists and the API returns JSON data.
Solution
Step 1: Understand the GET request to /users/123
This request asks the server for data about user with ID 123.Step 2: Check the condition given
The user 123 exists, so the server returns the user's data in JSON format.Final Answer:
{"id":123,"name":"Alice"} -> Option BQuick Check:
Existing user GET returns data = C [OK]
- Choosing error responses when user exists
- Confusing 404 with successful data return
- Not recognizing JSON format
POST /users/123
What is wrong if the goal is to update user 123's data?
Solution
Step 1: Understand HTTP methods for REST APIs
POST is usually for creating new data, PUT is for updating existing data.Step 2: Match method to action
Since the goal is to update user 123, PUT should be used instead of POST.Final Answer:
POST is used instead of PUT for updating existing data -> Option CQuick Check:
Update needs PUT, not POST = B [OK]
- Using POST to update instead of PUT
- Thinking GET updates data
- Believing DELETE updates data
Solution
Step 1: Identify methods for listing and adding
GET is used to retrieve or list data, POST is used to create or add new data.Step 2: Match methods with URLs
Both actions use the /products URL; GET lists products, POST adds new products.Final Answer:
GET /products to list, POST /products to add new -> Option DQuick Check:
List = GET, Add = POST = A [OK]
- Swapping GET and POST roles
- Using DELETE or PUT incorrectly
- Confusing URLs for different actions
Ready to prove it?
Pass the test and this pattern moves unknown
