Feature flags, also called feature toggles or switches, are a runtime mechanism that lets you turn individual pieces of functionality on or off without changing the deployed binary. In practice, a flag is a boolean (or sometimes a multivariate value) stored in a configuration service, a database table, or a simple file. Your code checks the flag before executing a code path, so you can enable a feature for a subset of users, a specific environment, or a time window.

How Feature Flags Work Under the Hood

Storage and Retrieval

  • Centralised config service – e.g., LaunchDarkly, Cloud Config, or an internal key‑value store.
  • Database table – a feature_flags table with columns name, enabled, target_audience.
  • File‑based – JSON/YAML read at startup (less flexible for live toggling).

Evaluation in Code

if flags.IsEnabled("new‑checkout") {
    // new checkout flow
    runNewCheckout()
} else {
    // fallback to old flow
    runLegacyCheckout()
}

The IsEnabled call fetches the flag value, often cached locally for performance. Some platforms allow targeting rules (e.g., "enable for 10% of users"), which the SDK evaluates based on request context.

Why Teams Use Feature Flags

  • Canary releases – roll out to a small user segment, monitor metrics, then expand.
  • A/B testing – compare two implementations while keeping the codebase single.
  • Operational safety – instantly disable a broken feature without a hot‑fix.
  • Decouple deployment from release – developers can merge incomplete work behind a flag, keeping the main branch green.

Trade‑offs and Risks

BenefitCost
Faster, safer releasesAdded code complexity; every flag introduces a conditional branch
Ability to test in productionRisk of stale flags lingering in code, leading to technical debt
Granular rollout controlNeed for robust monitoring and flag‑management tooling
  • Complexity: Over time, many flags can create a tangled web of conditions, making debugging harder.
  • Performance: Frequent remote look‑ups can add latency; most SDKs mitigate this with local caches.
  • Security: Mis‑configured targeting can expose unfinished features to unintended users.
  • Cleanup: Flags should be removed after the feature is stable; otherwise they become hidden bugs.

A Concrete Example

Imagine an e‑commerce platform planning a new checkout experience. The team adds a flag new_checkout stored in a central config service. In the code, they wrap the new flow with an if check as shown earlier. Initially, the flag is enabled for internal QA users only. After smoke testing, they enable it for 5% of production traffic, monitor cart abandonment and error rates, and gradually increase the rollout to 100% before turning the flag off and deleting the old code.

Typical Interview Questions

  1. “Can you describe a time you used a feature flag?” – Share a brief story (e.g., the checkout rollout) highlighting the problem, the flag’s role, and the outcome.
  2. “What are the downsides of using feature flags?” – Mention technical debt, increased testing surface, and the need for flag hygiene.
  3. “How do you ensure a flag doesn’t become a permanent part of the code?” – Talk about a flag‑removal process: schedule, code reviews, and automated lint rules.
  4. “What monitoring would you put in place when toggling a flag?” – Discuss metrics (error rate, latency), alerts, and canary analysis dashboards.
  5. “How would you implement a flag that targets only 10% of users?” – Explain using a deterministic hash of user ID modulo 10, or leveraging a flag service’s built‑in rollout rules.

60‑Second Spoken Answer

"Feature flags are a runtime switch that lets you turn a piece of code on or off without redeploying. They’re typically stored in a central config service and accessed via a lightweight SDK that caches the value locally. The main benefit is safer, faster releases – you can canary a new feature to a small user group, monitor metrics, and roll it back instantly if something goes wrong. The trade‑offs are added complexity, potential performance overhead, and the risk of stale flags becoming technical debt. For example, at my last company we introduced a new_checkout flag, rolled it out to 5 % of users, watched the conversion rate, and only after it proved stable did we enable it for everyone and then removed the old code. Good practice is to have a clear flag‑cleanup policy and monitoring around any toggle.”

How to Practice This

  1. Write a one‑minute script using the structure above and record yourself. Listen for clarity and timing.
  2. Run a mock interview with a colleague or use Call Assistant to capture your answer and get instant feedback on pacing and relevance.
  3. Create a mini‑project: add a simple flag to a personal app, practice explaining the storage, evaluation, and cleanup steps.

FAQ

  • What is the difference between a feature flag and a configuration flag? A feature flag controls whether a code path runs, usually for product features. A configuration flag tweaks parameters (e.g., timeout values) without changing logic.
  • When should a team avoid using feature flags? If the change is trivial, low‑risk, and doesn’t need a staged rollout, adding a flag may add unnecessary complexity.
  • How do you test code that is behind a flag? Write unit tests for both enabled and disabled states, and use integration tests with the flag toggled to ensure end‑to‑end behavior.
  • What tools help manage flag lifecycle? Many teams use dedicated SaaS platforms that provide UI for toggling, targeting rules, and expiration dates, plus SDKs for common languages.

Frequently asked questions

What is the difference between a feature flag and a configuration flag?

A feature flag decides whether a block of code runs (often to enable or disable a new feature), while a configuration flag adjusts values like timeouts or limits without changing the execution path.

When is it not worth adding a feature flag?

If the change is low‑risk, short‑lived, or only affects internal tooling, the overhead of managing a flag may outweigh its benefits.

How do you prevent stale flags from accumulating?

Establish a flag‑removal policy: schedule a cleanup after the feature is stable, include flag removal in the same pull request that finalises the feature, and use lint rules to flag unused toggles.

What monitoring should accompany a flag rollout?

Track error rates, latency, and business metrics (e.g., conversion) for the flagged segment versus the control group, and set alerts for regressions before expanding the rollout.

#concept#feature flags#interview#software engineering#best practices