1. Home
  2. Blog
  3. DevOps

DevOps

Feature Flags Explained: Safe Releases, Gradual Rollouts and Flag Hygiene

Feature flags let you ship code switched off, turn it on for a few users, and switch it off again in seconds if something breaks. We built a small flag system and simulated a rollout to 10,000 users to show how it works under the hood.

11 min read
Feature flags explained: safe releases, gradual rollouts and flag hygiene

Key takeaways

  • Feature flags (feature toggles) turn code paths on or off at runtime without a new deployment.
  • They separate deploying code from releasing a feature, enabling gradual rollouts and instant rollback.
  • Use a stable hash of the user ID for rollouts. In our simulation, a 20% rollout enabled 19.9% of 10,000 users, and nobody lost access when we raised it to 50%.
  • Every flag is temporary debt: give it an owner and a removal date.

A new checkout flow is ready. Releasing it to everyone at once is risky: if it has a bug, every customer hits it until a fix is deployed. With feature flags, the code goes to production switched off, then turns on for the QA team, then for 5% of users, then 20%, watching error rates at each step. If something goes wrong, one switch returns everyone to the old checkout in seconds.

We wrote a small flag evaluator in Python 3.13 and simulated 10,000 users. The numbers below come from that run. Commercial and open-source flag tools add dashboards, targeting and auditing, but the core logic is similar.

What are feature flags?

A feature flag is a condition in your code whose value comes from configuration instead of being fixed at build time:

def checkout(user):
    return "new checkout" if is_enabled("new_checkout", user) else "old checkout"

Changing the flag’s configuration changes behaviour for chosen users, immediately, without building or deploying anything. Flags are also called feature toggles, switches or feature gates.

Why teams use them

  • Separate deploy from release: merge and deploy unfinished work safely, switched off.
  • Gradual rollout: limit the blast radius of bugs to a small percentage of users.
  • Instant rollback: turn a feature off without a new deployment.
  • Trunk-based development: avoid long-lived branches and painful merges, as discussed in merge vs rebase.
  • Experiments: show variants to different groups and measure results.

Types of flags

Type Purpose Lifetime
Release flag Hide unfinished or new features until ready Days to weeks
Experiment flag A/B tests between variants Weeks
Ops flag / kill switch Turn off expensive or risky functionality under load Long-lived
Permission flag Enable features for plans, beta users or internal staff Long-lived

The type decides how the flag should be managed. Release flags should disappear soon after launch; permission flags may live as long as the product does, and are closer to configuration.

How a flag check works

FLAGS = {
    "new_checkout": {"enabled": True, "rollout_percent": 20,
                     "allow_users": ["qa-team-1"], "deny_users": []},
}

def is_enabled(flag, user_id, flags=FLAGS):
    f = flags.get(flag)
    if not f or not f["enabled"]:
        return False
    if user_id in f["deny_users"]:
        return False
    if user_id in f["allow_users"]:
        return True
    return bucket(flag, user_id) < f["rollout_percent"]

The rules are checked in a deliberate order: the master switch first, then explicit deny and allow lists, then the percentage. In our run, the allow-listed QA user always saw the new checkout, while a regular user outside the 20% saw the old one.

Percentage rollouts

20% rollout over 10,000 users: 1986 enabled (19.9%)

Close to the target, as expected from a well-distributed hash. Typical rollout plans move through steps such as 1%, 5%, 20%, 50% and 100%, pausing at each to compare error rates, latency and business metrics between users with and without the feature. Our monitoring vs observability guide covers what to watch.

Stable, sticky buckets

def bucket(flag, user_id):
    h = hashlib.sha256(f"{flag}:{user_id}".encode()).hexdigest()
    return int(h[:8], 16) % 100

Instead of a random number on each request, which would show a user the new checkout on one page and the old one on the next, each user is assigned a stable bucket from 0 to 99 by hashing the flag name and user ID. We checked three properties:

same user always same answer:       True
raised to 50%: 4937 enabled; of the 1986 already in, still in: 1986
different flags pick different users: overlap 57 of 362

The same user always got the same answer. When we raised the rollout from 20% to 50%, every user who already had the feature kept it, because their bucket number did not change; the rollout only added people. And including the flag name in the hash meant two flags at 20% chose largely different users, so the same unlucky group is not the guinea pig for every new feature.

