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?"
Practice
If-None-Match header do in a REST API request?Solution
Step 1: Understand the purpose of
This header sends the ETag value from the client to check if the resource has changed.If-None-MatchStep 2: Determine the server's response based on the ETag
If the ETag matches, the server returns304 Not Modified, meaning no new data is sent.Final Answer:
It asks the server to send data only if it has changed since the last request. -> Option AQuick Check:
If-None-Match checks changes [OK]
- Thinking it forces fresh data every time
- Confusing it with cache deletion
- Assuming it always sends full data
If-None-Match header with ETag value "abc123" in an HTTP request?Solution
Step 1: Recall HTTP header syntax
Headers use colon:to separate name and value, and ETag values are quoted strings.Step 2: Identify correct quoting and separator
If-None-Match: "abc123" uses colon and quotes correctly:If-None-Match: "abc123".Final Answer:
If-None-Match: "abc123" -> Option CQuick Check:
Headers use colon + quoted ETag [OK]
- Omitting quotes around ETag
- Using equals or arrow instead of colon
- Not quoting the ETag string
if request.headers.get('If-None-Match') == current_etag:
return 304
else:
return 200 with data and ETagWhat will the server respond if the client sends
If-None-Match: "xyz789" but current_etag is "abc123"?Solution
Step 1: Compare client's ETag with server's current ETag
The client sends "xyz789" but the server's current ETag is "abc123", so they do not match.Step 2: Determine server response based on mismatch
Since ETags differ, the server sends full data with status 200 and the current ETag.Final Answer:
200 OK with data and ETag -> Option AQuick Check:
ETags differ -> send data [OK]
- Assuming 304 when ETags differ
- Confusing 400 or 500 errors with ETag mismatch
- Ignoring quotes in ETag comparison
If-None-Match but it always returns 200 OK even when ETags match:if request.headers['If-None-Match'] == current_etag:
return 200, data
else:
return 304What is the main error?
Solution
Step 1: Analyze the if condition and returned status codes
The code returns 200 OK when ETags match, but it should return 304 Not Modified instead.Step 2: Identify correct status code usage for ETag match
When ETags match, server returns 304 to avoid sending data; else returns 200 with data.Final Answer:
The 200 and 304 status codes are swapped in the if-else blocks. -> Option DQuick Check:
ETag match -> 304, not 200 [OK]
- Returning 200 on ETag match
- Confusing header access methods
- Ignoring quotes in ETag comparison
If-None-Match. Which of the following sequences correctly describes the client-server interaction to achieve this?Solution
Step 1: Understand the correct use of If-None-Match and 304
The client first gets data and ETag, caches them, then sends If-None-Match with ETag to check for changes.Step 2: Identify the correct sequence
The sequence that starts with an initial request without the ETag header, receives data and ETag from the server, caches both, and then uses If-None-Match on future requests, receiving 304 if unchanged, is correct.Final Answer:
Client sends request without ETag -> Server sends data with ETag -> Client caches data and ETag -> Client sends request withIf-None-Matchheader -> Server returns 304 if data unchanged -> Option BQuick Check:
Proper If-None-Match flow [OK]
- Sending ETag in request body instead of header
- Expecting 304 without sending If-None-Match
- Ignoring caching after 304 response
Ready to prove it?
Pass the test and this pattern moves unknown
