1. What is Canary Deployment?
In a traditional deployment:2. Why Use Canary Deployment?
Canary deployment reduces the risk of deploying a new ML model directly to production.Main benefits
- Detect model performance problems early.
- Detect API or infrastructure failures.
- Compare old and new model behavior.
- Reduce the impact of a faulty model.
- Enable gradual production rollout.
- Make rollback easier.
3. Canary Deployment Architecture
4. Canary Rollout Strategy
A common rollout process is:Step 1 — Deploy new model
Deploy Model v2 alongside Model v1.Step 2 — Send small traffic
For example:Step 3 — Monitor
Monitor metrics such as:- Accuracy
- Precision
- Recall
- F1-score
- Latency
- Error rate
- Throughput
- CPU/Memory usage
- Prediction distribution
Step 4 — Increase traffic
If Model v2 is healthy:Step 5 — Rollback if necessary
If Model v2 causes problems:5. Feature Flags
A feature flag is a mechanism that controls whether a particular feature or model version is enabled. For ML systems, a feature flag can determine:6. Feature Flag Based Model Switching
- Canary testing
- A/B testing
- Manual rollback
- Internal testing
- Model comparison
7. canary_demo.py
The following example uses FastAPI and switches between two models based on a request header.
8. Running the API
Install dependencies:9. Testing Model v1
Request:10. Testing Model v2
Request:11. Canary Header
The important part is:12. Header-Based Canary vs Percentage-Based Canary
The above example is header-based routing.Header-based
- Developers
- Testing teams
- Internal users
- Debugging
- Controlled experiments
Percentage-based
Production systems can instead route traffic automatically:13. Canary Monitoring
A canary deployment should not only route traffic—it should measure the canary. Example:
The new model can be promoted when its metrics satisfy predefined thresholds.
Example:
14. Automated Canary Decision
A more advanced pipeline can automatically make the rollout decision:15. Canary vs Blue-Green vs A/B Testing
Canary deployment is particularly useful for reducing production risk when releasing a new ML model.
16. MLOps Canary Pipeline
A complete MLOps workflow can look like:Key idea
Canary deployment = gradual model rollout + monitoring + controlled traffic + rollback capability. TheX-Model-Version header example is a simple way to understand the mechanism in a production MLOps system, the same concept is usually combined with model registries, monitoring, automated evaluation, feature-flag services, and deployment infrastructure.