Prompt
How do I do staged deployments with feature flags?
Latest observation
Staged deployments with feature flags means you separate “deploying code” from “releasing behavior.” You ship the code to production in stages, but keep new behavior hidden behind flags until you’re ready.
The basic pattern
-
Add a feature flag around new behavior
- Default it to
off. - Example:
if feature_flag("new_checkout") { use_new_checkout() } else { use_old_checkout() }
- Default it to
-
Deploy the code everywhere
- The new code is in production, but nobody sees it yet.
- This reduces deployment risk because you’re not exposing users to the new path immediately.
-
Turn the flag on gradually
- Enable for:
- internal users
- a small percentage of traffic
- one region
- one customer segment
- Watch metrics and logs between each step.
- Enable for:
-
Ramp up in stages
- 1% → 5% → 25% → 50% → 100%
- If anything looks wrong, turn it off quickly without redeploying.
-
Clean up later
- Once the feature is fully rolled out and stable, remove the old code and the flag.
Common staged rollout strategies
1. Internal-first
- Turn on for employees or test accounts only.
- Good for catching obvious bugs before customer impact.
2. Canary rollout
- Enable the feature for a tiny subset of real users.
- Compare error rates, latency, conversion, etc. against control.
3. Segment-based rollout
- Roll out by:
- geography
- plan tier
- customer ID range
- app version
- Useful when some groups are lower risk than others.
4. Percentage-based rollout
- Use deterministic bucketing so the same user stays in the same group.
- Example: hash user ID and enable for users whose hash falls in the first 5%.
5. Kill switch pattern
- A flag that can disable a risky dependency or subsystem immediately.
- Especially useful for third-party integrations, payments, or expensive jobs.
A practical workflow
Step 1: Wrap the new feature
Keep the old path intact.
if flags.is_enabled("new_search"):
results = new_search(query)
else:
results = old_search(query)
Step 2: Ship behind the flag
Deploy to production with the flag off.
Step 3: Validate in production
Use:
- smoke tests
- observability dashboards
- logs
- error tracking
- synthetic tests
Step 4: Enable for small groups
Example rollout order:
- dev/staff
- beta customers
- 1%
- 10%
- 50%
- 100%
Step 5: Monitor
Watch:
- error rate
- latency
- conversion
- revenue
- customer support tickets
- infra cost
Step 6: Roll back instantly if needed
If metrics regress:
- disable the flag
- keep the deployment
- investigate later
Best practices
Make flags deterministic
So a user doesn’t keep switching between old/new behavior on refresh.
Keep flags short-lived
Flags should usually be removed after rollout. Otherwise they become technical debt.
Separate:
- release flags: temporary, used for staged rollout
- experiment flags: used for A/B testing
- ops flags: used for operational control / kill switches
Instrument both paths
Log and measure both old and new code paths so you can compare them.
Avoid deep nesting
Too many feature flags can make code hard to understand. Consider a flag service, middleware, or config layer.
Have ownership
Each flag should have:
- owner
- purpose
- expiration date
- rollback plan
Example rollout plan
For a new checkout flow:
- Deploy code behind
checkout_v2flag. - Enable for internal users.
- Enable for 1% of logged-in users.
- Check payment success rate and page load latency.
- Expand to 10%, then 50%.
- If stable, go to 100%.
- Remove old checkout code and delete the flag.
Watch out for
- Testing only the old path in staging
- Orphaned flags that never get removed
- Inconsistent state if some services use the flag and others don’t
- Misconfigured targeting that exposes the feature too broadly
- Flag dependency chains that are hard to reason about
Simple rule of thumb
- Deploy often
- Release slowly
- Monitor continuously
- Keep the ability to shut off fast
If you want, I can also show:
- a CI/CD pipeline example for staged deployments with flags,
- a percentage rollout implementation, or
- how to do this with tools like LaunchDarkly, OpenFeature, or Firebase Remote Config.