Overview
A feature switch (or feature flag, toggle, whatever your team calls them) is an if statement you can change without a redeploy.
if (switches.isEnabled("newCheckout", { userId: user.id })) { // the shiny new checkout} else { // the old one, still working}
That's really it. You merge your code with the new path hidden behind a switch, deploy it whenever you like, and then decide when (and for who) it's switched on. Deploying and releasing stop being the same thing, which takes a surprising amount of stress out of a release day.
Feature switches are also handy for:
- Testing in production. Switch something on for yourself, or your team, before anyone else sees it.
- Slow rollouts. Give a new feature to 5% of users, check nothing's on fire, then go to 25%, 50%, and so on.
- Beta programmes. Turn things on for the companies who've asked to try them early.
- Kill switches. Something going wrong? Switch it off. No hotfix, no waiting on a pipeline.
The key ideas
There are only a few moving parts, and it's worth knowing how they fit together before you dive in.
Switches
A switch has a name (for people), a key (for code, like newCheckout), and an optional description. The key is what your code asks for, and it can't be changed once the switch is created. So pick a good one!
Every switch is either on or off. There's no "value" beyond that, which keeps things simple.
Environments
Switches live in environments, such as stage and prod. Every switch exists in every environment, but each environment has its own value and its own rules. So you can have something switched on in stage while it's still off in prod.
The name and description are shared across all environments. The rules aren't.
Rules
Out of the box, a switch is just on or off for everyone. Rules let you give some requests a different answer, based on what your app sends with each check. For example:
- If
companyIdis one of42,117, serve On - If
emailends with@my-company.com, serve On - Roll out to 10% of
userIdvalues
Rules are checked top to bottom, and the first one that matches decides. If nothing matches, the environment's default is used. There's much more on this in Rules and rollouts.
Context
The context is what your app knows about the request being checked: who the user is, what company they belong to, what plan they're on, and so on. userId is the only thing that's required. Everything else is up to you.
{ userId: "u_123", companyId: "42", plan: "pro", seats: 25 }
API keys
Your code talks to Feature Switches with an API key. Each key belongs to a single environment, so your code never needs to say which environment it wants. The key decides.
There are two kinds of key:
| Server key | Client key | |
|---|---|---|
| Looks like | fs_srv_… |
fs_cli_… |
| For | Backend services | Browsers, mobile and desktop apps |
| Gets | Your rules, so it can work out answers itself | Only on/off answers for one user |
| Secret? | Yes. Keep it in your secrets manager | No. It's safe to ship to a browser |
Why the split? Your rules might list user ids, company ids or email domains. That's fine on a server, but you don't want it readable in someone's browser dev tools. Client keys only ever get the answers, never the rules.
Where next?
If you're ready to give it a go, Getting started takes you from nothing to a working switch in a few minutes.