Design a Feature Flag Service
🚩 What is a feature flag service? It is a central switchboard for features. Code asks "is the new checkout on for this user?" instead of hard-coding the answer, and the team changes the answer from one place without a deploy: on for employees, then for 5% of users, then for everyone, or off for everyone the moment something breaks.
This problem comes in a few shapes: "design feature flags", "design a rollout system with targeting rules", or "design a centralized configuration service with versions and watchers". They are the same design. A flag is a piece of central configuration whose value is a rule instead of a plain string, so we cover all three: turning features on and off (a kill switch), deciding who gets a feature (targeting rules and percentage rollouts), and managing the configuration itself (every change versioned, any flag can roll back, and watchers learn about changes in order).
Requirements
"Design a feature flag service. Teams create flags, decide who sees each feature, and roll changes out gradually. Other services need to know when a flag changes."
Clarifying Questions
You: What does a flag decide? Just on or off, or values like "blue" and "green"?
Interviewer: On or off for a given user. Variants can come later.
You: How do we decide who gets a flag?
Interviewer: Three ways. Targeting rules on user attributes, like "country is DE" or "plan is pro". A list of specific users, like the team testing it. And a percentage of all users for a gradual rollout.
You: For a percentage rollout, does a user who got the feature keep it?
Interviewer: Yes. A user must not see the feature flicker on and off between requests. And when we raise the rollout from 10% to 20%, the first 10% must stay in.
You: What happens when a feature breaks in production?
Interviewer: One switch turns it off for everyone, immediately, even for users on the allowlist.
You: Do we keep a history of changes?
Interviewer: Yes. Every change gets a version number, and we must be able to put a flag back the way it was at an earlier version. That is how we undo a bad change.
You: How do other services learn about changes? Do they ask us every time, or get told?
Interviewer: They register interest in some flags and pick up the changes. They must never miss one, and they must see them in order.
You: Many threads at once?
Interviewer: Assume one thread for the core design. Reads vastly outnumber writes, so be ready to talk about that.
The third answer shapes the rollout code: "sticky and growing" rules out the obvious random() call. The fifth and sixth answers turn a lookup table into a small versioned database.
💡 Interview tip: Ask for the order of checks explicitly: "if a user is on the allowlist but the kill switch is off, do they get it?" Interviewers often have an answer in mind, and the order is the core of the evaluation code.
Final Requirements
- Teams create flags by name. A new flag is disabled, with a rollout of 0% and no rules.
- Each flag has a kill switch (enabled or disabled), a rollout percentage from 0 to 100, and a set of targeting rules of the form "attribute equals value".
- A user gets a flag when the flag is enabled and either a rule matches the user's attributes, or the user falls inside the rollout percentage. A disabled flag is off for everyone.
- The user id is an attribute too, so a rule on
userworks as an allowlist. - Rollouts are sticky per user and per flag, and raising the percentage only adds users.
- Every successful change creates a new global version number and returns it.
- A flag can be rolled back to its settings as of any earlier version, which is itself a new change.
- Watchers register for flags and collect, in order, every change made to those flags after they started watching. Nothing is skipped or repeated.
Out of scope
- A web UI, user accounts and permissions for who may change a flag.
- Multi-valued flags and experiments (we come back to this in Extensibility).
- Deleting flags, and scheduled changes ("turn on at 9:00").
- Storage and networking: everything is in memory in one process.