Tip: Bucket by a stable identifier, normally the user or account ID, not the session or device. Otherwise a user who switches from phone to laptop sees the feature appear and disappear, which looks like a bug and confuses your metrics.

Kill switches and safe defaults

flag off (kill switch):          False
unknown flag defaults to off:    False

Turning enabled off disabled the feature for everyone, including the allow list. That master switch is your emergency brake. Equally important is what happens when the flag service is down, slow or returns an unknown flag. Our evaluator defaults to off, which is usually right for new features. For long-lived ops flags that protect the system, decide deliberately which state is safe. Cache flag values locally so a flag service outage does not take your application down with it.

Testing with flags

test both paths: new checkout / old checkout

Every flag doubles the code paths that can run in production. While a flag exists, test both the on and off states; our quick check exercised both. Inject flag values in tests, rather than reading a live service, so tests are fast and deterministic. Avoid testing every combination of many flags, which grows exponentially; instead, test each flag’s states and the specific combinations that interact. Flags fit naturally with shift-left and shift-right testing: code is tested before merge, then validated with real traffic during a gradual rollout.

Flag debt

Flags are easy to add and easy to forget. Old flags clutter code with dead branches, make behaviour hard to reason about, and occasionally cause disasters when someone flips a flag nobody remembered. Keep it under control:

  • Create a removal ticket when you create a release flag.
  • Set an expiry date and review expired flags regularly.
  • Once a feature is at 100% and stable, remove the flag and the old code path in the next release.
  • Track the number of active flags per service and treat a rising count as a warning.

Naming and ownership

Name flags for what they do, such as new_checkout_flow or temp_search_v2_rollout, not flag_17 or test_toggle. A name should make sense to someone reading it six months later. Record an owner, a type and a description for each flag in a central registry, and keep separate values per environment so that turning something on in staging cannot affect production.

Build or buy?

A simple config file or environment variable works for a handful of on/off flags. Once you need percentage rollouts, targeting rules, audit logs, approvals and per-environment control, teams usually adopt a dedicated tool: commercial services such as LaunchDarkly, open-source options such as Unleash and Flagsmith, or the vendor-neutral OpenFeature standard to avoid lock-in. We did not test these products for this article.

Security notes

Flags evaluated in the browser or mobile app are visible to users, who can inspect or change them. Never rely on a client-side flag to protect data or paid features; enforce permissions on the server. Treat changes to production flags like deployments: restrict who can make them, require review for sensitive ones, and keep an audit trail.

A simple rollout runbook

  1. Before: merge the code behind a flag that is off; confirm the off path still passes all tests.
  2. Internal: enable for the team’s own accounts through the allow list and use the feature for real.
  3. Small slice: 1 to 5% of users. Compare error rates, latency and key business numbers with the rest.
  4. Grow: step up to 20%, 50% and 100%, pausing long enough at each step to see a normal day’s traffic.
  5. Stop rules: agree in advance what triggers a rollback, such as errors above a threshold, and who may flip the switch.
  6. Clean up: once stable at 100%, remove the flag and the old code in the next release.

Writing the stop rules down before starting matters most. In the middle of an incident, nobody should be debating whether a 2% error rate is acceptable.

Final thoughts

Feature flags make releases boring in the best way: deploy whenever you like, release gradually, and switch off instantly. Use stable hashing so rollouts are consistent, default safely when the flag system fails, test both sides of every flag, and remove release flags promptly. In our simulation, stable buckets kept every early user enabled as the rollout grew, which is exactly the behaviour users and dashboards depend on.

Frequently asked questions

What is a feature flag?

A runtime switch in code that turns a feature or code path on or off for chosen users without deploying new code.

What is a percentage rollout?

Enabling a feature for a share of users, such as 20%, usually by hashing each user ID into a stable bucket so each user consistently sees the same version.

What is a kill switch?

A flag that turns a feature off for everyone immediately, used to stop a failing or overloaded feature without a deployment.

Are feature flags technical debt?

Release flags become debt if left in place. Remove them, and the old code path, once the feature is fully rolled out and stable.

Can users see feature flags?

Flags evaluated in the browser or app can be inspected and changed by users. Enforce anything security-related on the server.

Keep learning

Work with Stack Quora

Need help with testing, WordPress or SEO?

We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.

Keep reading

More guides on testing, AI & modern development

