Feature Flag and Experimentation SDK

Flags are how mobile teams change behaviour without a store release, and bad assignment logic silently ruins experiments.

Part 1 of 5

Basics

The whole idea first, then the terms the rest of this page uses.

1.1Whole idea

Flags decouple shipping code from turning it on. The app holds cached rules, computes each user's variant locally with a stable hash, and logs an exposure when the user reaches the feature. It keeps working offline and never blocks startup on a network call.

Why it matters

Flags are how mobile teams change behaviour without a store release, and bad assignment logic silently ruins experiments.

1.2Key concepts

One idea per card, in plain words.

01Deterministic bucketing

Hashes user ID with a salt, so the same user always gets the same variant.

02Exposure event

Records that the user actually saw the variant. Analysis counts only exposed users.

03Kill switch

Turns a feature off remotely, without an app update.

04Sample ratio mismatch

A 50/50 split that arrives as 47/53 means a logging or assignment bug, so results cannot be trusted.

Part 2 of 5

Question

What exactly you must answer, and what to settle before you start.

2.1Question

Interviewer

Design the feature flag and experiment SDK for an app with 150 engineers. It must not slow startup, must work offline, and must produce results analysts can trust.

2.2Clarify first

Each answer removes a guess.

You askAnswer
How fast must a kill switch act?Within minutes for active users, and on next launch for everyone else.
Does the variant change mid-session?No, except for kill switches. Stable UI beats instant updates.
Part 3 of 5

Detailed answer

How it works, and how to build or explain it.

3.1Step by step

The mechanism, in the order you would explain it.

  1. Read from cache first

    Startup uses rules stored on disk, with no network wait. New rules apply on next launch.

  2. Fetch in the background

    Download rules with an ETag, so unchanged rules cost almost nothing.

  3. Assign locally

    Hash user and salt into a bucket. A new salt gives each experiment an independent split.

  4. Log exposure at the point of use

    Log when the code path runs, not at launch, or unexposed users dilute the result.

  5. Check your own data

    Alert on sample ratio mismatch before anyone reads results.

  6. Clean up flags

    Every flag has an owner and an expiry. Old flags multiply the paths you must test.

3.2Code

Working code for the core of it.

Stable variant assignment

The same user must get the same variant on any device, offline, with no server call.

FlagEvaluator.ktKotlinCopy
1data class Variant(val name: String, val weight: Int)2 3data class Experiment(4  val key: String,5  val salt: String,6  val variants: List<Variant>,7  val rolloutPercent: Int = 100,8  val killed: Boolean = false,9)10 11/** Pure assignment: the same user and experiment always get the same variant, on any device. */12class FlagEvaluator(private val onExposure: (experiment: String, variant: String) -> Unit) {13  private val logged = HashSet<String>()14 15  fun variantFor(userId: String, exp: Experiment, default: String = "control"): String {16    if (exp.killed) return default                         // kill switch beats everything17    val bucket = bucketOf("${exp.salt}:$userId")18    if (bucket >= exp.rolloutPercent * 100) return default19    val total = exp.variants.sumOf { it.weight }20    var point = bucketOf("${exp.salt}:variant:$userId") % total21    val chosen = exp.variants.first { v -> (point < v.weight).also { if (!it) point -= v.weight } }.name22    // Log once per session, and only when the user actually reaches the feature.23    if (logged.add(exp.key)) onExposure(exp.key, chosen)24    return chosen25  }26}27 28/** FNV-1a into 0..9999: stable across platforms, unlike hashCode() between languages. */29fun bucketOf(s: String): Int {30  var h = 0x811C9DC5.toInt()31  for (b in s.encodeToByteArray()) {32    h = h xor (b.toInt() and 0xFF)33    h *= 1677761934  }35  return ((h.toLong() and 0xFFFFFFFFL) % 10_000).toInt()36}
36 linesUTF-8Kotlin
if (exp.killed) return default
The kill switch wins over everything.
bucketOf("${exp.salt}:$userId")
A new salt gives each experiment an independent split.
logged.add(exp.key)
Logs exposure once, when the feature is reached, so unexposed users don't dilute results.
FNV-1a
hashCode differs between languages. A hash written out by hand gives Android and iOS the same answer.

Compiled and tested by npm run test:mobile.

Part 4 of 5

Q&A

What interviewers ask next. Answer out loud, then open.

4.1Interview questions

Why hash on the client?
It works offline and adds no latency. The same function on the server keeps assignments consistent.
Why avoid hashCode()?
It differs between languages. Android and iOS would put one user in two variants.
What about logged-out users?
Use a stable install ID, and decide how assignment carries over at sign-in, or one person appears in both groups.
Part 5 of 5

Extra depth

Edge cases, counterpoints and what to read next.

5.1Edge cases and counterpoints

Includes the strongest argument against this approach.

Show detailsHide details
Old app versions
A flag can target a version that lacks the code. Rules need a minimum app version.
Interacting experiments
Salts keep splits independent, but two features can still clash. Use mutual-exclusion layers for risky overlaps.
Counterpoint
Small teams can start with remote config and no statistics. Add experimentation when decisions depend on it.