Client-server architecture in Rest API - Time & Space Complexity
When we use client-server architecture, the client sends requests and the server processes them. We want to understand how the time to handle these requests changes as more requests come in.
How does the server's work grow when many clients ask for data?
Analyze the time complexity of the following code snippet.
// Server receives a list of user IDs
function getUserData(userIds) {
let results = [];
for (let id of userIds) {
let data = database.queryUser(id); // fetch user data
results.push(data);
}
return results;
}
This code fetches user data for each ID sent by the client, one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each user ID to query the database.
- How many times: Once for each user ID in the input list.
As the number of user IDs increases, the server does more database queries, one per ID.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 database queries |
| 100 | 100 database queries |
| 1000 | 1000 database queries |
Pattern observation: The work grows directly with the number of user IDs; doubling the input doubles the work.
Time Complexity: O(n)
This means the server's work grows in a straight line with the number of requests it handles.
[X] Wrong: "The server handles all requests instantly, so time doesn't grow with more users."
[OK] Correct: Each request needs its own processing time, so more requests mean more total work and longer total time.
Understanding how server work grows with requests helps you design better APIs and explain performance clearly in interviews. It shows you know how systems handle real-world loads.
"What if the server could fetch data for all user IDs in one database query? How would the time complexity change?"
Practice
Solution
Step 1: Understand client role
The client is the part of the system that asks for information or services.Step 2: Differentiate from server role
The server responds to client requests by sending data or services.Final Answer:
To request data or services from the server -> Option AQuick Check:
Client requests, server responds [OK]
- Confusing client with server
- Thinking client stores data permanently
- Assuming client controls network hardware
Solution
Step 1: Identify REST API request methods
REST APIs use HTTP methods like GET, POST, PUT, DELETE to communicate.Step 2: Match method to client request
GET is used by clients to request data from servers.Final Answer:
Using an HTTP GET method -> Option BQuick Check:
REST client requests use HTTP methods [OK]
- Confusing SQL with REST API
- Using FTP commands for REST requests
- Thinking local file reads send requests
/users/123 and the user exists?Solution
Step 1: Understand GET request to resource
A GET request to /users/123 asks for user data with ID 123.Step 2: Server response when user exists
If user exists, server sends back user details, usually as JSON.Final Answer:
A JSON object with user details -> Option AQuick Check:
GET existing resource returns data [OK]
- Assuming error if user exists
- Thinking GET causes redirect
- Expecting blank page on success
Solution
Step 1: Understand 404 error meaning
404 means the requested URL or endpoint was not found on the server.Step 2: Link error to cause
If POST returns 404, likely the client sent request to wrong or missing URL.Final Answer:
The server URL endpoint is incorrect or missing -> Option DQuick Check:
404 means URL not found [OK]
- Confusing 404 with method error
- Assuming success on 404
- Thinking data size causes 404
Solution
Step 1: Identify security need
Allowing updates only with valid token means controlling access.Step 2: Apply client-server security concept
Authentication verifies identity; authorization controls permissions on server side.Final Answer:
Authentication and authorization on the server -> Option CQuick Check:
Access control = Authentication + Authorization [OK]
- Thinking client caches control access
- Assuming server sends data without checks
- Believing client can modify database directly
Ready to prove it?
Pass the test and this pattern moves unknown
