Design a Feature Flag System
Understanding the Problem
🚩 What is a feature flag system? A feature flag is an
ifstatement whose answer is controlled from outside the code:if (flags.isOn("new-checkout", user)) { ... }. A feature flag system lets teams ship code turned off, then turn it on for internal users, a few percent of customers, one country or one platform, and turn it off instantly if something breaks, all without a deploy. LaunchDarkly, Statsig, Unleash and Flagsmith are well-known products.
It's a close cousin of configuration management (see /system-design/config-management). Both distribute small, rarely changing data to a large fleet and need to survive the central service being down. What makes flags different is that the answer depends on who is asking: the same flag is on for one user and off for the next, and that decision has to be made on every request.
Functional Requirements
Core Requirements
- Admins should be able to create flags and configure targeting from an admin UI: specific users, user segments, attribute rules (country, plan, app version, platform) and percentage rollouts, separately for each environment (dev, staging, prod).
- Services and apps (backend services, web and mobile clients) should be able to evaluate a flag for a user and get a variation back quickly.
- Flag changes should take effect everywhere within seconds, including kill switches.
- Every change should be recorded in an audit log: who changed what, when, and why.
Below the line (out of scope)
- Experiment analysis: measuring whether a variation improved a metric. That's the experimentation platform (see /system-design/experimentation-platform), which builds on the same assignment logic.
- Billing, multi-tenant account management and SSO for the admin UI.
Non-Functional Requirements
Ask your interviewer about scale. We'll design for a company-wide or SaaS system with 50,000 flags across all projects, 10,000 server instances and 50 million monthly active app users, doing 1 million flag evaluations per second in total.
Core Requirements
- Flag evaluation should be fast: under a millisecond on the server, so teams can call it freely in hot code paths.
- Evaluation must never fail or block. If the flag system is unreachable, code still gets an answer (the last known value, or a default).
- Flag changes should propagate to all SDKs within about 5 seconds.
- Evaluation should be deterministic: the same user gets the same variation of a flag on every server and every request, until the targeting changes.
- The admin side should be strongly consistent and fully audited.
Below the line (out of scope)
- Global strong consistency of evaluations. For a few seconds after a change, some servers may still return the old variation.
Do the math on the 1M evaluations per second, because it decides the architecture.
If every evaluation were a network call to a central service, that service would take 1M requests per second, and every call would add at least a millisecond or two of network round trip to the caller's request. A page that checks 20 flags would add 20-40 ms, or need complicated batching. And the flag service would be in the critical path of every request at the company: if it's slow, everything is slow.
Now look at the data. 50,000 flags with an average targeting config of maybe 2 KB is 100 MB in total, but any one service only uses its own project's flags, typically a few hundred, so under 1 MB. That's tiny. Each server can hold all the rules it needs in memory and evaluate locally in microseconds.
The core insight: ship the rules to the code, not the requests to the rules. One million evaluations per second sounds like a scaling problem, but with local evaluation, the central service only has to handle flag changes, which happen a few times a minute.