API versioning with routing in Nginx - Time & Space Complexity
When using nginx to route API requests based on version, it is important to understand how the routing decisions scale as more versions or routes are added.
We want to know how the time to find the correct route grows when the number of API versions increases.
Analyze the time complexity of the following nginx configuration snippet for API version routing.
server {
listen 80;
location /api/v1/ {
proxy_pass http://backend_v1;
}
location /api/v2/ {
proxy_pass http://backend_v2;
}
location /api/v3/ {
proxy_pass http://backend_v3;
}
}
This configuration routes requests to different backend servers based on the API version in the URL path.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx checks each
locationblock in order to find a matching prefix for the request URL. - How many times: It compares the request path against each version prefix until it finds a match or exhausts the list.
As the number of API versions (and thus location blocks) increases, nginx must check more prefixes to find the right route.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 versions | Up to 3 prefix checks |
| 10 versions | Up to 10 prefix checks |
| 100 versions | Up to 100 prefix checks |
Pattern observation: The number of prefix checks grows linearly with the number of API versions configured.
Time Complexity: O(n)
This means the time to route a request grows linearly as more API versions are added.
[X] Wrong: "nginx instantly finds the correct API version route regardless of how many versions exist."
[OK] Correct: nginx checks each location prefix in order, so more versions mean more checks before matching.
Understanding how routing scales with configuration size helps you design efficient API gateways and shows you can reason about system performance beyond code.
"What if nginx used a hash map for location prefixes instead of checking them in order? How would the time complexity change?"
Practice
Solution
Step 1: Understand API versioning concept
API versioning allows different versions of an API to coexist, so clients using older versions still work.Step 2: Role of nginx routing
nginx useslocationblocks to route requests to the correct API version based on the URL path.Final Answer:
To run multiple API versions side by side and route requests accordingly -> Option CQuick Check:
API versioning = multiple versions side by side [OK]
- Thinking API versioning disables old versions
- Assuming all versions merge into one endpoint
- Confusing routing with blocking requests
/api/v1/?Solution
Step 1: Understand nginx location matching
Usinglocation /api/v1/matches all requests starting with/api/v1/, including subpaths.Step 2: Check syntax correctness
location /api/v1/ { proxy_pass http://backend_v1; } uses correct syntax without wildcards or regex, which nginx does not support in this form.Final Answer:
location /api/v1/ { proxy_pass http://backend_v1; } -> Option AQuick Check:
Trailing slash in location matches subpaths [OK]
- Using wildcards (*) in location which nginx does not support
- Missing trailing slash causing partial matches
- Using regex syntax without ~
/api/v2/users?
location /api/v1/ {
proxy_pass http://backend_v1;
}
location /api/v2/ {
proxy_pass http://backend_v2;
}Solution
Step 1: Match request path to location blocks
The request path/api/v2/usersstarts with/api/v2/, so it matches the second location block.Step 2: Determine proxy backend
The matching location proxies tohttp://backend_v2, so this backend handles the request.Final Answer:
http://backend_v2 -> Option AQuick Check:
Request path prefix matches location prefix [OK]
- Choosing backend_v1 for /api/v2/ path
- Assuming nginx load balances both backends automatically
- Thinking unmatched paths cause proxy to backend_v1
location /api/v1 {
proxy_pass http://backend_v1;
}
location /api/v2/ {
proxy_pass http://backend_v2;
}Solution
Step 1: Check location path consistency
The first location uses/api/v1without trailing slash, which matches only exact path or prefix differently than/api/v1/.Step 2: Understand routing impact
Requests like/api/v1/userswon't match the first location correctly, causing routing errors.Final Answer:
Missing trailing slash in first location causes incorrect routing -> Option DQuick Check:
Trailing slash in location matters for prefix matching [OK]
- Ignoring trailing slash difference in location paths
- Thinking proxy_pass URL must have trailing slash
- Believing location blocks cannot be next to each other
/api/ without version to v2 by default. Which config snippet achieves this correctly?Solution
Step 1: Define explicit version locations
Locations for/api/v1/and/api/v2/proxy requests to respective backends.Step 2: Redirect base /api/ to default version
Usingreturn 301 /api/v2/;in/api/location sends clients to v2 URL, ensuring default version usage.Final Answer:
location /api/v1/ { proxy_pass http://backend_v1; } location /api/v2/ { proxy_pass http://backend_v2; } location /api/ { return 301 /api/v2/; } -> Option BQuick Check:
Redirect base path to default version with return 301 [OK]
- Proxying /api/ directly instead of redirecting
- Overlapping location blocks causing conflicts
- Using proxy_pass in /api/ without redirect
Ready to prove it?
Pass the test and this pattern moves unknown
