Rules and rollouts

Give some users, companies, or a percentage of people a different answer from everyone else.

A switch on its own is on or off for everyone. Rules are how you get more specific: switch it on for your own team, for a handful of beta companies, or for 10% of users while you keep an eye on the error rates.

Rules belong to one environment. So you can have a pile of rules in stage and none at all in prod, or the other way around.

How rules are checked

Open a switch and you'll see its rules under Rules, with the environment's default at the bottom:

  1. Rules are checked top to bottom.
  2. The first rule that matches decides the answer. Nothing below it is looked at.
  3. If no rule matches, the switch serves the default (the Otherwise serve line at the bottom).

Order matters, then. Put the most specific rules at the top, and use the arrows on each rule to move them about.

Nothing is live until you click Save (it'll say Save to prod, or whichever environment you're in). If you try to leave with unsaved changes, you'll be asked what you want to do with them.

Anatomy of a rule

Each rule reads like a sentence:

If companyId is one of 42, 117 and plan is one of pro → Serve On

  • A name (optional). Something like Beta companies. Handy when you have a few.
  • Conditions. Each one checks a single attribute from the context. A rule can have up to 20, and all of them have to match. A rule with no conditions matches everything, which is useful as a catch-all.
  • A result. Serve On, Serve Off, or Roll out to a percentage.

A switch can have up to 100 rules per environment. If you need more than that, we'd gently suggest something else might be going on.

Conditions

Conditions check the context your app sends with each check. Say your app sends:

context.ts
{ userId: "u_123", companyId: "42", plan: "pro", seats: 25, roles: ["admin", "billing"] }

Then you can write conditions with these operators:

Operator Matches when the attribute… Example
is one of exactly equals any of the values plan is one of pro, team
is not one of equals none of the values plan is not one of free
contains contains any of the values email contains +test
starts with starts with any of the values userId starts with u_
ends with ends with any of the values email ends with @my-company.com
is greater than is a number above the value seats is greater than 10
is at least is a number at or above the value seats is at least 10
is less than is a number below the value appVersion is less than 300
is at most is a number at or below the value seats is at most 5
is set is in the context at all betaOptIn is set

A few gotchas that are worth knowing up front:

  • A missing attribute never matches. Whatever the operator. That includes is not one of, so plan is not one of free won't catch users whose app never sent a plan at all. This is deliberate. It stops a missing attribute quietly switching things on for people.
  • Text comparisons are case-sensitive. Pro is not pro. If in doubt, lowercase things in your app before you send them.
  • Lists match if any item does. With roles: ["admin", "billing"], the condition roles is one of admin matches. For is not one of, it only matches if none of the items are in the list.
  • Numbers are compared as numbers. "25" and 25 both work with is greater than. Anything that isn't a number never matches.

Suggestions

You don't need to remember every attribute name your apps send. As your apps check switches, Feature Switches learns which attributes they send (and a few recent values), and the rule editor suggests them as you type.

You can see everything it's learned on the Attributes page (under ⋮ on the Feature Switches page). If an attribute is personal, like an email address, turn off Remember values for it. Anything already remembered is forgotten straight away, and new values won't be stored. The attribute name is still suggested, though.

Percentage rollouts

Choose Roll out to a percentage as a rule's result, and it serves On to that percentage of people and Off to everyone else.

Serve On to 10% of userId values, Off to the rest.

The important bit is that the answer is sticky. The same user always gets the same answer, on every server, every time. No flickering between the old and new versions on a refresh.

How it works

Each value of the attribute you roll out by (userId, unless you change it) is hashed, along with a salt that's unique to the switch, into one of 10,000 buckets. If the bucket falls inside the percentage, it's On.

That gives you a few useful properties:

  • Turning the percentage up only adds people. Going from 10% to 25% keeps everyone who already had it, and adds another 15%. Nobody loses a feature they've already got.
  • Different switches pick different people. Because each switch has its own salt, the 10% who get newCheckout aren't the same 10% who get newSearch. Your unluckiest users don't get every experiment at once.
  • It's done in your app, not on a server somewhere. The server library works out the bucket itself, so it's instant.

Rolling out by something other than users

Sometimes rolling out per user is wrong. If two people at the same company see different versions of your app, it's going to make for a confusing support call.

So change the attribute to roll out by. Set it to companyId, and whole companies flip together: everyone at a company gets the same answer.

If a request doesn't send the attribute you're rolling out by, it gets Off. The same goes for an attribute that's a list, as there's no single value to put in a bucket.

Examples

Just you, for now

Want to see a feature in production before anyone else does?

  1. If userId is one of your-user-id → Serve On
  2. Otherwise serve Off

Staff first, then a slow rollout

  1. Staff: If email ends with @my-company.com → Serve On
  2. Rollout: no conditions → Roll out to 10% of userId
  3. Otherwise serve Off

Staff always get it, thanks to rule 1. Everyone else has a 10% chance (well, it's not really chance, it's sticky). Once you're happy, nudge the percentage up. When you hit 100%, it's time to remove the switch.

Rule 3 never gets a look in here, as rule 2 has no conditions and matches everything. That's fine, but it's worth knowing why.

Beta companies, except the ones on the free plan

  1. Free plan: If plan is one of free → Serve Off
  2. Beta: If companyId is one of 42, 117, 205 → Serve On
  3. Otherwise serve Off

Because rule 1 comes first, a beta company on the free plan gets Off. Swap the rules around and they'd get On. Order matters!

Why is it that value?

If a switch isn't giving the answer you expect, the library can tell you which rule decided it:

debug.ts
switches.explain("newCheckout", { userId: "u_123", companyId: "42" });// { value: true, reason: "RULE_MATCH", ruleId: "…", version: 12 }

There's more on explain in the Node.js & browser docs.