Validation-based caching in Rest API - Time & Space Complexity
When using validation-based caching in REST APIs, we want to know how the time to check and serve cached data changes as requests grow.
We ask: How does the caching validation affect the speed when many requests come in?
Analyze the time complexity of the following code snippet.
// Pseudocode for validation-based caching
GET /resource
if cache exists for resource:
if cache is still valid:
return cached response
else:
fetch fresh data
update cache
return fresh response
else:
fetch fresh data
store in cache
return fresh response
This code checks if cached data is valid before deciding to fetch fresh data or return cached data.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking cache validity and fetching fresh data if needed.
- How many times: Once per request, repeated for each incoming API call.
As the number of requests grows, each request does a quick cache check and sometimes fetches fresh data.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 cache checks, some fresh fetches |
| 100 | 100 cache checks, some fresh fetches |
| 1000 | 1000 cache checks, some fresh fetches |
Pattern observation: The number of operations grows roughly in direct proportion to the number of requests.
Time Complexity: O(n)
This means the time to handle requests grows linearly with the number of requests, since each request does a quick validation check.
[X] Wrong: "Cache validation makes the response time constant no matter how many requests come."
[OK] Correct: Each request still needs to check the cache, so the total work grows with the number of requests, even if each check is fast.
Understanding how caching affects time complexity helps you explain how APIs stay fast under load, a useful skill for real-world backend work.
"What if the cache validation involved a slow database call? How would the time complexity change?"
Practice
Solution
Step 1: Understand validation-based caching concept
It is used to avoid downloading data again if it has not changed.Step 2: Identify the main purpose
The main goal is to check if data changed before downloading again to save bandwidth and time.Final Answer:
To check if the data has changed before downloading it again -> Option DQuick Check:
Validation-based caching = check data change before download [OK]
- Thinking caching stores data permanently
- Confusing caching with encryption
- Assuming caching skips validation
Solution
Step 1: Identify client headers for validation
The client uses headers to tell the server what version it has.Step 2: Match header for ETag validation
TheIf-None-Matchheader is sent with the ETag value to check if data changed.Final Answer:
If-None-Match -> Option CQuick Check:
Client asks with If-None-Match header [OK]
- Confusing If-Modified-Since with If-None-Match
- Using Cache-Control header for validation
- Thinking ETag is sent by client
If-None-Match: "abc123" header:HTTP/1.1 304 Not Modified ETag: "abc123"
What does this response mean?
Solution
Step 1: Understand HTTP 304 status meaning
304 means Not Modified, so data is unchanged since last fetch.Step 2: Interpret ETag match
ETag matches client's value, so server confirms data is still valid.Final Answer:
The data has not changed; client should use cached data -> Option BQuick Check:
304 Not Modified = use cached data [OK]
- Thinking 304 means new data sent
- Confusing 304 with 200 OK
- Assuming server rejects caching
If-None-Match: "xyz789". The server responds with:HTTP/1.1 200 OK
ETag: "abc123"
{ "data": "new content" }What is the likely problem in this interaction?
Solution
Step 1: Compare client and server ETags
Client sent "xyz789", server responded with "abc123" which is different.Step 2: Understand server response code
Since ETags differ, data changed, so server correctly sends 200 OK with new data.Final Answer:
The server sent a different ETag, indicating data changed, so 200 OK is correct -> Option AQuick Check:
Different ETag means data changed, send 200 OK [OK]
- Expecting 304 when ETags differ
- Thinking client ETag must match server's
- Confusing If-None-Match with If-Match
Solution
Step 1: Identify correct client-server header roles
Client sendsIf-None-Matchwith ETag to ask if data changed.Step 2: Understand server response logic
Server replies 304 if data unchanged, or 200 with new data and new ETag if changed.Final Answer:
Client sends request with If-None-Match header; server replies 304 if data unchanged or 200 with new data and ETag if changed -> Option AQuick Check:
Client sends If-None-Match; server replies 304 or 200 [OK]
- Mixing up who sends ETag and If-None-Match
- Thinking server sends If-None-Match header
- Assuming server never sends 304
Ready to prove it?
Pass the test and this pattern moves unknown
