REST constraints and principles in Rest API - Time & Space Complexity
When working with REST APIs, it's important to understand how the design rules affect how fast the system responds as more requests come in.
We want to know how the time to handle requests grows when the number of clients or data increases.
Analyze the time complexity of a REST API handling requests with stateless constraints.
GET /items/{id} // Fetch a single item by ID
POST /items // Create a new item
GET /items // Fetch list of all items
// Each request is independent and contains all needed info
// Server does not store client session data
This code snippet shows typical REST API calls following statelessness and resource-based design.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Server processes each incoming request independently.
- How many times: Once per request; no repeated server-side loops across requests.
Each request is handled separately, so the time depends on the size of the data requested or sent.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 items | Processes 10 items in list fetch |
| 100 items | Processes 100 items in list fetch |
| 1000 items | Processes 1000 items in list fetch |
Pattern observation: The time grows roughly in proportion to the amount of data handled per request, but each request is independent.
Time Complexity: O(n)
This means the time to handle a request grows linearly with the size of the data involved in that request.
[X] Wrong: "Because REST is stateless, the server does not do any work per request."
[OK] Correct: Stateless means no stored session data, but the server still processes each request fully, so work depends on request size.
Understanding how REST constraints affect request handling time helps you design APIs that scale well and respond quickly, a key skill in real projects.
"What if the server started storing session data between requests? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of statelessness in REST
Statelessness means the server does not store any client context between requests. Each request must be self-contained.Step 2: Match the constraint to the description
The description says each request must have all needed info, which matches statelessness.Final Answer:
Statelessness -> Option CQuick Check:
Request self-contained = Statelessness [OK]
- Confusing cacheability with statelessness
- Thinking layered system means stateless
- Assuming code on demand is required
Solution
Step 1: Recall safe and idempotent HTTP methods
GET, PUT, and DELETE are idempotent; GET is safe. POST is neither safe nor idempotent.Step 2: Identify the method that is not safe and idempotent
POST changes server state and can create multiple resources, so it is not idempotent or safe.Final Answer:
POST -> Option AQuick Check:
POST is not safe or idempotent [OK]
- Thinking PUT is unsafe
- Confusing DELETE as unsafe
- Assuming GET modifies data
Cache-Control: max-age=3600 ETag: "abc123"
Solution
Step 1: Analyze the headers Cache-Control and ETag
Cache-Control and ETag headers control how responses are cached by clients or intermediaries.Step 2: Identify the REST constraint related to caching
Cacheability means responses can be stored and reused to improve performance.Final Answer:
Cacheability -> Option AQuick Check:
Cache headers = Cacheability [OK]
- Confusing cache headers with statelessness
- Thinking uniform interface relates to headers
- Assuming client-server means caching
Solution
Step 1: Identify the violated constraint
Storing session data on server between requests breaks statelessness because server keeps client state.Step 2: Suggest a fix to restore statelessness
Move session info to client (like tokens) or send all needed info in each request.Final Answer:
Statelessness; move session data to client or include in each request -> Option DQuick Check:
Server session storage breaks statelessness [OK]
- Confusing cacheability with session storage
- Thinking layered system relates to sessions
- Trying to fix by changing URLs
Solution
Step 1: Identify the HTTP method for partial updates
PATCH is designed for partial updates, unlike PUT which replaces entire resource.Step 2: Connect the method to the REST principle
Uniform Interface means using standard methods consistently; PATCH fits this principle for partial updates.Final Answer:
PATCH method and Uniform Interface constraint -> Option BQuick Check:
Partial update = PATCH + Uniform Interface [OK]
- Using PUT for partial updates
- Confusing POST with PATCH
- Linking DELETE to updates
Ready to prove it?
Pass the test and this pattern moves unknown
