0
0
Rest APIprogramming~5 mins

Statelessness requirement in Rest API - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Statelessness requirement
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.

How Execution Grows With Input

Each request is handled separately, so work grows linearly with number of requests.

Number of Requests (n)Approx. Operations
1010 database lookups
100100 database lookups
10001000 database lookups

Pattern observation: More requests mean proportionally more work, but each request cost stays the same.

Final Time Complexity

Time Complexity: O(n)

This means the total work grows directly with the number of requests, since each is handled independently.

Common Mistake

[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.

Interview Connect

Understanding statelessness helps you explain how APIs scale and why each request cost matters on its own.

Self-Check

"What if the server started caching results between requests? How would the time complexity change?"