Sub topic 9
Choosing the Randomization Unit and Keeping Assignment Sticky
Assume you can serve a model through an HTTP or RPC endpoint, understand basic model metrics, and can read short Python. This chapter focuses on deciding who receives each model version. It does not cover how to calculate every model metric.
Why the randomization unit matters
The randomization unit is the entity assigned to a model variant. Common units include:
- Request: each request is assigned independently.
- Session: all requests in one short visit use the same variant.
- User: one user consistently receives one variant.
- Account or organization: related users receive the same variant.
- Device: one device receives one variant.
Request-level assignment is simple, but it can produce a confusing user experience. A user might see different recommendations on two consecutive requests. If the outcome is a purchase, request-level assignment also makes it difficult to connect the model to the user's final behavior.
User-level assignment is usually a good default for a user-facing experiment. Account-level assignment is safer when users influence one another, such as coworkers sharing a queue or customers sharing an account budget.
| Randomization unit | Useful when | Main risk |
|---|---|---|
| Request | Testing serving behavior or latency | Users see mixed variants |
| Session | Short-lived interactions | Users switch variants between sessions |
| User | Measuring long-term user outcomes | Cross-device identity may be incomplete |
| Account | Users affect the same business outcome | Fewer independent samples |
Deterministic sticky assignment
Sticky assignment means that the same unit keeps the same variant during the experiment. A common method hashes a stable identifier with an experiment-specific salt. A hash converts text into a repeatable numeric value. The salt keeps assignments for separate experiments independent.
import hashlib
def bucket(unit_id: str, salt: str, buckets: int = 10_000) -> int:
value = f"{salt}:{unit_id}".encode("utf-8")
digest = hashlib.sha256(value).hexdigest()
return int(digest, 16) % buckets
def choose_variant(unit_id: str, salt: str, candidate_percent: float) -> str:
if not 0 <= candidate_percent <= 100:
raise ValueError("candidate_percent must be between 0 and 100")
threshold = int(candidate_percent / 100 * 10_000)
return "candidate" if bucket(unit_id, salt) < threshold else "stable"
for user_id in ["user-123", "user-456", "user-123"]:
print(user_id, choose_variant(user_id, "ranker-v2-exp1", 5))The same user and salt always produce the same result. The salt should remain unchanged for the experiment. Changing it reassigns nearly every unit and can make the experiment appear to have more exposure than it actually has.
The assignment service should record the experiment name, unit identifier, variant, assignment timestamp, and model version. Do not use an identifier that changes during the experiment. If a user is anonymous, use a durable first-party identifier where permitted, and define what happens when that user later signs in.
Changing rollout percentages safely
For a canary, increase the candidate threshold in stages, such as 1%, 5%, 25%, and 50%. With the same hash and salt, units already below the threshold remain candidates as the rollout grows. This makes exposure cumulative.
A decrease is different. Some candidate users may move back to stable, which can interrupt their experience. For a controlled experiment, avoid changing the percentage after analysis begins. For an emergency rollback, routing everyone to stable is more important than preserving assignment.
Exercise
An experiment measures a recommendation model's effect on purchases. Should it randomize each request, each session, or each user? Why?
Show answer
Randomize each user when possible. Purchases are user-level outcomes, so sticky user assignment prevents the same user's requests from mixing model variants. Use account-level assignment instead if users share a purchase decision or budget.