Sub topic 15
Shadow Deployments and Canary Releases
Assume you know how a model is trained and served through an API or batch job. This chapter focuses on safely testing a feature-pipeline change that might introduce training-serving skew.
A new model or preprocessing version can produce wrong predictions even when the service stays healthy. A rollout strategy limits the number of users affected while you compare the new path with the current path.
Shadow deployments
A shadow deployment sends a copy of each production request to a candidate model. The current model still produces the response. The candidate response is recorded for comparison but is not shown to the user.
Shadowing is useful when the candidate has a new feature transformation, model version, or serving runtime. Compare:
- Feature values received by both models.
- Prediction values and confidence scores.
- Latency and error rates.
- Resource use, such as memory and CPU.
The candidate must not change production data or trigger actions such as sending a payment or notification. Use a read-only path or a test side-effect boundary.
A shadow test cannot directly measure accuracy unless labels become available later. It can still expose feature mismatches immediately. For example, if the current path receives an age of 40 and the candidate receives 0, the transformation paths disagree.
Canary releases
A canary release sends a small, selected share of real requests to the candidate and returns its response. The remaining traffic uses the current version. Start with a small percentage, such as 1%, then increase it only when predefined checks pass.
import random
def current_model(features):
return 0.8 if features["age"] >= 18 else 0.2
def candidate_model(features):
return 0.9 if features["age"] >= 21 else 0.1
def route(features, canary_fraction=0.01):
if random.random() < canary_fraction:
version = "candidate"
prediction = candidate_model(features)
else:
version = "current"
prediction = current_model(features)
return {"version": version, "prediction": prediction}
for _ in range(5):
print(route({"age": 20}, canary_fraction=0.5))A canary tests the complete production path, including request parsing, feature retrieval, preprocessing, model inference, and response handling. It can reveal errors that shadow traffic misses, such as a candidate response having an incompatible format.
Define rollback rules before starting. Examples include an increase in prediction errors, a feature schema failure, a latency limit, or a large difference in predictions for the same requests.
| Strategy | User receives candidate response? | Main use |
|---|---|---|
| Shadow | No | Compare behavior without user impact |
| Canary | Yes, for a small traffic share | Validate the complete live path |
A safe rollout sequence
- Run offline parity tests and validation checks.
- Deploy the candidate in shadow mode.
- Compare logged features, predictions, latency, and errors.
- Start a small canary.
- Monitor technical metrics and, when labels arrive, model quality.
- Increase traffic gradually or roll back.
Exercise
Exercise
Why should a candidate model run in shadow mode before a canary release?
Show answer
Shadow mode compares its features and predictions without exposing users to incorrect responses or triggering candidate side effects.