How to design feature flags for a web app (frontend)
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- The right version from the first paint
- Fetching and keeping the flags
- When the flag service is down
- Turning a feature on for a share of people
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
- Disney
- Atlassian
The problem
A feature flag system lets a team switch a feature on for selected accounts or a slice of the audience, and switch it off fast when it misbehaves. The frontend half turns on one tension: the page should know its flags before it draws, yet the flags live behind a network call that can be slow or absent. The design that resolves it has the page server evaluate this person's flags while it renders and embed them in the markup, so the client store starts full, with the last saved flags and coded fallbacks behind that for the day the service is down.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
Is the page rendered on the server, or is it a single-page app served as static files? | With a server render, the flags can be evaluated for this person and written into the markup before the browser sees it. A static page has no such moment, so it must start from saved flags or fallbacks and fetch after. | Assume a server render for signed-in pages, with a static build as a variant you can mention. Pages read only on the server are someone else's problem today. |
Who counts as a person: signed-in accounts, anonymous visitors, or both? | A share of people needs a stable id to hash. Signed-in accounts have one; visitors need a device or session id that survives a reload, and the two can disagree when a visitor signs in. | Both. Most traffic is signed in, but the checkout flag in the scenario is on for half of people, visitors included. |
How fast must a flag that was turned off reach a page that is already open? | It decides whether pages check back on a timer or hold a connection open for pushed changes, which is the main cost question in this design. | Within 30 seconds is fine for the base design. Treat a faster path as a follow-up: the interviewer may ask how you'd get to under a minute, or well under it. |
What should the page show if the flag service does not answer at all? | It fixes what the fallback at each flag read means: the old version, the feature off, or a last known value. Without it, an outage of a side service takes the page down. | The page must still work. The safe version is the old one, so the fallback for a new feature is off, and a copy kept on the device from the previous visit beats the code fallback. |
Do the targeting rules contain anything private, such as lists of account ids or attributes? | If they do, the rules must stay on the server and the browser should receive only this person's evaluated values. | Yes. Rules can name accounts and read attributes other teams would not want in a public page, so evaluate on the server. |
Does any flagged feature change the layout above the fold? | A late swap is a visual problem when it moves things. A checkout that changes shape on the first screen is the worst case for layout shift. | Yes, the new checkout changes the first screen, so a late swap would be seen and measured. |
Do we need to know which version each person actually saw? | It adds exposure events, which cost a request per page unless they are batched, and it decides whether the data team can trust the rollout later. | Yes, record which variation each person was shown. The statistics that read those events are out of scope. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
The first content shows within 2.5 seconds, flags or not | The flag lookup should not be the one thing holding up the critical path. Either the server evaluates flags while it renders, so no extra round trip is added before the first paint, or the page draws on saved flags and fallbacks after a short timeout. The figure is borrowed from web.dev, which treats a largest contentful paint of 2.5 seconds or less as good, measured at the 75th percentile of page loads; this design uses it as the budget for getting the page's main content on screen, flags or not, so it has to hold for most visits, not the best ones. |
Nothing on the page shifts by more than a score of 0.1 when flags arrive | The version the browser paints first has to be the version the flags say. A flag learned after the paint may change text or styling in place but should not move content; a flag that changes the layout must be known before the first draw, which is why the flags ride in the markup. |
An open page picks up a flag change within 30 seconds | A client store that checks the server on a timer, carrying the version it has, or a pushed update. A page that fetches once at load and keeps the values until the tab closes fails this on its own. |
The page still works, on safe defaults, when the flag service is down | Each read of a flag carries a coded fallback, the store begins from the device's saved copy on a return visit, and the fetch gets a timeout and quiet retries. The flag service becomes something the page uses when it can, not something the page depends on to render at all. |
A person keeps the same version as a rollout grows | The share decision comes from a hash of a stable id and the flag key, not from a random draw, and visitors who are not signed in get a lasting device or session id so their result repeats across reloads. |
Core entities
| Entity | What it holds |
|---|---|
Flag | One switch a team controls, with the rules that decide who gets which variation. |
Person context | What the rules read about the person loading the page. |
Evaluated flags for one person | The answer the page actually holds: a key to variation map for this context, with the version it came from. |
Exposure event | A record that one person was shown one variation of one flag. |
Saved flags on device | The last evaluated flags kept in local storage, so a repeat visit starts before the network answers. |
The API
| Endpoint | Notes |
|---|---|
| The server evaluates the rules for one context and returns only the result: a map from flag key to variation plus the version it was computed at. The page server calls it while rendering; a page with no server render calls it from the client store. Rules and the ids of other accounts stay behind it. |
| The change check an open page makes on its timer. It carries the version the store holds and gets back what changed and the new version, so a quiet half hour costs little. It is also the call that lets a page recover after the service comes back. |
| A batch of exposure events, accepted with HTTP 202 so the page does not wait for processing. Sent on a timer rather than one request per flag read. |
| The one way page code reads a flag. It is synchronous against the store, so it does not wait on the network; the fallback is required, so a missing or unknown flag has a defined answer. Reading it can record an exposure. |
| Lets a part of the page react when a flag changes while open. The handler decides whether to apply the change now or at the next safe moment; a turned-off flag should apply at once. |
The high-level design
Step 1 of 4
Fetch the flags after the page draws
The simplest thing that works: the page server returns markup with the new features off, the browser's script asks the flag service for this person's values, and the parts of the page redraw when the answer lands. It shows the pieces and the data flow, and it already fails the first requirement: half of people see the old checkout, then watch it become the new one, and if the layout moves far enough the shift score can go past 0.1.
What breaks first
The deep dives
The right version from the first paint. A new checkout is on for half of people. Walk through how one person's browser ends up painting their version first, with nothing swapping afterward, and say where the flag lookup sits against the first content.
Fetching and keeping the flags. Many parts of the page each need a flag, and someone leaves the tab open all afternoon while a team changes one. Walk through how the flags are fetched, where they live in the page, and how an open page learns about the change.
When the flag service is down. The flag service stops answering during a busy hour; requests for flags hang. Walk through what a person sees on a first visit and on a return visit, and how the page comes back when the service does.
Turning a feature on for a share of people. A team turns a feature on for a tenth of people and raises the share over a week. Walk through how the design decides whether one person is in, what that person sees across reloads and tabs, and what happens to a visitor who is not signed in.
A concept that helps here: Deterministic bucketing with hashes and salts
A concept that helps here: Staged rollouts and canariesConcepts this guide uses
- Rendering strategies: client, server, static
- Core Web Vitals
- Where state lives
- Client caching and normalized stores
- WebSockets, server-sent events and polling
- Dynamic configuration
- Failing open or failing closed
- Partial failure in the UI
- Deterministic bucketing with hashes and salts
- Staged rollouts and canaries