Percentage-based feature flag targeting lets you roll out a new feature to a specific percentage of users, typically by hashing a user identifier and comparing it to a threshold. This is the core mechanism behind gradual deployments, A/B testing, and canary releases, allowing you to monitor impact and catch regressions before full exposure.
What Are Feature Flags and Why Use Percentages?
Feature flags (also called feature toggles or feature switches) are configuration mechanisms that let you turn code paths on or off at runtime without deploying new code. Instead of shipping a feature to everyone at once, you can control its visibility dynamically. Percentage-based targeting is one of the most powerful flag strategies because it provides a controlled, incremental exposure path. It is widely used for canary releases (testing with a small subset of users), progressive delivery, and experiments where you need to compare behavior between groups.
How Percentage Targeting Works Under the Hood
At its core, percentage targeting relies on a deterministic function that maps a user identifier (like a user ID, session ID, or device ID) to a number between 0 and 1. The most common approach is to compute a hash of the identifier, then convert that hash to a decimal fraction. For example, you might use MD5 or SHA-256 to hash the string "user-123", take the first few bytes, and interpret them as a 32-bit integer. Dividing that integer by the maximum possible value gives a number in [0, 1).
If that number is less than the target percentage (e.g., 0.20 for 20%), the user sees the new feature; otherwise, they see the old behavior. The key property is determinism: the same identifier always produces the same hash, so a given user consistently gets the same experience across requests and sessions. This is crucial for avoiding flickering or inconsistent behavior.
Why Deterministic Hashing Matters
If you used a random number generator instead of a hash, a user might see the feature on one page load and not on the next, causing confusion and unreliable analytics. Deterministic hashing ensures stability. However, there is a subtlety: if you change the percentage from 20% to 30%, users who were previously in the 20% bucket remain in the new bucket because their hash value is still below 0.30. But if you change the hashing algorithm or the identifier used, the assignment changes completely. For example, switching from user ID to session ID will reassign users, which might be intentional for some experiments but not for others.
Choosing the Right Identifier and Hash Function
The choice of identifier and hash function affects both consistency and distribution. Common identifiers include user IDs, email addresses, or anonymous device IDs. For logged-out users, you might use a cookie-based ID. The hash function should produce a uniform distribution so that the percentage is accurate. Cryptographic hashes like SHA-256 work well but are slower; non-cryptographic hashes like MurmurHash or CityHash are faster and often sufficient. Some feature flag services use a technique called "consistent hashing" to minimize reassignments when the percentage changes, but simple hashing is fine for most use cases.
Practical Steps to Implement Percentage Targeting
- Choose an identifier that is stable for the user across sessions (e.g., user ID or a persisted device ID).
- Pick a hash function that returns an integer or a byte sequence. For example, in JavaScript you might use a simple string hash like cyrb53 or a library implementation of MurmurHash3.
- Normalize the hash to a float between 0 and 1. If your hash returns a 32-bit unsigned integer, divide by 2^32 (4,294,967,296).
- Compare the normalized value to your target percentage (e.g., 0.15 for 15%). If it is less, enable the feature.
- Cache the result for the duration of a request or session if needed, but always recompute from the same identifier to ensure consistency.
Common Pitfalls and How to Avoid Them
One common mistake is using a hash function that does not produce a uniform distribution, leading to skewed percentages. For example, taking the modulo of a hash by 100 can introduce bias if the hash range is not a multiple of 100. Another pitfall is using a volatile identifier like an IP address, which changes frequently and can cause inconsistent experiences. Also, be aware that if you have multiple flags, each flag should use a different salt (a random string appended to the identifier) to avoid correlation between flags. For instance, if you have two flags both targeting 10% of users, without a salt the same 10% of users get both features, which might not be what you want.
Percentage Targeting vs. Other Targeting Methods
Percentage targeting is just one type of targeting rule. Other common methods include targeting by user attributes (e.g., country, plan type, browser), by cohort (e.g., users who signed up in a certain month), or by random sampling (which is non-deterministic). Percentage targeting is often combined with attribute targeting: for example, you might roll out to 10% of users in the EU first. The advantage of percentage targeting is its simplicity and the ability to gradually ramp up without redeploying. The disadvantage is that you have less control over who exactly gets the feature, which can be a problem if you need to include or exclude specific users (e.g., for compliance or support reasons).
Costs and Performance Considerations
Hashing is computationally cheap, but if you are evaluating flags at high frequency (e.g., in a high-throughput API), the overhead can add up. Most feature flag SDKs are optimized and cache evaluation results. The cost is mainly in the infrastructure of managing flags and the complexity of testing multiple feature combinations. There is also an operational cost: you need to monitor the rollout and be prepared to roll back quickly if something goes wrong. However, the benefits usually outweigh the costs, as percentage targeting reduces the blast radius of a bad deployment.
Safety and Best Practices
Always use percentage targeting in conjunction with health checks and automated monitoring. For example, if you are rolling out a new checkout flow to 10% of users, you should monitor error rates and conversion rates for that group compared to the control group. Have a kill switch to immediately disable the flag if something breaks. Also, be aware that percentage targeting does not guarantee that a user will always see the same version if you change the identifier or the hash salt. For long-lived experiments, consider using a stable bucketing key that does not change over time. Finally, document your targeting logic so that future developers understand how assignments are made.
Frequently Asked Questions
Does percentage targeting guarantee the same user always sees the same variant?
Yes, as long as you use the same identifier and hash function. The hash is deterministic, so the same input always produces the same output. However, if you change the percentage, a user might move from the control to the treatment group if their hash value is now below the new threshold. If you need absolute stability even when changing percentages, you would need a more advanced technique like consistent hashing with sticky buckets.
What is the difference between percentage targeting and A/B testing?
Percentage targeting is a mechanism to split traffic; A/B testing is a methodology that uses such splits to compare outcomes. In A/B testing, you typically assign users randomly to groups and measure a metric. Percentage targeting can be used for A/B testing, but it is also used for gradual rollouts where you do not necessarily have a control group. In a canary release, you might not have a formal experiment, just a gradual exposure.
How do I ensure a uniform distribution when using a hash?
Use a well-known hash function that is designed to produce uniformly distributed outputs, such as MurmurHash3 or SHA-256. Then normalize the hash value to a float in [0, 1) by dividing by the maximum possible value. Avoid using modulo operations on the raw hash, as that can introduce bias unless the hash range is an exact multiple of the divisor.
Can I use percentage targeting for multiple features at once?
Yes, but you should use a unique salt for each feature flag. If you use the same identifier and no salt, all flags will be perfectly correlated: the same users will get all features or none. By adding a per-flag salt (e.g., "flagA" and "flagB") to the hash input, you decorrelate the assignments, which is usually what you want for independent rollouts.
What happens if I change the percentage from 20% to 80%?
Users who were in the 20% group remain in the new 80% group because their hash value is still below 0.80. Users who were not in the 20% group will be added if their hash value is between 0.20 and 0.80. This means the rollout expands monotonically, which is generally desirable. However, if you decrease the percentage, some users will be removed from the feature, which might cause a jarring experience if they had already seen the new feature.
Is percentage targeting GDPR compliant?
Percentage targeting itself does not necessarily involve personal data, but if you use identifiers like email addresses or user IDs, you may be processing personal data. The GDPR requires a lawful basis for processing, and you should ensure that your use of feature flags for analytics or experimentation is covered by your privacy policy and, where required, user consent. The mechanism itself is neutral; compliance depends on how you handle the identifiers and data.