Ch.51: Feature Flag System Design: The Switch That Stayed On

Outline

Transcript

0:00 A feature flag lets engineers choose which application code runs without deploying a new version. Imagine a photo service using one to try a new image processor with a small group of accounts. The new processor starts breaking photos. Priya, the engineer on call, turns its flag off. The console confirms her change. One backend loses contact with the flag service but keeps serving uploads through the broken code. We'll design that flag platform: fast decisions inside each backend, stable rollout groups, and a way to limit how long an isolated server can keep starting the risky feature.

0:35 A successful checkbox isn't much comfort while another server is still making broken photos. And an older reply saying yes could arrive after Priya's off edit. I want to know why that can't give the new processor more time. Now trace Priya's off command through the complete architecture. It connects her console to an authoritative configuration store, then distributes versioned rules to the running services. Each service evaluates those rules locally, beside its request handler. So the rules tell us who qualifies.

1:03 The extra path asks whether this risky feature still has recent permission. Ordinary decisions avoid a network call; that permission refresh happens in the background. So Priya also needs evidence from the applications. Their reports travel back to her operator view. We'll return to this map once saving her change and actually applying it mean different things to us. First, set the requirements behind those separate paths. Functionally, engineers need authorized flag edits, account targeting, percentage rollouts, and an emergency off switch.

1:33 Backends need a quick Boolean decision for each request. Give that decision a small embedded client library, an SDK. Application code supplies the account context; the library evaluates a locally stored ruleset and explains which rule or default produced the answer. For our workload, let's say 1,000 backend processes, 100 flags, and 100,000 application requests each second. Five flag checks per request means 500,000 evaluations a second. That's our exercise scale, not a vendor statistic. Aiming below a millisecond locally makes a remote round trip on every check a poor starting point. Healthy connected services should normally receive an edit within 2 seconds.

2:14 But a server with no connection cannot receive Priya's off message on that schedule. Hmm, then the non-functional requirements need a separate failure policy. A cosmetic feature may retain its last known value. Our risky processor must eventually stop getting new work, even while disconnected. We choose a 5-second freshness window for that feature. After permission expires, the application uses its stable old processor. Existing image jobs may finish; a flag change doesn't reach backward into completed work.

2:43 LaunchDarkly and Unleash both document local backend decisions. Links are in the description. The extra expiry we're adding is our own policy. A fast answer and a recent answer need different paths, then. We'll design backend flagging, not a whole experimentation suite, a mobile rules platform, or an automatic deployment controller. Before Priya needs the off switch, her rollout needs stable membership. Suppose the new processor is enabled for 10% of accounts. A random draw on each upload could change what the same account gets.

3:14 And that account can hit different backends each time. How do they all keep it in the same group without asking a central server? Each SDK hashes the account, environment and flag key with a fixed salt, an extra value we keep unchanged. The same byte encoding and algorithm in every language produce the same numbered bucket. Then move the cutoff from 10% to 20, and the existing assignments stay put. We make more room in the same group instead of drawing everyone a new ticket. Yes, while the identity, salt and other targeting rules stay fixed.

3:47 Never include the changing configuration revision in the hash: an ordinary edit would reshuffle the accounts. For example, a small trial might not realize a perfect 10-to-90 split. Stable hashing doesn't parcel out actual customers with a measuring cup. So membership stays stable without promising an exact headcount. Before using that percentage, though, we need a rule order. Priya puts her account in both the inclusion and exclusion lists. Which one wins? Exclusion. Disabling the flag returns the off value before either list.

4:16 Otherwise, exclusions beat inclusions, then the first matching rule, then the percentage or default. Missing required identity gets an explicit default and reason. Next, make the console's write as dependable as the account assignment. Her console submits an off command expecting revision seven, the enabled ruleset it last read. Give that command its own identifier, D. The Admin API authenticates her, checks her permission for this environment and validates the replacement rules. Another engineer may have changed them since she loaded the page.

4:46 So the environment authority puts edits in one order. If seven remains the latest revision, one database transaction stores complete revision eight, moves the current pointer, records the audit and D's result, and adds a delivery job. That delivery job is an outbox entry, saved with the change. A worker sends it afterward and can retry. We don't commit the rules and merely hope a separate notification call succeeds. The primary must save revision eight durably, and so must one required standby, a second database copy in this region.

5:19 Only then do we tell Priya her edit succeeded. For example, Priya's command commits but its reply disappears, so she can't know whether it saved. What should a retry carrying D do? It returns D's recorded result, without creating another edit. Reusing D with different content is rejected. A conflict requires a reload and a fresh choice, not an automatic new intent. What about undoing a bad edit? Could a delayed copy of the rules interfere if we restored revision seven? Restore the old contents as revision nine, keeping the order forward.