Enjoyed this article? Pick a topic and jump straight into our newest tutorials.

Artificial Intelligence

Prompt Injection Explained: How AI Apps Get Tricked and How to Defend Them

Prompt injection explained for builders: direct and indirect attacks, why keyword filters fail (our test blocked 1 of 4), and…

Read article →
DevOps

DNS Records Explained: A, AAAA, CNAME, MX, TXT and TTL With Real Lookups

DNS records explained with lookups we ran: how resolution works, A and AAAA, CNAME rules, MX and null MX, TXT…

Read article →
DevOps

Feature Flags Explained: Safe Releases, Gradual Rollouts and Flag Hygiene

Feature flags explained with a rollout simulation we ran: flag types, percentage rollouts with stable hashing, kill switches, allow lists,…

Read article →
Test Automation

Test Data Generation With Python Faker: Realistic, Repeatable and Safe Data

Test data generation with Python Faker, run and measured: seeding for repeatable data, locales, unique values, domain-shaped records, edge cases…

Read article →
Software Testing

Shift-Left Testing Explained: Find Bugs Earlier Without Slowing the Team

Shift-left testing explained: what it means, why early defects are cheaper, practices from requirement reviews to CI checks, the tester's…

Read article →
Databases & SQL

SQL String Functions Explained: TRIM, SUBSTR, REPLACE, CONCAT and Data Cleanup

SQL string functions explained with queries we ran: concatenation and NULL, TRIM, UPPER and LOWER, SUBSTR and INSTR, REPLACE, padding,…

Read article →
Databases & SQL

SQL Triggers Explained: BEFORE and AFTER Triggers, Audit Logs and Pitfalls

SQL triggers explained with triggers we ran: BEFORE vs AFTER, OLD and NEW, audit logs, validation, stock updates, WHEN conditions,…

Read article →
Software Development

Python JSON Module Explained: dumps, loads, Custom Types and Common Errors

The Python JSON module explained with code we ran: dumps and loads, files, type conversions, dates and decimals, custom encoders,…

Read article →
Software Development

ES Modules vs CommonJS: How JavaScript Imports Work in Node.js, Tested

ES modules vs CommonJS explained with code we ran in Node.js: import and export syntax, require, live bindings, caching, top-level…

Read article →
Software Development

Python Regex Explained: Patterns, Groups, Lookaheads and Pitfalls, Tested

Python regex explained with patterns we ran: search vs match, findall, groups and named groups, greedy vs lazy, sub, lookaheads,…

Read article →
Artificial Intelligence

LLM Structured Output Explained: Getting Reliable JSON From AI Models

LLM structured output explained with validation we ran in Pydantic: JSON mode and schemas, constrained decoding, the ways model output…

Read article →
DevOps

SSH Keys Explained: How They Work, ssh-keygen, Ed25519 and Safe Habits

SSH keys explained with keys we generated: public vs private keys, ssh-keygen, Ed25519 vs RSA, fingerprints, passphrases, ssh-agent, authorized_keys, config…

Read article →
DevOps

HTTP Caching Explained: Cache-Control, ETag and 304 Responses, With a Live Test

HTTP caching explained with a small server we ran: Cache-Control directives, max-age, no-cache vs no-store, ETag revalidation and 304 responses,…

Read article →
Test Automation

API Contract Testing Explained: Catch Breaking Changes Before They Ship

API contract testing explained with a schema check we ran: what a contract is, consumer-driven contracts, Pact, OpenAPI, which changes…

Read article →
Software Testing

Risk-Based Testing Explained: Score Risks, Build a Matrix and Test What Matters

Risk-based testing explained step by step: identifying risks, scoring likelihood and impact, building a risk matrix, mapping risk to test…

Read article →
Databases & SQL

SQL Constraints Explained: NOT NULL, UNIQUE, CHECK and FOREIGN KEY, Tested

SQL constraints explained with tests we ran: NOT NULL, UNIQUE, CHECK, DEFAULT, PRIMARY and FOREIGN KEY, the NULL loopholes, SQLite's…

Read article →
Databases & SQL

SQL Date Functions Explained: Adding, Comparing and Grouping Dates Without Bugs

SQL date functions explained with queries we ran: adding intervals, month-end surprises, date differences, the BETWEEN trap, grouping by day,…

Read article →
Software Development

Python asyncio Explained: async, await, gather and TaskGroup With Real Timings

