0
0
Rest APIprogramming~5 mins

Response headers (Cache-Control, ETag) in Rest API - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Response headers (Cache-Control, ETag)
O(n)
Understanding Time Complexity

When working with response headers like Cache-Control and ETag, it's important to understand how their use affects the time your API takes to respond.

We want to know how the time to handle these headers changes as the number of requests or resources grows.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

GET /resource HTTP/1.1
Host: example.com

Response:
Cache-Control: max-age=3600
ETag: "abc123"
Content-Type: application/json
{
  "data": "..."
}

This snippet shows a server sending Cache-Control and ETag headers with a resource response to help clients cache data efficiently.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Checking and generating ETag for each resource requested.
  • How many times: Once per request, but depends on resource size and number of requests.
How Execution Grows With Input

As the number of requests or resource size grows, the server must generate or verify ETags and set Cache-Control headers for each response.

Input Size (n)Approx. Operations
10 requests10 ETag checks and header sets
100 requests100 ETag checks and header sets
1000 requests1000 ETag checks and header sets

Pattern observation: The work grows linearly with the number of requests handled.

Final Time Complexity

Time Complexity: O(n)

This means the time to handle response headers grows directly with the number of requests processed.

Common Mistake

[X] Wrong: "ETag generation is free and does not affect response time."

[OK] Correct: Generating or verifying ETags requires reading or hashing resource data, which takes time proportional to resource size and request count.

Interview Connect

Understanding how response headers affect performance shows you can think about real-world API efficiency and scalability, a valuable skill in software development.

Self-Check

"What if we cache ETag values instead of generating them on every request? How would the time complexity change?"