Drive a staged canary rollout from per-window error rates

MediumMLOpsPro

A new model version is shipped to a small slice of traffic first. If its error rate stays close to the current version, traffic is raised in stages; if it is clearly worse, it is rolled back automatically. Deciding on too few requests is how bad models get promoted.

Write canary_rollout(windows, stages=(5, 25, 50, 100), min_requests=200, ratio=1.5, margin=0.005).
Start at stage index 0 (stages[0] percent of traffic). Process each window, a dict with "base_req", "base_err", "can_req", "can_err":
- If can_req < min_requests: not enough evidence, stay at the current stage.
- Else base_rate = base_err / base_req (0.0 if base_req is 0), can_rate = can_err / can_req, threshold = base_rate * ratio + margin. If can_rate > threshold: roll back, traffic becomes 0, stop.
- Else advance one stage. If you were already at the last stage, the canary is promoted, stop (traffic stays at stages[-1]).
Append the traffic percentage after each processed window to history. Windows after a stop are ignored.
Return (status, history) with status "rolled_back", "promoted" or "in_progress".

Signaturedef canary_rollout(windows, stages=(5, 25, 50, 100), min_requests=200, ratio=1.5, margin=0.005)

Example

stages (5, 100), windows [ok with 500 requests, ok with 500 requests] gives ("promoted", [100, 100]).

Understand the idea

A canary release sends a new model version a small slice of traffic first. Each time window, compare its error rate with the current version: if it is clearly worse, roll back; if it is fine, raise traffic one stage; if the window has too few canary requests, wait, because a decision on little data is a coin toss. Worked example: stages (5, 25, 100), min_requests 200, ratio 1.5, margin 0.005. Window 1: canary had 100 requests, too few, stay at 5. Window 2: baseline 10 errors in 1000 = 1%, threshold = 0.01 * 1.5 + 0.005 = 0.02; canary 3 errors in 300 = 1%, fine, move to 25. Window 3: canary 12 in 400 = 3% > 2%, roll back to 0 and stop. History [5, 25, 0], status 'rolled_back'. Rule: skip windows with can_req < min_requests; base_rate is 0 if base_req is 0; roll back if can_rate > base_rate * ratio + margin; else advance, and advancing from the last stage means 'promoted'; record traffic after every processed window; ignore windows after a stop; otherwise 'in_progress'. Used in ML deployment pipelines.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: worked examplecanary_rollout([_w(1000, 10, 500, 5), _w(1000, 10, 500, 5)], stages=(5, 100))
  • Sample 2: healthy all the waycanary_rollout(_ok)