Header-based versioning in Rest API - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using header-based versioning in REST APIs, it's important to understand how the server handles requests as the number of clients grows.
We want to know how the time to process requests changes when more clients send requests with different version headers.
Analyze the time complexity of the following code snippet.
def handle_request(request):
version = request.headers.get('API-Version')
if version == '1':
return process_v1(request)
elif version == '2':
return process_v2(request)
else:
return default_response()
This code checks the API version from the request header and routes the request to the correct version handler.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking the header and selecting the matching version handler.
- How many times: Once per request received by the server.
Each request requires a simple check of the header and a call to the correct function.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 header checks and 10 function calls |
| 100 | 100 header checks and 100 function calls |
| 1000 | 1000 header checks and 1000 function calls |
Pattern observation: The work grows directly with the number of requests; each request is handled independently.
Time Complexity: O(n)
This means the time to handle requests grows linearly with the number of requests received.
[X] Wrong: "Checking the header for each request takes longer as more versions are added."
[OK] Correct: The header check is a simple conditional that runs once per request, so adding more versions only adds a few more checks, not a big increase in time per request.
Understanding how request handling scales with the number of clients and versions helps you design APIs that stay fast and reliable as they grow.
"What if we changed from simple if-else checks to searching a list of version handlers? How would the time complexity change?"
Practice
Solution
Step 1: Understand header-based versioning concept
Header-based versioning uses HTTP headers to indicate which API version the client wants.Step 2: Compare with other versioning methods
Unlike URL or query parameter versioning, header-based keeps URLs clean and uses headers instead.Final Answer:
To specify the API version using HTTP headers instead of URLs -> Option BQuick Check:
Header versioning = version in HTTP headers [OK]
- Confusing header versioning with URL versioning
- Thinking version is in request body
- Assuming query parameters are used
Solution
Step 1: Identify common headers for versioning
TheAcceptheader is often used to specify the desired API version by content negotiation.Step 2: Exclude unrelated headers
Content-Typespecifies data format sent,Authorizationis for credentials, andUser-Agentidentifies client software.Final Answer:
Accept -> Option CQuick Check:
Version in Accept header = D [OK]
- Using Content-Type instead of Accept for versioning
- Confusing Authorization with version header
- Thinking User-Agent controls version
Accept: application/vnd.example.v2+jsonWhich API version is the client requesting?
Solution
Step 1: Parse the Accept header value
The valueapplication/vnd.example.v2+jsonincludesv2, indicating version 2.Step 2: Confirm version number meaning
Thev2part is a common pattern to specify version 2 of the API.Final Answer:
Version 2 -> Option AQuick Check:
v2 in Accept header means version 2 [OK]
- Ignoring the 'v2' part and guessing version 1
- Assuming no version if not in URL
- Confusing +json suffix with version
version = request.headers.get('Content-Type')
if version == 'application/vnd.example.v1+json':
return 'Version 1'
else:
return 'Unknown version'What is the main issue here?
Solution
Step 1: Identify header used for versioning
Header-based versioning typically uses theAcceptheader, notContent-Type.Step 2: Explain why Content-Type is wrong here
Content-Typedescribes the data format sent by the client, not the requested API version.Final Answer:
Using Content-Type header instead of Accept header for versioning -> Option DQuick Check:
Version header should be Accept, not Content-Type [OK]
- Checking Content-Type for version info
- Assuming request.headers.get is invalid
- Ignoring else branch
Solution
Step 1: Understand header-based versioning goal
It requires checking the HTTP headers to determine which API version to serve.Step 2: Identify correct version detection method
Checking theAcceptheader for specific version strings and routing requests accordingly is the right approach.Final Answer:
Check the Accept header for 'application/vnd.example.v1+json' or 'application/vnd.example.v2+json' and route accordingly -> Option AQuick Check:
Route by Accept header version strings [OK]
- Mixing header versioning with URL or query parameter versioning
- Ignoring version headers and serving one version only
- Using wrong headers for version detection