5:52 That gives the console a durable saved result. Whether a running application installed it is the next part. Once an edit is durable, our Snapshot API returns a snapshot: one complete committed version of the environment's rules. The SDK checks its environment, revision, supported schema and checksum before installing anything. I wouldn't want half the new targeting rules beside half the old ones. A validation error should leave the previous complete snapshot in place and report the problem. The SDK builds the replacement away from live readers, then atomically swaps the active snapshot.

6:24 A request captures one immutable snapshot and evaluates its account against that version. No database lookup in the ordinary decision. And no out-of-process cache call for every account either; the rules themselves are already in this service's memory. On cold start, before a valid snapshot exists, return the documented application default with a reason. For our processor, false chooses the stable old path. Take a backend that reads the flag once at startup and stores that Boolean in a shared application object.

6:53 It keeps using that answer after the SDK receives later rules. Ah, we refreshed the rulebook and never consulted it. Each entry must ask the SDK again. Reusing the rules is fine; freezing the earlier Boolean defeats the update. The configuration store is authoritative, but it's no longer serving the bulk request traffic. Now we have to keep those many independent local copies useful. With local evaluation in place, the update stream must keep those copies useful. The outbox worker sends revision hints through regional distributors.

7:25 A hint tells the SDK that newer committed data is available. Could we just publish once to a live message channel and let every SDK listen? Imagine a process restarting at that moment: it misses the update. We keep delivery work durably, retry it, and make reconnection check the current committed revision rather than trusting that no event was missed. The reconnected SDK fetches a complete snapshot. It doesn't need every intermediate percentage change if the newest complete version already tells it what to do.

7:56 Yes. A duplicate hint is harmless. A delayed older snapshot can't replace a newer installed revision. Compare versions within the same environment; a newer test configuration mustn't replace this service's production rules. If snapshot bytes are mirrored through a content-delivery network, that helps with a reconnect surge. But a lagging mirror can't certify that its version is still the newest one. That distinction will matter for the off switch. Snapshot distribution helps applications learn; it cannot prove that a disconnected application has heard the latest decision.

8:28 The SDK can report installation with an identifier that changes whenever this process restarts. A successful write to its network socket only proves we handed bytes to a connection. And even an installation report grows old. The dashboard needs each observation's age, rather than turning one old acknowledgement into permanent green status. But those update paths still leave the isolated backend holding revision seven. Its valid stored rules continue admitting Priya's test group to the faulty image path. LaunchDarkly's docs describe continuing with stored values after disconnection.

9:01 I'd keep that for a badge. Why should a flag-service outage hide a feature that was working? That's the purpose of last-known-good state. It tells the server what it learned, but it can't tell the server whether Priya changed her mind after the connection broke. I was hoping a heartbeat would settle that. But a live connection to an intermediary might only prove the intermediary is alive. It could be cut off from the configuration authority. We can keep that rulebook indefinitely. This critical path also needs an expiring permission slip from the current authority, matching the installed revision.

9:35 Our pass lasts 5 seconds from asking. Consider this processor instead. The rulebook can say on, but an expired pass selects the old code. Losing contact costs her rollout, while photo uploads keep working. I think that cost is worth paying here. But our pass mustn't gain extra life just because its confirmation arrives late. Then show me the mechanism. How do we add recent permission without putting a remote request back into every flag check? The permission path runs in the background, so local evaluations stay fast.

10:07 Once a second, a process can ask whether its installed environment revision is still current. That one background probe covers the environment's critical flags. The authority must check current state in an order consistent with edits. An asynchronous replica or cached snapshot could still answer seven after the off change committed as eight. If the uniquely established authority can't perform that read, it issues no new permission. Its response binds the environment, revision, a fresh probe identifier and this process boot.

10:38 My first thought is to give the process 5 seconds when the reply arrives. That's the moment it finally has permission in hand. Let's say the process sends a probe at elapsed time zero, the authority confirms seven, and then Priya commits eight. The reply reaches the process at time four. Hold on. Starting a new 5-second timer there would let network delay extend the old answer. We'd be treating time spent in transit as if it never happened. Instead, record the probe's send time using the process's elapsed-time clock.

11:12 Its deadline is that time plus 5 seconds. A reply at four has 1 second left; a reply after five grants nothing. So the reply carries evidence for the original request, not a fresh 5 seconds starting at delivery. Restarting the process clears that permission, and a different snapshot needs a matching one. A test-group account sends a photo before expiry. Its matching rules and permission admit it to the new processor. When that account sends another photo after expiry, the old processor handles it instead.

