If-None-Match and 304 responses in Rest API - Time & Space Complexity
We want to understand how the server handles requests using If-None-Match headers and 304 responses.
How does the server's work change as more requests come in?
Analyze the time complexity of the following code snippet.
GET /resource HTTP/1.1
If-None-Match: "etag-value"
// Server checks if the resource's current ETag matches the If-None-Match value
if (request.headers['If-None-Match'] === resource.etag) {
return 304 Not Modified;
} else {
return 200 OK with resource data;
}
This code checks if the client's cached version matches the server's current version and decides whether to send the full data or just a 304 response.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing the If-None-Match header value with the resource's ETag.
- How many times: Once per request received by the server.
Each request requires one comparison to decide the response.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 requests | 10 comparisons |
| 100 requests | 100 comparisons |
| 1000 requests | 1000 comparisons |
Pattern observation: The work grows directly with the number of requests, one check per request.
Time Complexity: O(n)
This means the server's work increases linearly with the number of requests it handles.
[X] Wrong: "The server does a complex search or multiple comparisons for each request."
[OK] Correct: The server only compares one ETag string per request, so the work is simple and direct.
Understanding how servers handle conditional requests helps you explain efficient web communication and caching in real projects.
"What if the server had to check multiple ETags for each request? How would the time complexity change?"