Skip to main content
Canary deployment is a release strategy where a new model version is gradually exposed to a small percentage of users/requests before being rolled out to everyone.

1. What is Canary Deployment?

In a traditional deployment:
The entire production traffic uses the existing model. With canary deployment:
The new model is initially exposed to a small portion of traffic. If the new model performs well:
If problems are detected:

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

The API decides which model should process the request.

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:
For example:
The API can inspect this header and select the model.

6. Feature Flag Based Model Switching

This is useful for:
  • 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:
Run the server:
API:
Swagger UI:

9. Testing Model v1

Request:
Response:

10. Testing Model v2

Request:
Response:
Here the same input produces different predictions because the request was routed to different model versions.

11. Canary Header

The important part is:
and:
This creates a simple feature-flag mechanism.

12. Header-Based Canary vs Percentage-Based Canary

The above example is header-based routing.

Header-based

Useful for:
  • Developers
  • Testing teams
  • Internal users
  • Debugging
  • Controlled experiments

Percentage-based

Production systems can instead route traffic automatically:
For example:
This sends approximately 5% of requests to the canary model. For production systems, routing is often handled by an API gateway, service mesh, load balancer, or deployment platform rather than implementing random routing directly inside the application.

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:
This creates an automated progressive delivery pipeline.

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. The X-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.