Sub topic 10
Control Traffic During the Change
Assume you understand basic model deployment, production monitoring, command-line tools, and APIs or services. This chapter focuses on traffic control: deciding which requests use the current model and which use the known-good model during a rollback.
Use traffic as a safety control
A rollback should change exposure gradually when the system allows it. A traffic controller routes requests to deployment units according to a rule. A deployment unit is the model, application code, dependencies, and configuration that run together.
The safest common strategies are:
| Strategy | How traffic moves | Main advantage |
|---|---|---|
| Blue-green | Switch nearly all traffic between two complete environments | Fast reversal |
| Canary | Send a small percentage to the restored version first | Limits initial impact |
| Weighted routing | Assign percentages to each version | Precise, gradual control |
| Feature or tenant routing | Route selected users or tenants | Useful for targeted validation |
Blue-green deployment works well when two environments are already warm and state-compatible. Canary or weighted routing is better when you need to observe the restored model before full exposure. If the service cannot route requests by model version, you may need to roll back the whole application deployment instead.
Plan the traffic sequence
Define each traffic step before changing production. A step should specify:
- The target model version and artifact digest.
- The percentage of traffic that moves.
- The minimum observation period.
- The error-rate, latency, prediction-quality, and business thresholds.
- The action if a threshold fails.
- The person or automation allowed to approve the next step.
For example, a rollback might move 10% of traffic to the known-good model, hold for 10 minutes, and then move to 100% only if all checks pass. Keep the failed version available until validation finishes. Removing it too early makes another reversal harder.
from dataclasses import dataclass
@dataclass
class TrafficPlan:
steps: list[tuple[int, int]]
current_percent: int = 0
def advance(self, error_rate: float, max_error_rate: float) -> int:
if error_rate > max_error_rate:
raise RuntimeError("hold traffic: error threshold exceeded")
if not self.steps:
return self.current_percent
percent, hold_minutes = self.steps.pop(0)
self.current_percent = percent
print(f"route {percent}% to known-good model; hold {hold_minutes} minutes")
return percent
plan = TrafficPlan(steps=[(10, 10), (100, 0)])
plan.advance(error_rate=0.01, max_error_rate=0.03)
plan.advance(error_rate=0.02, max_error_rate=0.03)Observe both sides
Compare the restored model with the still-running version when possible. Monitor request errors, p95 or p99 latency, timeouts, resource use, prediction distributions, and model-quality signals. Also check downstream effects such as rejected transactions, delayed jobs, or changes in conversion.
Avoid common traffic errors
Do not confuse a percentage with a fixed number of requests. A percentage can behave unpredictably when traffic is low or bursty. Use request counts and a minimum sample size before making a decision.
Do not send retries to a different model without recording the route. Otherwise, one user request may receive inconsistent predictions. Preserve session or tenant stickiness when the model uses state. Confirm that health checks test the model endpoint, not only the web server.
Exercise
Why might a canary rollback remain at 10% instead of moving immediately to 100%?
Show answer
The hold period provides enough observations to verify errors, latency, prediction quality, and downstream behavior before increasing exposure.