1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-23 00:22:19 +01:00

Consolide 'What is a feature flag' docs (#8050)

This commit is contained in:
Melinda Fekete 2024-09-09 10:35:56 +02:00 committed by GitHub
parent b836d117e3
commit 7051fabecd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 150 additions and 67 deletions

View File

@ -1,135 +1,218 @@
---
title: What is a feature flag?
title: What is a feature flag and why are feature flags used?
slug: /what-is-a-feature-flag
description: Feature flags, also known as feature toggles, allow developers to enable or disable features or code paths dynamically.
description: Feature flags let you control software features in real time, enabling safer deployments, better testing, and faster innovation.
---
Feature flags, also known as feature toggles, allow developers to enable or disable features or code paths dynamically. This approach helps manage the deployment of new features, perform A/B testing, roll out features gradually, and provide quick rollback capabilities.
import Figure from '@site/src/components/Figure/Figure.tsx'
### The Concept
Feature flags allow you to release, test, and manage features and functionality across your application without changing the source code. Organizations use added control and flexibility to deliver more and higher quality features with reduced cost, time, and risk.
Feature flags work by wrapping a piece of code with a conditional statement that checks the status of a variable, called a flag. If the flag is enabled, the new code executes; if not, the existing or alternative code path runs. That flag can then be toggled (hence, feature toggles) dynamically at runtime. This enables features to be merged into the main codebase but remain dormant until they are ready to be activated.
In this guide, well introduce you to the basics of feature flags and why so many development teams choose them.
<Figure img="/img/feature-flag-example.png" caption="At its most basic level, a feature flag is a conditional statement that checks the status of a variable. The value of the variable determines if the code path is enabled or disabled."/>
## Why developers use feature flags
*The easiest way to explain why developers use feature flags is to understand the story of why [Ivar Østhus](https://github.com/ivarconr),
our CTO, created the Unleash feature flag service in the first place. Here's Ivar in his own words:*
> In 2014, I was working as a software developer at FINN.no, Norways largest online marketplace. Getting new features into production was a pain, so, as a developer, I looked around and wondered if I could write some code to make it easier. My initial goal was to protect features under development, allowing our team to transition to [trunk-based development](https://www.getunleash.io/blog/how-to-implement-trunk-based-development-a-practical-guide)—a practice recommended according to the [State of DevOps Report](https://www.getunleash.io/blog/guide-to-feature-flags#:~:text=State%20of%20DevOps%20Report).
>
> Even back at that time, we embraced agility with an automated delivery pipeline which made fixing bugs a breeze, but frequent feature releases? Not so much. Building features takes time, and waiting weeks for production testing felt wasteful.
> We used feature branches, a common approach, to isolate unfinished features. However, this created a tangled web of problems:
> - **Limited feedback**: Getting stakeholder input required manually deploying test environments for each branch, a cumbersome process.
> - **Merge mayhem**: Conflicts became a constant headache, delaying progress.
> - **Production mystery**: Would a change actually work in production with production data? Were there any edge cases we had not considered?
>
> Frustrated by the slow pace and the additional complexity of feature branches, I explored ways to streamline the development process through code. This is how I ended up creating [Unleash](https://github.com/Unleash/unleash), a popular open-source feature flag solution that solves these problems by decoupling code deployments and feature releases.
### Benefits of Feature Flags
## What is a feature flag?
Feature flags are a key part of DevOps, and help speed up the software development process. Feature flags:
A feature flag is a software engineering technique that turns features or functionality on or off without modifying the source code or requiring a redeploy. Its also referred to as feature toggles, switches, flippers, or bits.
* Reduce the need for long testing cycles that still fail to find all possible bugs when code meets real users.
* Minimize the risk of downtime when a feature needs to be rolled back.
* Enable data-driven optimization, which is essential for improving user experience and driving revenue.
Feature flags enable you to release and test new features by making them available to a specific group of users, or no users at all, with the power to immediately turn them off without any risk to the rest of your application.
### Feature Flag Use Cases
The control that feature flags allow means that theyre already becoming a popular superpower in feature lifecycle management. Combined with feature management platforms and processes, they can enable a cultural shift within organizations towards more agile and experimental development and ways of serving users.
#### Gradual Rollouts
![Diagram demonstrating the relationship between a CI/CD pipeline, an application, and its feature flags](/img/how-feature-flags-work.png).
Gradual rollouts involve deploying a new feature to a small subset of users initially and then gradually increasing the user base. This reduces the risk of widespread issues and allows for monitoring and feedback collection in a controlled manner. [Read more about how Unleash enables Gradual Rollouts.](https://www.getunleash.io/feature-flag-use-cases-progressive-or-gradual-rollouts)
## Benefits of feature flags
#### A/B Testing
With an understanding of what feature flags are, lets look at the key benefits of using feature flags, such as improved user experience, faster release cycles, and more effective testing and experimentation.
Feature flags facilitate A/B testing by allowing different user segments to experience an app without a particular feature, or different variants of a feature. This helps in determining which version performs better in terms of user engagement and other KPIs. [Read more about how Unleash enables A/B testing.](https://www.getunleash.io/feature-flag-use-cases-a-b-testing)
### Improve user experience
#### Kill Switches and Rollbacks
Feature flags give you more control over what is released, to whom, and when. This means you have more control over the user experience of your end product. Instead of making release decisions based mostly on operational constraints and risk fears, you have the control to make features available based on what makes the most sense for specific users as well as your business goals. For example, releases can be timed to align with marketing campaigns, customer support, and product marketing efforts.
Kill switches provide the ability to quickly disable a feature if it starts causing issues in production. This minimizes the impact on users and allows for immediate rollback without requiring a new deployment. Read more about how to do [Kill Switches](https://www.getunleash.io/feature-flag-use-cases-software-kill-switches) and [Rollbacks](https://www.getunleash.io/feature-flag-use-cases-rollbacks) with Unleash.
### Level up your QA
<Figure img="/img/feature-flag-rollback.png" caption="Feature flags provide the ability to turn off code paths in production without deploying new code. This is useful when unexpected bugs pop up or in break-glass scenarios."/>
Feature flags are a powerful tool for quality assurance (QA) as they allow you to test features under specific conditions that you define. This testing happens within the full context of your live product, as your users would experience it. That is because with trunk-based development, once a feature is coded, it exists in production. However, feature flags enable you to make a feature visible only to those users you want.
### Reduce risk
Feature flags reduce risk by decoupling deployment from release, allowing teams to deploy new code to production without immediately making the changes live for all users. This approach ensures that even if there are issues with the new code, it doesn't impact users right away, providing a safer environment to validate changes.
Moreover, feature flags enable quick mitigation of issues by allowing teams to instantly disable problematic features without rolling back the entire deployment, minimizing downtime and user disruption. Additionally, they offer granular control over feature exposure, enabling targeted releases to specific user segments, which reduces the risk of negatively impacting the entire user base while providing valuable feedback from a smaller, controlled group.
### Speed up release cycles
Feature flags significantly accelerate operational release cycles by enabling rapid release, testing, and rollback of features. This speed allows teams to adopt a more action-oriented and experimental approach, quickly iterating on new ideas without the risk of complex code integrations or burdensome deployments. Even when multiple teams are working on overlapping components of complex applications, feature flags streamline the process by reducing dependencies and conflicts.
Additionally, automated feature flags can [dynamically enable or disable features based on user behavior or system events](../../reference/actions.md), further speeding up the adaptation process. By embracing a CI/CD (continuous integration and continuous deployment) workflow with feature flags, teams can deliver improvements to their applications more frequently and reliably, ensuring a faster, more agile development cycle.
### Enable testing and experimenting
A/B testing is a great example of the power of feature flags. Use your quick control of who has access to what to understand how and when users are interacting with your features. Which functionalities are serving them best? In what circumstances are new features helping or hindering your users? How are different features interacting with one another at different times?
Set up testing to serve new features to specific groups of your power users or target demographics without any risk to your build or visibility to other users. With more power to test with less risk, teams outside of the product team are also enabled to adopt an agile and experimentation mindset.
## Feature flags vs. feature branches
While feature flags and feature branches may sound similar and are often used with overlapping goals, there are key operational and outcome differences that development teams should carefully evaluate.
A feature branch is a workflow within a Git repository that allows developers to isolate the code for a new feature into a separate branch, distinct from the main branch (i.e., the main codebase). This isolation minimizes the risk of introducing broken code into the main codebase during development and after deployment.
Feature branches, while common, pose a few challenges:
- **Delayed feedback**: Gathering feedback from stakeholders can be slow and cumbersome because it often requires setting up separate test environments for each branch, adding time and complexity to the process.
- **Merge conflicts**: Integrating changes from multiple branches into the main codebase can lead to conflicts, which are time-consuming to resolve and can stall development progress.
- **Uncertainty in production**: It's challenging to predict how code changes will perform in a production environment with real data, increasing the risk of encountering unexpected issues or edge cases that weren't considered during development.
### Do you still need feature flags if you use feature branches?
In an ideal world, feature flags and trunk-based development offer a streamlined alternative to feature branches by enabling continuous integration and minimizing merge conflicts. Feature flags allow teams to deploy new features incrementally, providing control over their exposure without the need for long-lived branches. This approach promotes a more efficient development process, enhancing collaboration and reducing complexity.
However, feature flags can still provide significant benefits even if your organization requires feature branches due to specific workflows or compliance needs. All those benefits of using feature flags still apply when your central unit of analysis is a feature branch instead of main. They allow teams to merge incomplete features safely, reduce merge conflicts, enable testing in production, support gradual rollouts, and improve collaboration. In this way, feature flags enhance flexibility and control, making them valuable in any development strategy, regardless of the branching model used.
## Implementing feature flags
Feature flags enable development teams to manage features in a dynamic, flexible, and controlled manner. Like any tool, you need to use them the right way—you dont want to build a spaceship out of bricks.
<Figure caption="Source: https://imgs.xkcd.com/comics/the_wrong_stuff.png" img="/img/xkcd-comic-spaceship.png"/>
#### Trunk-Based Development
Feature flags are integrated into the software development lifecycle, allowing developers to introduce code conditionality. This conditionality determines whether specific code blocks execute, enabling or disabling features without requiring code deployments.
Feature flags enable [trunk-based development](https://www.getunleash.io/trunk-based-development) by allowing new features to be merged into the main branch continuously without being fully activated until they are ready. This supports continuous integration and continuous deployment (CI/CD) practices. [Read more about how to do Trunk-based Development](https://www.getunleash.io/feature-flag-use-cases-trunk-based-development) with Unleash.
The code for the feature is already deployed. A feature flag controls where it executes at run-time. At its most basic, a feature flag is implemented as a conditional statement in the source code. This conditional hinges on a flag value, typically retrieved from a configuration file or a feature management platform, to decide whether a particular code path should be executed.
#### Canary Deployments
## Types of feature flags
Canary deployments use feature flags to roll out a new feature to a small, representative subset of users before a wider release. This allows for early detection of issues and ensures the feature is stable before full deployment. [Read more about how to do Canary Releases](https://www.getunleash.io/feature-flag-use-cases-canary-releases) with Unleash.
Lets take a look at the different types of feature flags, including their purpose and benefits:
- [Release flags](#release-flags)
- [Experiment flags](#experiment-flags)
- [Kill switches](#kill-switches)
- [Operational flags](#operational-flags)
- [Permission flags](#permission-flags)
<Figure caption="Inspired and extended from Pete Hodgsons taxonomy of feature flag types https://martinfowler.com/articles/feature-toggles.html" img="/img/feature-flag-lifecycle-chart.png"/>
### Release flags
- **Purpose**: Manage the deployment of new or incomplete features.
- **Benefits**: Risk mitigation, incremental development and testing of new features.
- **Lifespan**: Short-lived; a few weeks or months depending on the feature's scope.
Release flags enable teams to integrate code into the mainline without immediately exposing it to all users. This practice supports continuous integration and delivery by always keeping the main branch deployable. They allow for incremental development and testing of features in production without impacting the entire user base, thereby enhancing stability and reliability.
While release flags are invaluable, they should be short-lived to prevent codebase complexity and technical debt accumulation. Once the feature is complete and tested, the flag should be removed to streamline the code.
#### Feature Management
### Experiment flags
Feature management involves the strategic use of feature flags to control the lifecycle and rollout of features. This includes scheduling feature releases, managing access based on user roles, and monitoring feature performance and usage. Effective feature management ensures that new features are released in a controlled and measured manner, reducing risks and improving overall product stability. [Read more about how to do Feature Management ](https://www.getunleash.io/feature-flag-use-cases-feature-management)with Unleash.
#### FedRAMP and SOC2 Compliant Feature Flags
- **Purpose**: A/B and multivariate testing.
- **Benefits**: Informed, evidence-based decisions on feature implementations.
- **Lifespan**: Limited to the duration of the experiment; a few days to a few weeks.
For enterprises with strict compliance requirements, feature flags must include robust security and access controls. This ensures that feature flagging systems meet industry standards and regulations, providing data encryption, secure storage, and regular audits. [Read more about how to enable compliant feature flags ](https://www.getunleash.io/fedramp-soc2-feature-flags)with Unleash.
Experiment flags allow for evaluating different user experiences to determine the most effective iterations. They segment users into cohorts, providing data-driven user preferences and behavior insights. They empower teams to optimize the user experience based on actual usage data and feedback, thereby maximizing product value and user satisfaction.
#### A Few Words About Feature Flag Security, Access Controls, and Compliance
Experiment flags are dynamic and transient. Their lifespan is inherently limited to the duration of the experiment—enough to gather significant data but not so long as to risk polluting the findings with unrelated changes or prolonging the exposure of potentially suboptimal features.
Because feature flags enable code execution, they require robust security and compliance measures. Most introductory tutorials on feature flags ignore these concepts, but you should think about how you will enable these controls early into your feature flag journey.
- **Access Controls:** Ensuring that only authorized users can interact with feature flags. Implementing role-based access control (RBAC) helps manage permissions effectively and enable the principle of least privilege.
### Kill switches
- **Audit Logs:** Maintaining detailed logs of changes to feature flags to track who made changes and when. This is essential for both compliance and troubleshooting.
- **Purpose**: Quickly disable certain functionalities or features.
- **Benefits**: Immediate remediation in the face of unexpected, critical incidents.
- **Lifespan**: Long-term or even permanent.
- **Change Requests and Approvals:** Implementing a structured process for handling change requests and approvals to ensure that modifications to feature flags are properly reviewed and authorized.
- **Single Sign-On (SSO) Integration:** Integrating with SSO systems to streamline authentication and enhance security.
Kill switches act as emergency levers that can quickly turn off certain functionalities or features in response to critical issues like severe performance degradations or security vulnerabilities. Unlike other operational flags, kill switches are designed for rapid, decisive action in crisis situations. They often impact user-facing features to preserve overall system stability or security. By preemptively implementing these switches, teams can ensure they have a fail-safe mechanism to maintain control over the systems operational integrity, even under adverse conditions.
- **Compliance:** Ensuring that the feature flagging system meets industry standards and regulations such as FedRAMP and SOC2. This is often greatly simplified when you run your feature flag solution in your own cloud or data center. Most [open-source feature flags](https://www.getunleash.io/pricing) providers allow you to self host.
Contrary to the short-lived nature of most feature flags, kill switches are inherently long-term or even permanent tools within a systems architecture. Their enduring presence allows for ongoing protection against unforeseen critical events. However, these switches must be regularly reviewed and tested to ensure they remain functional and relevant, adapting to the evolving system and operational requirements.
### Implementing Feature Flags
Let's explore how feature flags can be implemented using a simple example in Python. You can explore a more comprehensive [Python feature flag tutorial](https://docs.getunleash.io/feature-flag-tutorials/python) here.
### Operational flags
#### Step 1: Define Feature Flags
- **Purpose**: Transition between technical implementations with minimal risk.
- **Benefits**: Thorough validation of changes in a live environment without disrupting the user experience.
- **Lifespan**: Short-lived; a few days to a couple of weeks.
Feature flags can be stored in a configuration file, database, or environment variable. For simplicity, let's use a dictionary to represent our feature flags.
Operational flags manage a systems technical aspects that do not directly influence user-visible features but are crucial for the applications underlying stability and performance. For example, during a library version upgrade, operational flags can toggle between the old and new implementations to ensure the update does not adversely affect the system. By enabling real-time switching between different operational states or configurations, these flags allow for thorough validation of changes in a live environment without disrupting the user experience.
```python
Operational flags should be short-lived, existing only long enough to confirm that a change, such as a library update, is stable and does not introduce regressions. Once the new state is validated, the flag should be promptly retired to avoid accumulating technical debt. Avoid using operational flags as a solution for long-term configuration management; see our [Best practices for building and scaling feature flags](../feature-flags/feature-flag-best-practices.md) guide to learn more.
feature_flags = {
"new_feature": False,
### Permission flags
"beta_feature": True,
- **Purpose**: Control feature access based on user roles or entitlements.
- **Benefits**: Getting early feedback from select user groups or delivering enhanced functionality to premium users.
- **Lifespan**: Varies from short-lived to permanent.
}
Permission flags allow for features to be selectively enabled for different user segments, such as internal, beta, or premium users. They help with phased feature exposure and help drive engagement and value differentiation. Their lifespan can vary significantly. While they might be permanent for certain features exclusive to specific user tiers, its crucial to distinguish these from short-lived feature flags. For features eventually intended for broader release, the toggles should be transitional, aligning with broader access strategies and product evolution.
```
## Use cases for feature flags
#### Step 2: Use Feature Flags in Code
Next, let's look at some of the common use cases for feature flags, such as progressive rollouts, A/B testings, kill switches, and more.
Wrap the new feature code with a conditional statement that checks the status of the flag.
### Progressive rollouts
```python
Progressive rollouts of features—where new functionality is gradually exposed to a broader audience based on successful interim outcomes—are much better than all-or-nothing releases. This phased approach allows for collecting valuable user feedback and performance data at each stage, enabling fine-tuning or course corrections as needed.
def main_feature():
For example, a cloud storage service might initially introduce a new file-sharing interface to a small percentage of users, expanding the rollout as confidence in the features stability and usability grows. The alternative is super long testing cycles that might still miss some bugs. It is much better to ship code quickly and start with a small audience that you can validate before rolling out more broadly.
print("This is the main feature.")
### A/B testing
def new_feature():
Feature flags facilitate A/B testing by allowing different user segments to experience an app without a particular feature, or different variants of a feature. By directing different user segments to experience variant features, data can be gathered to inform which version effectively achieves the desired objectives.
print("This is the new feature.")
An online bookstore, for instance, could test two different landing page designs to identify which one yields a higher engagement rate or increases sales, thereby making data-driven decisions to enhance user experience and business outcomes.
def beta_feature():
### Kill switches
print("This is the beta feature.")
Feature flags can be used to quickly disable a feature as needed, minimizing the impact on both users and the organization deploying the feature.
def execute_features():
To implement kill switches, a good general practice is to wrap your flaky feature in an inverted feature flag. Your application should assume that the feature is working as expected as long as the feature flag is disabled. When you disable a flag by default, your application will still have the feature enabled. This is in case it cant fetch the latest version of the feature flag. If you detect any problems with the integration, you can then easily turn on the kill switch, which will then turn off the feature.
main_feature()
### Rollbacks
if feature_flags.get("new_feature"):
Feature flags enable rapid rollbacks of features that are causing issues or not performing as expected. Instead of redeploying the entire application, teams can simply disable the problematic feature, reducing downtime and mitigating risk. Imagine a scenario where a new image compression algorithm is causing longer page load times; a feature flag can quickly disable this algorithm, reverting to the previous state while the issue is investigated.
new_feature()
if feature_flags.get("beta_feature"):
### Trunk-based development
beta_feature()
Trunk-based development is a software development strategy where all developers commit code changes to a single shared branch, known as the “trunk” or “mainline,” rather than working in separate branches for extended periods. Feature flags support trunk-based development by allowing features to be integrated into the main codebase without being immediately visible to users. This encourages continuous integration and reduces the complexity associated with long-lived feature branches.
execute_features()
Developers have shifted towards trunk-based development for several reasons, primarily driven by the need for faster, more efficient, and more reliable software delivery processes. Feature flags allow teams to merge code for incomplete features into the trunk without exposing these features to all users. This way, you can continue developing and testing in production-like environments without affecting end-user experience.
```
In this example, `main_feature` is always executed. The `new_feature` and `beta_feature` functions are executed only if their corresponding flags are enabled.
### Feature management
Now, if you turn these static variables into dynamic feature flags connected to a feature management system, you can enable or disable `new_feature` and `beta_feature` dynamically without redeploying your code. Thats the beauty of feature flags.
Feature management involves the strategic use of feature flags to control the lifecycle and rollout of features. This includes scheduling feature releases, managing access based on user roles, and monitoring feature performance and usage. Effective feature management ensures that new features are released in a controlled and measured manner, reducing risks and improving overall product stability.
### Conclusion
Additionally, feature flags can be implemented in a secure and scalable manner to support enterprise-level requirements. By using robust feature flagging systems, organizations can ensure that their feature management processes comply with security standards and can scale to handle large user bases without compromising performance.
Feature flags are a versatile tool in modern software development, offering numerous advantages for managing feature deployment, experimentation, and risk mitigation.
They are a fantastic tool for speeding up the development process, reducing downtime risks, and enabling data-driven optimization. With robust security, access controls, and compliance measures, feature flags can be used at scale, ensuring stability and security in production environments.
### Compliance with FedRAMP, SOC 2, ISO27001, Air-gap, and others
Whether you're working on a small project or a large-scale application, integrating feature flags will significantly enhance your workflow and the stability of your products.
Feature flags can be managed in environments that require strict compliance with standards such as FedRAMP, SOC 2, ISO27001, and air-gap configurations. By ensuring that feature flagging systems meet these regulatory requirements, organizations can maintain compliance while benefiting from agile feature management.
## Applying feature flag best practices with Unleash
The risk and cost of building software the old way are too high. When developers are forced to use tools and processes they hate, like infrequent high-stakes releases based on complex branching strategies, innovation is [reduced by 50%](https://www.microsoft.com/en-us/research/publication/devex-in-action-a-study-of-its-tangible-impacts/). Additionally, when code is deployed and released in this manner, rolling back after discovering an issue can be difficult and time-consuming. On average, application downtime costs organizations [$400,000 per hour](https://www.ibm.com/downloads/cas/L57KW7ND), and more than half of organizations face unexpected downtime every two months.
Unleash is on a mission to make developers lives easier. Individual developers love Unleash because it removes the pain of testing and deploying new features so they have more time and energy to innovate. Unleash is trusted by thousands of companies in production including Visa, Wayfair, Lloyds Banking Group, and Samsung. While we serve the needs of some of the worlds largest and most security-conscious organizations, we are also rated the *Easiest to Use in Feature Management software* by G2.
If you want to learn more about how to implement feature flags at scale, check out the following resources:
- [Feature Flag Tutorials](/feature-flag-tutorials)
- [Best practices for using feature flags at scale](../feature-flags/best-practices-using-feature-flags-at-scale.md)
- [Best practices for building and scaling feature flags](../feature-flags/feature-flag-best-practices.md)
- [Try Unleash for Free](https://www.getunleash.io/pricing)

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 223 KiB