awesome-everything RU
↑ Back to the climb

Engineering Practice

Feature flags: code and targeting reading

Crux Read real flag-evaluation code and a targeting rule, predict the behaviour or the bug, and pick the highest-leverage fix a senior engineer makes first.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

The flag check, the bucketing hash, and the targeting rule are where flag systems quietly go wrong. Read the snippet, predict what production does with it, then choose the fix a senior would make first.

Goal

Practise the loop you run on every flag review: read the evaluation path, spot the consistency or fail-open bug, and reach for the structural fix — sticky bucketing, a safe default, a removal ticket — before adding more flags.

Snippet 1 — the rollout check

function inRollout(flag, user) {
  // 25% rollout: roll a fresh random number per call
  return Math.random() * 100 < flag.rolloutPercent;
}

if (inRollout(newCheckout, user)) {
  return renderNewCheckout(user);
}
return renderOldCheckout(user);
Quiz

A 25% rollout uses this check. What goes wrong in production, and what is the fix?

Snippet 2 — the kill-switch path

async function chargeCard(order) {
  const enabled = await flags.evaluate("payments-enabled", order.user);
  if (!enabled) {
    throw new Error("payments disabled by kill-switch");
  }
  return gateway.charge(order);
}

// flags.evaluate throws if the flag service is unreachable
Quiz

This wires a kill-switch around payments. What is the production hazard, and what is the senior fix?

Snippet 3 — the targeting rule

flag: new-dashboard
rules:
  - if: user.plan == "enterprise"
    serve: on
  - if: user.betaOptIn == true
    serve: on
  - rollout:
      percent: 10
      stickyBy: user.id
default: off
Quiz

An enterprise user who has also opted into beta loads the dashboard. Which rule decides their variant, and what general property does this config rely on?

Snippet 4 — the stale flag in code

// added 2023-02 for the checkout-v2 release, ramped to 100% in 2023-03
if (flags.isEnabled("checkout-v2")) {
  return renderCheckoutV2(cart);
} else {
  return renderCheckoutV1(cart);   // V1 no longer maintained
}
Quiz

It is now a year later; checkout-v2 has served on to everyone since 2023-03 and V1 is unmaintained. What is the correct action and why?

Recap

Every flag review reads the same way: a rollout check must hash a stable key into a fixed bucket or it flickers; a kill-switch must evaluate locally with a safe default or the flag service becomes a hard dependency that fails your hot path; targeting rules are order-sensitive and the first match short-circuits, so rule order is precedence; and a release flag at 100% with an unmaintained other branch is flag debt to delete, not config to keep. Read the evaluation path, fix the structure, then re-review.

Continue the climb ↑Feature flags: ship dark, ramp, and retire
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources2
expand
  1. 01
  2. 02

Trademarks belong to their respective owners. Editorial reference only.