Feature Flags Without the Baggage: A Swift-Native Rollout Layer
Photo: Parker Byrd parkerabyrd, CC0, via Wikimedia Commons
Feature flags have a reputation problem. The pitch is simple: ship code behind a switch, turn it on for 5% of users, sleep better. The reality is often a vendor SDK, a dashboard nobody owns, and a codebase littered with flags from 2023 that nobody remembers. That is not a rollout system. That is technical debt with a login screen.
You can build a small, dependency-free flag layer in Swift that handles gradual rollouts, kill switches, and remote overrides without dragging in a heavyweight SDK. The trick is to keep the core boring, make evaluation deterministic, and treat flag cleanup as part of the feature, not an afterthought.
The shape of the layer
I split the system into three pieces: a flag definition, a provider, and a context. The definition says what a flag means. The provider says where the value comes from. The context says who is asking. That separation makes testing easy and keeps the UI layer from knowing whether a flag came from a bundled default, a remote JSON file, or a local override.
enum FlagKey: String, CaseIterable {
case newCheckout
case compactFeed
}
struct FlagDefinition: Decodable {
let key: FlagKey
let enabled: Bool
let rollout: Int
let allowlist: [String]
}
protocol FlagProvider {
func definition(for key: FlagKey) -> FlagDefinition?
}
Bundled defaults live in code or a JSON file. Remote overrides live behind a provider that fetches a signed payload. In tests, a static provider returns exactly what you need. The rest of the app asks one question: is this flag on for this user?
Keep the flag model boring
Resist the urge to build a mini programming language. You do not need rules like user.country == US AND build > 410 AND cohort in beta. Those systems start as a config file and end as an outage. Start with four fields: enabled, rollout percentage, allowlist, and an optional kill switch. If you need targeting by country or plan, add a small context struct with those fields and keep the evaluation function pure.
struct FlagContext {
let userID: String
let country: String
let appVersion: String
}
func isEnabled(_ flag: FlagDefinition, for context: FlagContext) -> Bool {
guard flag.enabled else { return false }
if flag.allowlist.contains(context.userID) { return true }
let bucket = stableBucket(userID: context.userID, key: flag.key.rawValue)
return bucket < flag.rollout
}
The stable bucket is the important part. Do not use hashValue; Swift's hashing is randomized per process. Use a deterministic hash such as FNV-1a or SHA-256 over the user ID and flag key. That way the same user gets the same bucket across launches and devices, which makes rollouts measurable and support tickets sane.
Remote overrides without a heavyweight SDK
A flag SDK usually does three things: fetch config, cache it, and evaluate locally. You can do the same with URLSession and a small cache. Fetch a JSON file from your backend on launch and on foreground. Store it in Application Support with an ETag. If the network fails, use the last good payload. If that is missing, use bundled defaults. That is the whole reliability story.
func refreshFlags() async throws {
var request = URLRequest(url: endpoint)
request.setValue(etag, forHTTPHeaderField: "If-None-Match")
let (data, response) = try await URLSession.shared.data(for: request)
guard let http = response as? HTTPURLResponse else { return }
if http.statusCode == 304 { return }
try data.write(to: cacheURL, options: .atomic)
etag = http.value(forHTTPHeaderField: "ETag")
}
Notice what is missing: a background agent, a dashboard SDK, and a 20 MB binary. The kill switch is just a flag that defaults to off and is evaluated before everything else. If the backend is down, the last known good config still works. If the cache is corrupt, bundled defaults win. That is enough for most apps under a few million users.
Gradual rollout that is deterministic
A rollout percentage only works if it is stable. If user A gets the new checkout on Monday and the old one on Tuesday, your metrics are noise. Hash the user ID plus the flag key, map it to 0...99, and compare to the rollout percentage. Keep the same bucket across app launches. When you widen the rollout from 5% to 25%, users in the first 5% stay in. That is the behavior your support team expects.
Testing strategy
Test the evaluator, not the network. Unit tests should cover disabled flags, allowlisted users, bucket boundaries, and missing definitions. Inject a fixed context and assert exact outcomes. For UI tests, add a launch argument that loads a local JSON override so you can force a flag on or off without a backend. Contract-test the remote payload shape in CI so a backend rename does not silently turn every flag off.
Operational guardrails
Flags rot when nobody owns them. Give every flag an owner, a creation date, and an expiry date in the definition. Add a CI check that fails if a flag is older than 90 days and still referenced in code. Keep a simple dashboard or even a spreadsheet that lists active flags and their rollout state. Name flags for the behavior, not the team: newCheckout, not paymentsTeamExperiment2. Default new flags to off in production. Make the kill switch path faster than the launch path.
Ship, measure, remove
The last step is the one teams skip: removal. A flag that has been at 100% for three months is not a feature flag anymore. It is a conditional. Delete the old path, remove the flag, and celebrate. The goal is not a permanent control plane. The goal is to ship with less fear and then clean up after yourself. A small Swift-native layer gives you that without the vendor baggage.