Python asyncio explained with code we timed: coroutines, await, gather, TaskGroup, error handling, timeouts, limiting concurrency and the blocking-call mistake…

Read article →
Software Development

Debounce vs Throttle in JavaScript: How They Work, When to Use Each, Tested

Debounce vs throttle in JavaScript explained with timings we measured in Node.js: how each works, simple implementations, leading and trailing…

Read article →
Software Development

Python Context Managers Explained: The with Statement, contextlib and Real Uses

Python context managers explained with code we ran: how with works, __enter__ and __exit__, contextlib.contextmanager, suppress, ExitStack, database transactions and…

Read article →
Artificial Intelligence

LLM Temperature and Top-p Explained: How AI Models Choose Their Next Word

LLM temperature and top-p explained with a simulation we ran: how probabilities are reshaped, what top-p keeps, recommended settings by…

Read article →
DevOps

YAML Explained: Syntax, the Norway Problem and Other Traps We Tested

YAML explained with a parser test: basic syntax, lists and maps, the Norway problem, versions turning into floats, octal zip…

Read article →
DevOps

Cron Jobs Explained: Syntax, Real Schedules and the Mistakes That Break Them

Cron jobs explained with schedules we verified: the five fields, operators, real examples, the day-of-month OR day-of-week trap, environment problems,…

Read article →
Software Testing

Bug Life Cycle in Software Testing: Every Status, Who Owns It and Common Mistakes

The bug life cycle explained: every defect status from New to Closed, who owns each step, rejected, deferred and reopened…

Read article →
Test Automation

Property-Based Testing in Python: Hypothesis Finds the Bugs Your Examples Miss

Property-based testing explained with Hypothesis tests we ran: properties vs examples, strategies, shrinking, the real bugs it found in our…

Read article →
Databases & SQL

SQL Pagination Explained: OFFSET vs Keyset, With a Million-Row Test

SQL pagination explained with a million-row test we ran: LIMIT and OFFSET, why deep pages get slow, keyset (cursor) pagination,…

Read article →
Databases & SQL

SQL EXISTS vs IN: Differences, the NOT IN NULL Trap and Performance

SQL EXISTS vs IN explained with queries we ran: how each works, why NOT IN returns no rows when a…

Read article →
Artificial Intelligence

LLM Tokens Explained: How AI Models Read Text, Count Costs and Fill Context Windows

LLM tokens explained with a tokenizer we ran: how text is split, why counts differ by language and tokenizer, context…

Read article →
Software Testing

The Test Pyramid Explained: Layers, Ratios and When to Break the Shape

The test pyramid explained: unit, integration and end-to-end layers, sensible ratios, the ice cream cone anti-pattern, the testing trophy and…

Read article →
Test Automation

pytest Fixtures Explained: Scope, yield Teardown, conftest.py and Factories

pytest fixtures explained with a suite we ran: how fixtures work, scopes, yield teardown, conftest.py, factory fixtures, parametrized fixtures, built-ins…

Read article →
Artificial Intelligence

AI Hallucinations Explained: Why Language Models Make Things Up and How to Reduce It

AI hallucinations explained: what they are, why language models produce them, common types, how to reduce them with grounding and…

Read article →
Test Automation

Mocking in Python: unittest.mock, patch and the Mistakes That Make Tests Lie

Mocking in Python with unittest.mock, tested: Mock and return_value, side_effect, patching where a name is used, spec and autospec, mocking…

Read article →
Software Testing

Code Coverage Explained: Line vs Branch Coverage and What 100% Really Means

Code coverage explained with a real coverage.py run: line, branch and condition coverage, reading a report, why 100% coverage can…

Read article →
Artificial Intelligence

What Are AI Agents? How They Work, With a Simple Agent Loop You Can Run

What are AI agents? A plain guide to the agent loop, tools, memory and guardrails, with a small Python loop…

Read article →
Test Automation

TDD vs BDD: Key Differences, One Feature Built Both Ways and When to Use Each

TDD vs BDD explained with one discount feature built both ways: red-green-refactor with code we ran, Given-When-Then scenarios, tools, trade-offs…

Read article →
Software Testing

User Acceptance Testing (UAT): Process, Checklist and Real Examples

User acceptance testing explained: who runs it, how it differs from QA, a seven-step process, a UAT test case template,…

Read article →