Feature Flags Every Client Agrees On¶
A feature flag looks like a boolean. In practice it's a question: is this feature on for this user, in this environment, right now? The web app asks it, the mobile app asks it, and your server asks it again when the request arrives. If they don't all give the same answer, a user sees the new checkout page and then gets an error from the old checkout API.
This post shows the rule that makes every client agree, and how to run it with ObjectsGrid FeatureFlags.
What a flag holds¶
A flag belongs to an application, and says per environment whether it's on, for what share of users, and for which users always:
{
"id": "new-checkout",
"application": "web-shop",
"owner": "checkout team",
"expires": "2027-06-30",
"environments": {
"staging": {"enabled": true},
"production": {"enabled": true, "rolloutPercentage": 10, "allow": ["user-1", "user-2"]}
}
}
- The id is the flag's key, yours to choose.
environmentsuses your own names:development,staging,production.allowlists the user keys that always get the feature: your testers, a pilot customer.ownerandexpiresare for people. A flag nobody owns and nobody removes becomes permanent dead code.
The rule¶
Every client evaluates a flag the same way, for the environment it runs in and a user key, a stable id of the user such as their account id:
- The flag doesn't name the environment:
defaultEnabled, false when not given. - Not
enabled: off. - The user key is in
allow: on. - No
rolloutPercentage, or 100: on. - Otherwise, take the SHA-256 hash of
<flag key>:<user key>. Read its first 4 bytes as an unsigned big-endian number, take it modulo 100: that is the user's bucket. The flag is on when the bucket is belowrolloutPercentage.
async function bucket(flagKey, userKey) {
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(flagKey + ":" + userKey));
return new DataView(digest).getUint32(0) % 100;
}
async function isEnabled(flag, environment, userKey) {
const rule = flag.environments?.[environment];
if (!rule) return flag.defaultEnabled === true;
if (!rule.enabled) return false;
if (rule.allow?.includes(userKey)) return true;
const percentage = rule.rolloutPercentage ?? 100;
return percentage >= 100 || (await bucket(flag.id, userKey)) < percentage;
}
The same in Go, for your server:
func bucket(flagKey, userKey string) int {
sum := sha256.Sum256([]byte(flagKey + ":" + userKey))
return int(binary.BigEndian.Uint32(sum[:4]) % 100)
}
Check your implementation against these buckets before you trust it:
| Flag key | User key | Bucket |
|---|---|---|
new-checkout |
user-1 |
83 |
new-checkout |
user-2 |
61 |
new-checkout |
user-3 |
40 |
dark-mode |
alice |
88 |
dark-mode |
bob |
35 |
Why this rule works¶
- No coordination. A hash needs no shared state: the browser, the phone and the server compute the same bucket without asking anyone.
- Sticky. A user's bucket never changes for a flag, so they don't see the feature appear and disappear between page loads.
- Monotonic. Raising the percentage from 10 to 25 keeps everyone below 10 and adds the users from 10 to 24. Nobody who had the feature loses it.
- Independent flags. The flag key is part of the hash, so each flag picks a different 10%. Hashing the user key alone would give the same users every experiment first.
Pick the user key with care. An account id is stable; an email changes; a session id makes a user's answer change every visit.
Reading flags cheaply¶
Read all of an application's flags in one list, then poll:
curl -G https://apis.objectsgrid.com/featureflags \
-H 'X-ObjectsGrid-AccessPoint: EU' -H "Authorization: Bearer $TOKEN" \
--data-urlencode 'where={"application": {"equals": "web-shop"}}'
A single flag read with the ETag you hold in If-None-Match answers 304 Not Modified while it's unchanged, and
a 304 isn't billed; see runtime configuration with free polling.
Servers can subscribe to the FeatureFlagUpdated and FeatureFlagPatched webhook
events instead.
Or let the server evaluate for you, with the same rule, in one GraphQL request:
query {
featureFlagsFor(application: "web-shop", environment: "production", userKey: "user-1") { key enabled }
}
featureFlagsFor reads the flags from search, so a change shows within seconds rather than at once.
Flags in the browser¶
A storefront without sign-in still needs its flags. Tag the flags it may read with visibility = public, and a new
Organization Account's default policy lets its public API key read those and
nothing else of them. Server-only flags stay private. Never put anything secret in a flag: whatever a browser can
read, anyone can.
Keep production in few hands¶
A flag is an object like any other, so your access policy decides who may change
it, every change is a version you can read back, and meta.lastUpdatedBy says who flipped it. A flag holds all its environments in one object, so a rule
on its fields can't single out production: let only a release role change flags, or tag each flag with its team and
let each team change its own. See
Configuration and feature flags for the full guide.