11:42 Give me a second to trace the off change. Any confirmation of seven must have been ordered before eight committed. Its request began even earlier, so a send-based 5-second window can't stretch past 5 seconds after that commit. Exactly. A probe checked after the off commit can't renew seven at all. That bound assumes a compliant SDK and a correct clock. On our Linux hosts we choose the boot-time clock: it counts process pauses and machine sleep. This stops new entries, not running jobs or malicious code.

12:14 And replication doesn't choose a leader for us. During uncertain failover, edits and renewals stop. Resume only after the old authority is blocked from answering and its replacement has recovered the confirmed saves. So, under those assumptions, old permission dies within 5 seconds of Priya's off commit. A lost connection may also stop the rollout before anyone asks it to. That's the availability price. That separate freshness path changes our capacity estimate. 500,000 evaluations per second stay local, while a renewal each second from every process creates about 1,000 authority checks. The probes have one budget, and the update payload has another.

12:53 Why copy all hundred flags when Priya changed one Boolean? For instance, take our hundred flags at 500 serialized bytes per flag. A complete ruleset is roughly 50 kilobytes. Refreshing 1,000 processes moves roughly 50 megabytes before transport overhead. Oh, for our workload that's a small document. The full copy lets a reconnecting SDK recover its rules from the latest version; shipping only changes would need its own gap detection and recovery. The payload is not the whole story. 1,000 backends might reconnect together.

13:24 How do we keep that surge from overwhelming the snapshot service? Keep the newest delivery hints for each customer and environment. Spread retries with growing, capped waits and random variation, so every process doesn't try again on the exact same beat. Immutable snapshot mirrors can absorb reads, while the freshness authority keeps its own current-state path. Letting a mirror answer that question cheaply would quietly remove the contract we just designed. The arithmetic gives us a workload to test, not evidence that a particular database or machine can handle it.

13:57 Measure the authority, distributor and SDK separately. Beyond capacity, the feedback path needs to separate what is known. Priya should see which revision was saved, which processes reported installing it, and how old those reports are. For example, a service restarts after reporting successful installation. Its reports belong to process boots, so its old acknowledgement cannot make the new instance look synchronized before initialization. And a quiet process hasn't told Priya it's safe. Missing reports stay unknown.

14:28 We don't divide the newest acknowledgements by whichever small set happened to answer and call the whole fleet updated. For critical decisions, track expired-permission fallbacks and rejected stale replies as well. Those explain extra traffic to the old processor when flag updates or permission renewals fail. Priya could also deliver an edit perfectly to the wrong environment. Her access check must match the chosen environment, and the audit shows which production or test rules she changed. Hmm, and invalid configuration needs to stay visible.

15:00 Keeping the previous valid snapshot is useful, but silence would leave Priya thinking the new one had been adopted. Exactly. Retaining data, applying a change and having current permission answer different questions. One dashboard light shouldn't stand in for all three. I also wouldn't describe reverting a flag as undoing an incident. It changes the next decision. Images already processed by the broken path still need their own repair. With those limits stated, we can finally follow the original off change across the full architecture without pretending every server heard it at once.

15:35 Finally, put the full map back in view. Priya's authenticated edit commits revision eight, its audit and the outbox entry. That is the console's saved change, under our chosen durable acknowledgement policy. Regional distribution and the Snapshot API carry the new complete rules to connected SDKs. Validation and an atomic swap make eight active; delayed seven can't replace it. Each request still evaluates locally, and stable account bucketing keeps an ordinary rollout coherent. Increasing exposure doesn't require a fresh lottery for every photo.

16:06 The isolated server may still hold seven. Its critical permission cannot renew from a stale mirror, and a delayed old reply cannot restart the expiry clock. Once that send-based deadline expires, the next guarded decision selects the established processor. Work already underway may finish, and the operator view reports missing observations honestly. We've paid for a separate background authority check because Priya asked for more than continued access to yesterday's rules. We could choose retained state for another feature, but we'd name that different promise.

16:37 That's the interview answer I'd want to leave with: who owns the rules, where a request evaluates them, how an edit survives failure, and what a disconnected process is allowed to keep doing. Priya can now ask which rules that server installed and whether its permission has expired. A yes-or-no interface hides those questions; our interview answer shouldn't. And Priya's old processor keeps serving photos while the risky path loses its permission. Thanks for listening to Learning Podcasts.