cs.oldnet

All sheets / CI/CD & Release

Feature Flags & LaunchDarkly

Separating deploy from release with feature flags.

Why flags

  • Deploy ≠ release: ship code dark, turn it on later.
  • Trunk-based development: merge unfinished work behind a flag.
  • Progressive rollouts, canaries, beta cohorts, A/B experiments.
  • Kill switch: disable a bad feature in seconds without a deploy.

Flag types

TypeLifetimeExample
Releasedays–weeksnew-checkout
Experimentweekspricing-page-variant
Ops / kill switchlong-liveddisable-recommendations
Permission / entitlementlong-livedenterprise-sso

LaunchDarkly concepts

  • Project → Environments (dev, staging, prod), each with its own SDK key.
  • Flag: key, variations (boolean, string, number, JSON).
  • Targeting: individual contexts → rules (attribute matches, segments) → default rule (fallthrough).
  • Percentage rollout: deterministic bucketing by context key (sticky per user).
  • Contexts: user, organization, device… (multi-context targeting).
  • Prerequisites: flag B only on if flag A serves a variation.

Node SDK

import { init } from '@launchdarkly/node-server-sdk';
const ld = init(process.env.LD_SDK_KEY);
await ld.waitForInitialization({ timeout: 5 });

const context = { kind: 'user', key: user.id, email: user.email, plan: user.plan };
const on = await ld.variation('new-checkout', context, false); // false = fallback
if (on) newCheckout(); else oldCheckout();

process.on('SIGTERM', () => ld.close());

Server SDKs stream rules and evaluate locally (no network call per evaluation).

React SDK

import { withLDProvider, useFlags } from 'launchdarkly-react-client-sdk';
function Banner() {
  const { newCheckout } = useFlags();   // camelCased keys
  return newCheckout ? <New /> : <Old />;
}
export default withLDProvider({ clientSideID: ID, context })(App);

Progressive rollout playbook

  1. Ship dark (flag off in prod); test on internal users via targeting.
  2. 1% → 5% → 25% → 50% → 100% with guardrail metrics (errors, latency, conversion).
  3. Automate with release guardians / guarded rollouts that roll back on regression.
  4. Hold at 100% briefly, then remove the flag and dead code.

Best practices

  • Always pass a safe fallback value; app must work if LD is unreachable.
  • Evaluate once per request; pass the result down (consistent UX).
  • Name flags clearly; add owner + expiry; track and clean stale flags (flag debt).
  • Don't nest flags deeply; keep flag checks at the edges.
  • Audit log + approvals for prod flag changes.
  • Test both code paths in CI.

Interview Qs

  • Flags vs branches? Flags enable continuous integration; long branches cause merge pain.
  • Risks? Combinatorial complexity, stale flags, config-driven outages.
  • Sticky rollout? Hash(flagKey + salt + contextKey) → bucket 0–100000; same user, same result.