Blue-green deployment for models in MLOps - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to switch between two model versions grows as the size of the model or traffic increases.
How does the deployment process scale when moving from one model to another?
Analyze the time complexity of the following deployment switching code.
# Assume models are deployed in two environments: blue and green
# Switch traffic from blue to green
def switch_traffic(traffic_router, green_model_endpoint):
# Update routing rules to point to green model
for user in traffic_router.users:
traffic_router.route[user] = green_model_endpoint
return "Switched to green model"
This code switches user traffic routing from the blue model to the green model by updating routing rules for each user.
Look for loops or repeated steps in the code.
- Primary operation: Loop over all users to update routing.
- How many times: Once per user in the traffic router.
As the number of users grows, the time to update routing grows too.
| Input Size (n users) | Approx. Operations |
|---|---|
| 10 | 10 updates |
| 100 | 100 updates |
| 1000 | 1000 updates |
Pattern observation: The time grows directly with the number of users; doubling users doubles the work.
Time Complexity: O(n)
This means the time to switch models grows linearly with the number of users to update.
[X] Wrong: "Switching traffic is instant and does not depend on user count."
[OK] Correct: Each user's routing must be updated, so more users mean more updates and more time.
Understanding how deployment time scales helps you design smoother model updates and shows you think about real-world system behavior.
"What if the routing update was done in batches instead of per user? How would the time complexity change?"
Practice
Solution
Step 1: Understand blue-green deployment concept
Blue-green deployment uses two separate environments to avoid downtime and risk during updates.Step 2: Identify the key purpose
The main goal is to switch traffic to the new model only after it is fully tested and ready, ensuring safety.Final Answer:
To switch traffic to a new model only after it is fully tested and ready -> Option BQuick Check:
Safe model update = A [OK]
- Thinking both models run and combine outputs
- Updating production without backup
- Deploying only during off-peak hours
Solution
Step 1: Understand traffic switching in Kubernetes
Traffic is routed by the service selector labels pointing to the correct deployment environment.Step 2: Identify the command that changes service selector to green
The patch command updates the service selector to point to pods labeled with "env=green".Final Answer:
kubectl patch service model-service -p '{"spec":{"selector":{"env":"green"}}}' -> Option DQuick Check:
Change service selector = B [OK]
- Restarting deployment does not switch traffic
- Setting image changes deployment but not traffic
- Deleting blue deployment before switch causes downtime
current_env = "blue"
new_env = "green"
if current_env == "blue":
print(f"Switching traffic to {new_env} environment")
else:
print(f"Switching traffic to {current_env} environment")Solution
Step 1: Analyze the condition in the script
The variable current_env is "blue", so the if condition is true.Step 2: Determine the printed output
Since current_env is "blue", it prints "Switching traffic to green environment" using new_env.Final Answer:
Switching traffic to green environment -> Option AQuick Check:
current_env == "blue" triggers green switch = C [OK]
- Confusing current_env and new_env variables
- Assuming else branch runs when condition is true
- Ignoring f-string formatting
Solution
Step 1: Understand traffic routing in blue-green deployment
Traffic is controlled by the service selector labels pointing to the active environment.Step 2: Identify why traffic still hits blue
If users still hit blue, the service selector likely was not updated to green, so traffic stays on blue.Final Answer:
The service selector was not updated to point to the green environment -> Option AQuick Check:
Traffic routing depends on service selector = D [OK]
- Assuming green deployment deletion causes traffic to blue
- Confusing pod crashes with traffic routing
- Thinking model version affects routing directly
Solution
Step 1: Deploy and test new model in green environment
Deploying and testing in green ensures the new model works without affecting users.Step 2: Switch traffic to green, monitor, then clean up blue
Switching traffic only after testing reduces risk. Monitoring ensures stability before deleting blue.Final Answer:
Deploy new model to green environment, test it, switch service selector to green, monitor, then delete blue -> Option CQuick Check:
Test before switch, monitor after = A [OK]
- Deleting blue before green is ready
- Switching traffic before testing
- Deploying new model to blue environment instead of green
