Statelessness requirement in Rest API - Time & Space Complexity
When building REST APIs, statelessness means each request is independent and carries all needed info.
We want to see how this affects the work the server does as requests come in.
Analyze the time complexity of the following REST API request handler.
GET /items?id=123
// Server receives request
// Parses query parameters
// Looks up item by id in database
// Returns item data as response
This code handles one request by fetching an item using its id from storage.
Each request triggers these steps:
- Primary operation: Database lookup by id
- How many times: Once per request
There are no loops or recursion inside a single request; work depends on the request size.
Each request is handled separately, so work grows linearly with number of requests.
| Number of Requests (n) | Approx. Operations |
|---|---|
| 10 | 10 database lookups |
| 100 | 100 database lookups |
| 1000 | 1000 database lookups |
Pattern observation: More requests mean proportionally more work, but each request cost stays the same.
Time Complexity: O(n)
This means the total work grows directly with the number of requests, since each is handled independently.
[X] Wrong: "The server remembers past requests, so handling new ones gets faster."
[OK] Correct: Statelessness means the server does not keep info between requests, so each request takes similar time.
Understanding statelessness helps you explain how APIs scale and why each request cost matters on its own.
"What if the server started caching results between requests? How would the time complexity change?"
Practice
Solution
Step 1: Understand statelessness in REST
Statelessness means the server does not keep any client context between requests.Step 2: Identify what each request must contain
Each request must have all data needed to process it, including authentication and parameters.Final Answer:
Each request from client to server must contain all information needed to understand and process it. -> Option AQuick Check:
Statelessness = All info in each request [OK]
- Thinking server stores session data
- Assuming client sends partial info only once
- Confusing statelessness with authentication persistence
Solution
Step 1: Check if request contains all needed info
GET /profile HTTP/1.1\nHost: example.com\nAuthorization: Bearer token123includes Authorization header, so server can authenticate without stored session.Step 2: Compare with other options
POST /login HTTP/1.1\nHost: example.com\nCookie: sessionid=abc123relies on cookies, which imply server state. Options B and D lack explicit authentication info.Final Answer:
GET request with Authorization header carrying token -> Option DQuick Check:
Auth info in request = stateless [OK]
- Assuming cookies mean statelessness
- Ignoring missing auth info in requests
- Confusing Referer header with authentication
1. GET /data?user=123&token=abc2. GET /data?user=123What will happen on the second request?
Solution
Step 1: Analyze statelessness and authentication
Since server is stateless, it does not remember previous tokens; each request must include token.Step 2: Check second request parameters
Second request lacks token parameter, so server cannot authenticate and must reject it.Final Answer:
Server rejects request due to missing authentication token. -> Option AQuick Check:
Missing token means reject request [OK]
- Assuming server remembers token from first request
- Thinking server ignores missing token
- Believing server crashes on missing params
def handle_request(request):
if 'session_id' in server_memory:
user = server_memory['session_id']
return f"Hello, {user}!"
else:
return "Unauthorized"What is the main problem related to statelessness here?
Solution
Step 1: Identify server memory usage
The code checks 'session_id' in server_memory, meaning it stores client state on server.Step 2: Compare with statelessness principle
Statelessness forbids server from storing client session data between requests.Final Answer:
Code stores session data on server, violating statelessness. -> Option CQuick Check:
Server storing session = stateful, not stateless [OK]
- Thinking server memory use is allowed
- Ignoring missing token checks
- Assuming code is stateless because it returns a string
Solution
Step 1: Understand statelessness in API design
Statelessness means server does not keep client state; all info must come with each request.Step 2: Evaluate options for cart tracking
Require the client to send the full cart data with each request. requires the client to send the full cart data with each request, fitting statelessness. Options A, B, and C rely on server or cookies storing state.Final Answer:
Require client to send full cart data with each request. -> Option BQuick Check:
Client sends all data = stateless API [OK]
- Using server sessions to store cart
- Assuming cookies make API stateless
- Relying on server memory for user data
Ready to prove it?
Pass the test and this pattern moves unknown
