Blue-green deployment routing in Nginx - Time & Space Complexity
We want to understand how the routing decisions in blue-green deployment scale as requests increase.
How does nginx handle routing when switching between two versions of an app?
Analyze the time complexity of the following nginx routing configuration for blue-green deployment.
upstream blue {
server 10.0.0.1:8080;
}
upstream green {
server 10.0.0.2:8080;
}
server {
listen 80;
location / {
if ($cookie_version = "blue") {
proxy_pass http://blue;
}
if ($cookie_version = "green") {
proxy_pass http://green;
}
}
}
This code routes user requests to either the blue or green server based on a cookie value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx checks the cookie value for each incoming request.
- How many times: once per request, no loops or recursion inside the config.
Each new request triggers a single cookie check and routing decision.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 cookie checks and routing decisions |
| 100 | 100 cookie checks and routing decisions |
| 1000 | 1000 cookie checks and routing decisions |
Pattern observation: The work grows linearly with the number of requests.
Time Complexity: O(n)
This means the routing work increases directly with the number of requests.
[X] Wrong: "Routing decisions take constant time regardless of request count."
[OK] Correct: Each request requires a routing check, so total work grows with requests, not fixed.
Understanding how routing scales helps you explain real deployment strategies clearly and confidently.
What if we added more versions (blue, green, yellow, etc.)? How would the time complexity change?
Practice
Solution
Step 1: Understand blue-green deployment concept
Blue-green deployment uses two versions of an app (blue and green) to switch traffic smoothly.Step 2: Identify Nginx role in routing
Nginx switches traffic by changingproxy_passbetween blue and green upstreams, avoiding downtime.Final Answer:
To switch traffic between two app versions without downtime -> Option DQuick Check:
Blue-green routing = traffic switch without downtime [OK]
- Confusing blue-green with load balancing
- Thinking it increases server count automatically
- Assuming it handles encryption or caching
Solution
Step 1: Identify directive for routing requests
Theproxy_passdirective tells Nginx where to send incoming requests.Step 2: Understand blue-green switching
Switchingproxy_passbetween blue and green upstream blocks changes the app version served.Final Answer:
proxy_pass -> Option AQuick Check:
proxy_pass switches traffic in blue-green [OK]
- Using listen to switch app versions
- Confusing server_name with routing
- Using root which sets document root, not proxy
upstream blue {
server 10.0.0.1:8080;
}
upstream green {
server 10.0.0.2:8080;
}
server {
listen 80;
location / {
proxy_pass http://green;
}
}Solution
Step 1: Check proxy_pass target
Theproxy_passpoints tohttp://green, so traffic goes to green upstream.Step 2: Identify green upstream server
Green upstream has server10.0.0.2:8080, so this server receives traffic.Final Answer:
10.0.0.2:8080 (green) -> Option BQuick Check:
proxy_pass to green = 10.0.0.2:8080 [OK]
- Assuming traffic goes to blue by default
- Thinking both upstreams get traffic simultaneously
- Believing config has syntax errors
upstream blue {
server 10.0.0.1:8080;
}
upstream green {
server 10.0.0.2:8080
}
server {
listen 80;
location / {
proxy_pass http://blue;
}
}Solution
Step 1: Check upstream block syntax
The green upstream server line lacks a semicolon at the end, causing syntax error.Step 2: Validate other directives
proxy_passwith http is valid; listen can omit IP; upstream names can be lowercase.Final Answer:
Missing semicolon after green server line -> Option AQuick Check:
Missing semicolon causes config error [OK]
- Assuming http must be https in proxy_pass
- Thinking listen needs IP address
- Believing upstream names must be uppercase
Solution
Step 1: Understand zero downtime switch
Changingproxy_passin Nginx config directs traffic to green without downtime.Step 2: Reload Nginx to apply changes
Reloading Nginx applies new config smoothly without dropping connections.Final Answer:
Changeproxy_passin Nginx config from blue to green and reload Nginx -> Option CQuick Check:
proxy_pass switch + reload = zero downtime [OK]
- Stopping blue servers causes downtime
- Relying on DNS changes delays switch
- Restarting Nginx without config change does nothing
Ready to prove it?
Pass the test and this pattern moves unknown
