Canary deployments in Nginx - Time & Space Complexity
We want to understand how the work done by nginx changes when using canary deployments.
Specifically, how does the number of requests affect nginx's routing decisions during canary releases?
Analyze the time complexity of the following nginx configuration snippet for canary deployment.
upstream backend {
server backend-v1.example.com weight=9;
server backend-v2.example.com weight=1 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
This config sends most traffic to backend-v1 and a small weighted portion to backend-v2 as a canary.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: For each incoming request, nginx selects a backend server from the upstream list.
- How many times: This selection happens once per request, repeating for every request received.
As the number of requests increases, nginx performs the server selection for each request independently.
| Input Size (n requests) | Approx. Operations |
|---|---|
| 10 | 10 server selections |
| 100 | 100 server selections |
| 1000 | 1000 server selections |
Pattern observation: The work grows linearly with the number of requests.
Time Complexity: O(n)
This means the time nginx spends routing grows directly with the number of requests it handles.
[X] Wrong: "Adding a canary server makes nginx slower by a lot because it loops through all servers for each request."
[OK] Correct: nginx uses efficient algorithms to pick a server, so adding a small number of servers does not cause a big slowdown per request.
Understanding how nginx handles requests during canary deployments shows you how real systems balance new and stable versions smoothly.
"What if we increased the number of backend servers in the upstream group? How would the time complexity change?"
Practice
Solution
Step 1: Understand canary deployment concept
Canary deployment means sending some traffic to a new version while most traffic still goes to the old version.Step 2: Identify NGINX role in canary deployment
NGINX controls traffic split using weights in upstream blocks to route requests gradually.Final Answer:
To gradually route traffic between old and new application versions -> Option AQuick Check:
Canary deployment = gradual traffic routing [OK]
- Thinking canary blocks all traffic
- Confusing canary with load increase
- Assuming canary disables logging
Solution
Step 1: Understand weight meaning in NGINX upstream
Weight controls traffic share; higher weight means more traffic.Step 2: Match weights to desired traffic split
Old version should get 90%, new version 10%, so old weight=90, new weight=10.Final Answer:
upstream backend { server old.example.com weight=90; server new.example.com weight=10; } -> Option AQuick Check:
Weight 90 old, 10 new = 90% old traffic [OK]
- Swapping weights between old and new servers
- Omitting weights defaults to equal traffic
- Setting weight=0 disables server completely
upstream backend {
server old.example.com weight=80;
server new.example.com weight=20;
}
What percentage of traffic will be routed to new.example.com?Solution
Step 1: Calculate total weight
Total weight = 80 (old) + 20 (new) = 100.Step 2: Calculate new server traffic percentage
New server traffic = (20 / 100) * 100% = 20%.Final Answer:
20% -> Option BQuick Check:
New server weight 20 of 100 total = 20% traffic [OK]
- Confusing weight with percentage directly
- Adding weights incorrectly
- Assuming equal traffic without weights
upstream backend {
server old.example.com weight=70
server new.example.com weight=30;
}
But NGINX fails to start. What is the error?Solution
Step 1: Check syntax of upstream block
Each server directive must end with a semicolon in NGINX config.Step 2: Identify missing semicolon
First server line lacks semicolon after weight=70, causing syntax error.Final Answer:
Missing semicolon after first server directive -> Option CQuick Check:
Semicolons required after each directive [OK]
- Ignoring missing semicolon errors
- Thinking weights must be equal
- Assuming server names must be IPs
Solution
Step 1: Understand canary traffic control in NGINX
NGINX uses weights in upstream blocks to control traffic percentage per server.Step 2: Adjust weights gradually and reload NGINX
Manually changing weights from 5 to 50 and reloading NGINX safely shifts traffic as needed.Final Answer:
Manually updateweightvalues inupstreamblock and reload NGINX -> Option DQuick Check:
Weight changes + reload = traffic control [OK]
- Using proxy_pass without weights for traffic split
- Setting weight=0 disables server, not gradual traffic
- Using limit_conn limits connections, not traffic percentage
Ready to prove it?
Pass the test and this pattern moves unknown
