Sub topic 3
Splitting Traffic Between Model Versions
Assume you know how to serve a model behind an HTTP or RPC endpoint. You should also be comfortable with latency, error rate, and basic probability. This chapter focuses on routing requests to a stable model and a candidate model.
The routing model
A model release usually has two versions:
- Stable is the version currently serving normal production traffic.
- Candidate is the new version being evaluated.
A traffic router decides which version handles each request. A simple canary might send 95% of requests to stable and 5% to candidate. The router must preserve the model API, so both versions should accept the same request schema and return compatible responses.
A percentage split answers a system question: can the candidate handle real production load? It does not, by itself, prove that the candidate produces better business results.
| Routing method | Assignment decision | Typical use |
|---|---|---|
| Random per request | Each request is assigned independently | Load testing |
| Sticky user assignment | A user or account keeps one version | Experiments and canaries |
| Header or rule based | Request attributes choose the version | Internal testing and debugging |
Weighted routing with a service mesh
A service mesh can split traffic before the request reaches a model server. The following Istio configuration routes 95% of traffic to the stable subset and 5% to the candidate subset.
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: fraud-model
spec:
hosts:
- fraud-model
http:
- route:
- destination:
host: fraud-model
subset: stable
weight: 95
- destination:
host: fraud-model
subset: candidate
weight: 5The stable and candidate subsets must be defined in a matching Istio DestinationRule. The weights are approximate over many requests. A 5% setting does not guarantee that every group of 20 requests contains exactly one candidate request.
Sticky assignment
For an experiment, assigning each request independently can expose one user to both versions. That makes outcomes harder to interpret. A user might receive a stable recommendation on one visit and a candidate recommendation on the next.
Instead, choose a randomization unit, the entity assigned to a variant. Common units include a user, account, device, or session. Hash the unit identifier with an experiment-specific salt. The hash produces a repeatable bucket.
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))The salt separates experiments. Without it, users may receive correlated assignments across experiments. Do not hash sensitive identifiers directly if your logs or configuration could expose them. Use a stable internal identifier or a protected identifier.
A sticky assignment should be recorded or reproducible. If the routing configuration changes from 5% to 20%, keep existing assignments when possible and add new users to the candidate group. Sudden reassignment can contaminate the comparison.
Exercise
Why is a deterministic hash useful for a model experiment?
Show answer
It gives the same randomization unit the same variant while distributing many units approximately according to the target percentage.