Lesson 4 of 9 · 25m
Targeting Rules and Attribute Contracts
Build targeting from stable user, tenant, environment, and version attributes while protecting privacy, determinism, and authorization boundaries.
Targeting looks easy until a customer asks why their administrator saw a feature but their finance user did not, a mobile user lost it after upgrading, and a worker processed the same account differently. Targeting is a contract between the control plane and every application that supplies context. If attributes are improvised, rollout behavior is improvised too.
Define the context schema
Treat evaluation context like an API:
type FlagContext = {
userId: string;
tenantId: string;
plan: "free" | "team" | "enterprise";
role: "owner" | "admin" | "member";
appVersion: string;
platform: "ios" | "android" | "web";
};Stable bucketing beats random percentages
Percentage rollouts need a stable assignment. Use a deterministic hash of flag key plus stable subject ID:
bucket = int.from_bytes(
hashlib.sha256(f"{flag_key}:{tenant_id}".encode()).digest()[:8],
"big",
) % 10_000
enabled = bucket < percentage * 100Choose the bucketing subject carefully. User bucketing creates inconsistent experiences inside a shared tenant. Tenant bucketing creates consistent product behavior but can concentrate risk in one large customer. For B2B systems, tenant-level bucketing is usually the least surprising default.
Targeting rules are authorization-adjacent
If a rule uses role=admin, plan=enterprise, or beta entitlements, the server still has to authorize the operation. A non-targeted user can modify local storage, replay requests, or use an older client. Flags shape exposure; they are not a security boundary.
Privacy and minimization matter
Do not ship a data warehouse to the flag evaluator. Use the smallest stable attributes needed. Hashing an email address is not anonymization if the same hash appears everywhere; prefer internal IDs. Be especially careful with IP addresses, precise geography, device identifiers, health or financial attributes, and any property users do not expect to affect product behavior.
Write rules for operators, not just machines
A readable rollout definition prevents accidents:
key: reports.query-builder
rules:
- {name: internal staff, if: user.email_domain == "sudosecurity.org", value: true}
- {name: enterprise beta tenants, if: tenant.plan == "enterprise" and tenant.beta_enrolled == true, value: true}
- {name: default, value: false}Avoid clever nested logic that only one engineer understands. During an incident, the person editing the rule may be awake, stressed, and not the author.
Field notes
- Never target by mutable display names or email strings when an immutable internal ID exists.
- Percentage math should be centrally implemented; every team inventing its own hash produces incompatible rollout populations.
- Test missing attributes explicitly. A missing
tenantIdshould not accidentally match a rule written as inequality. - Keep a dry-run or explanation API. Support cannot debug what evaluation cannot explain.
Was this lesson helpful?