Neural Mastery
← Practice
Canary Rollout Traffic Assignmentmedium

Canary Rollout Traffic Assignment

medium ML Systems & MLOps 15 min

NumPy allowed · plain Python earns 10 bonus points

Implement deterministic canary-rollout traffic routing -- the real mechanism behind gradually shifting production traffic to a new model version.

Task

Implement canary_route(request_id, canary_pct). Hash request_id with hashlib.md5, take the first 4 bytes as a big-endian unsigned int, and compute bucket = (that_int % 10000) / 100.0 (a value in [0, 100)). Return 'canary' if bucket < canary_pct, else 'stable'.

Function Signature

canary_route(request_id: str, canary_pct: float) -> str

Examples

Example 1: 0% canary always stable
Input: request_id = 'user-1' canary_pct = 0
Output: 'stable'
Example 2: 100% canary always canary
Input: request_id = 'user-1' canary_pct = 100
Output: 'canary'
Example 3: deterministic for fixed id
Input: request_id = 'user-42' canary_pct = 50
Output: 'canary'

Constraints

  • 0 <= canary_pct <= 100
Python3Saved ✓
def canary_route(request_id, canary_pct):
# Your implementation here
pass

Case 1: 0% canary always stable
Input: request_id = 'user-1' canary_pct = 0
Expected: 'stable'
Case 2: 100% canary always canary
Input: request_id = 'user-1' canary_pct = 100
Expected: 'canary'
Case 3: deterministic for fixed id
Input: request_id = 'user-42' canary_pct = 50
Expected: 'canary'