Sub topic 2
Shadow deployment: testing on live traffic with no user impact
This chapter assumes you can serve a model behind an HTTP or RPC endpoint, read accuracy, precision, recall, and latency metrics, and understand proportions and averages. You should also be comfortable reading short Python snippets.
A shadow deployment sends a copy of each live request to a candidate model. Only the stable model answers the user. The candidate's output is logged and compared offline, so a bad candidate cannot affect users, though it still consumes compute.
Canary release: exposing a small share of users in stages
A canary release sends a small, fixed share of real traffic to the candidate. You increase that share in stages, such as 1%, 5%, 25%, and 100%, and check guardrail metrics before each step. Unlike shadowing, canary users see the candidate's output, so the exposure is real.
Choosing the randomization unit and keeping assignment sticky
The randomization unit is the entity you assign to a variant, usually a user ID. Assignment must be sticky: the same unit should get the same variant on every request. Hashing the unit ID with a salt gives that property without storing state.
import hashlib
def bucket(unit_id: str, salt: str, buckets: int = 10000) -> int:
digest = hashlib.sha256(f"{salt}:{unit_id}".encode()).hexdigest()
return int(digest, 16) % buckets
def choose_variant(unit_id: str, salt: str, candidate_percent: float) -> str:
threshold = int(candidate_percent * 100)
return "candidate" if bucket(unit_id, salt) < threshold else "stable"
print(choose_variant("user-123", "ranker-v2-exp1", 5))Use a different salt for each experiment so that the same users are not always the ones in the canary.
Deciding to promote or roll back
Define guardrail thresholds before the rollout starts. A typical rule compares the candidate with the stable model on error rate and p95 latency. Promote to the next stage only when every guardrail passes. Roll back to 0% candidate traffic when any guardrail fails.
> Set thresholds before the release, not after you see the numbers.
Exercise
A user ID is hashed with the salt "exp1". On the next request, you change the salt to "exp2". Why might the same user now get a different variant, and why is that acceptable?
Show answer
The bucket depends on the salt, so changing it reshuffles all assignments. This is acceptable for a new experiment because independent assignment prevents the new test from reusing the same users. It would be a problem mid-experiment, so keep the salt fixed for the life of one experiment.