Medium Distributed Systems
A/B Experiment System Consistent hashing Feature flags Traffic splitting
You're building a backend service that lets your team run controlled experiments on product features. The goal is a lightweight experiment evaluation service that:
1. Stores experiment configurations (name, variants, traffic allocation, targeting rules)
2. Deterministically assigns users to variants based on their user ID (consistent hashing)
3. Supports attribute-based targeting rules (e.g., only users matching specific region or device_type values)
4. Returns all active experiment assignments for a user in a single API call
Key Requirement: Deterministic Assignment
The same user_id must always get the same variant for a given experiment:
- No random assignment at request time
- Use hash: hash(experiment_id + user_id) % 100 → maps to a traffic bucket (0–99)
- Traffic allocation defines which bucket ranges map to which variant
Example: Buckets 0-49 → control (50%), Buckets 50-79 → variant_a (30%), Buckets 80-99 → variant_b (20%)
Constraints
● Assignment must be deterministic — same user always gets same variant
● Use FNV-1a or CRC32 hash (not cryptographic — needs to be fast)
● Targeting rules are AND-based: user must match ALL specified attribute fields
● Experiment with status: "paused" should not assign anyone
● Traffic percentages must sum to exactly 100
● Handle edge case: experiment with 0% traffic to a variant
Hints
▸ Hint 1 — General approach click to reveal
▸ Hint 2 — Key details
▸ Hint 3 — Pseudocode
Test Cases (8)
1. Create experiment with valid config
Returns 201, stores correctly
2. Evaluate user with matching targeting
Returns correct variant assignment
3. Evaluate user with non-matching targeting
Experiment appears in skipped
4. Same user evaluated twice
Returns identical variant both times
5. Paused experiment
Not included in assignments or skipped
6. Traffic percentages don't sum to 100
Returns 400 validation error
7. 1000 unique users evaluated
Distribution roughly matches allocation (±5%)
8. User matches some experiments
Mixed assignments and skipped
Test Results
Click "Run Tests" to execute your solution