Skip to main content

Lesson 6 of 9

Production Deployment Patterns: Shadow Deployments, Canary Releases & A/B Testing

Replacing a production machine learning model abruptly via a 'big bang' deployment is an unacceptably risky engineering practice. Modern MLOps employs progressive deployment architectures: (1) Shadow Mode (the challenger model receives live traffic and runs inference asynchronously, but its outputs are discarded; performance and latency are audited against the champion), (2) Canary Deployments (route 5% of traffic to the new model, expanding incrementally), and (3) A/B Testing (split users to measure true business KPI impact).

25 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Shadow Deployment & Traffic Splitting Logic

Shadow Deployment & Traffic Splitting Logic
import numpy as np
import pandas as pd

class ProgressiveDeploymentRouter:
    """
    Manages progressive model releases: Shadow Mode, Canary, and A/B Testing.
    """
    def __init__(self, champion_version: str, challenger_version: str):
        self.champion = champion_version
        self.challenger = challenger_version
        self.mode = "SHADOW" # Options: SHADOW, CANARY, AB_TEST
        self.canary_traffic_pct = 0.05 # 5% traffic to challenger

    def route_request(self, user_id: str, payload: dict) -> dict:
        # Deterministic hashing guarantees a user experiences consistent behavior
        user_hash = hash(user_id) % 100 # 0 to 99
        
        if self.mode == "SHADOW":
            # Real decision comes from Champion
            decision = f"Champion_{self.champion}_Executed"
            # Asynchronous background execution of Challenger for audit logging
            shadow_audit = f"Challenger_{self.challenger}_Logged_Asynchronously"
            return {"active_decision": decision, "shadow_log": shadow_audit}
            
        elif self.mode == "CANARY":
            if user_hash < (self.canary_traffic_pct * 100):
                return {"active_decision": f"Canary_Challenger_{self.challenger}_Executed"}
            else:
                return {"active_decision": f"Champion_{self.champion}_Executed"}
                
        elif self.mode == "AB_TEST":
            # 50:50 randomized split
            variant = "A_Champion" if user_hash < 50 else "B_Challenger"
            return {"active_decision": variant}

router = ProgressiveDeploymentRouter(champion_version="v1.4", challenger_version="v2.0")

print("=== PROGRESSIVE DEPLOYMENT ROUTING SIMULATION ===")
# 1. Shadow Mode execution
print("1. SHADOW MODE:")
print(router.route_request(user_id="user_8841", payload={"score": 85}))

# 2. Transition to 10% Canary Release
router.mode = "CANARY"
router.canary_traffic_pct = 0.10
print("\n2. 10% CANARY MODE:")
for u in ["user_01", "user_02", "user_03", "user_04"]:
    print(f"  {u:10s} -> {router.route_request(user_id=u, payload={})['active_decision']}")

Pro Tip: Use Deterministic User ID Hashing for A/B Splits

Never use random splitting (`np.random.rand() < 0.5`) in A/B testing. If a user refreshes the page or applies for a loan twice in 10 minutes, random splitting could show them Model A on the first request and Model B on the second request. Hash the unique user ID: `hash(user_id) % 100` to guarantee sticky consistency.

Diagram
Loading diagramโ€ฆ

MlABTesting Visual Representation

Hands-On Challenge: Build an Automated Canary Rollback Trigger

Write a monitoring watchdog that tracks the 5-minute rolling error rate of the Canary model. If HTTP 500 errors or prediction nulls exceed 1.0%, automatically reset `canary_traffic_pct = 0.0` to roll back traffic instantly.

Quick Quiz & Key Concept Checks

Common Pitfall: Ending an A/B Test Too Early (Peeking Problem)

Checking an A/B test p-value every morning and declaring victory the first time p drops below 0.05 inflates the false positive rate from 5% to over 30%. Fix your sample size in advance using power analysis and only evaluate significance once the target sample size is reached.

Topics in This Module

0/9 done