What are feature flags? A deep dive
If you’ve been involved with deploying or releasing software, you have likely come across feature flags. Feature flags are a useful tool in agile software development methodology. What are feature flags, and why are they important?
In this post, we’ll do a deep dive into feature flags, looking at their use cases, benefits, and challenges. Then, we’ll examine a simple way to implement basic feature flags for your applications.
Let’s begin by answering the question, ‘What is a feature flag?'
What is a feature flag?
A feature flag is a mechanism in software development that allows developers to enable or disable a specific application feature without deploying new code. Put simply, feature flags are software switches to hide or activate features. Developers can do this by embedding conditional checks (in simplest terms, if-else statements) in the code that activate or deactivate features at runtime. Feature flags determine which branches of code are executed, which enables developers to make features visible only to, for example, a specific subset of users.
A feature flag is also known as a feature toggle, feature switch, feature flipper, or a runtime flag. A feature flag is a powerful and convenient mechanism for controlling the availability of particular application functionality.
Feature flag example
An example of a feature flag is a scenario where a development team is rolling out a new search functionality on an e-commerce website. Instead of deploying the new search feature directly to all users, the team uses a feature flag to control its availability. Initially, the flag is set to off, and the new search functionality is accessible only to internal testers or a small subset of users. This allows the team to monitor performance, collect feedback, and address any issues without affecting the entire user base.
As confidence in the feature grows, the flag can be gradually turned on for larger groups of users until it is fully deployed. If any problems arise during the rollout, the team can quickly disable the feature by toggling the flag off, ensuring minimal disruption. This approach minimizes risk and provides a safer, more controlled deployment process.
Why are feature flags important? Challenges of traditional deployment and the need for feature flags
Feature flags were developed to address the risks and inefficiencies associated with traditional deployment and release processes. Before feature flags, deploying a new feature meant making it available to all users at once. This carried significant risks; for example, if the new feature had bugs or performance issues, the only recourse was to roll back the deployment, often resulting in system downtime and complex fixes.
To mitigate these risks, developers needed a way to test new features in a production environment without exposing them to all users. This led to the concept of decoupling deployment from release. Deployment refers to the act of pushing new code to the production environment, while release involves making a feature available to users. By using feature flags, developers can deploy code with new features toggled off, making them invisible or inactive for the user until they are ready to be safely activated.
In this way, feature flags provide the flexibility to enable or disable features selectively for different user groups or under specific conditions. This means new features can be incrementally rolled out, starting with internal testers or a small group of users, allowing for real-world testing and feedback collection without widespread exposure. If problems are detected, the feature can be quickly turned off without the need to redeploy or revert code, significantly reducing the risk of downtime and user impact.
Additionally, feature flags support continuous integration and continuous deployment (CI/CD) practices by allowing developers to merge and deploy code frequently, even when features are incomplete. This facilitates faster development cycles and more robust software, as new features can be iteratively tested and refined in a live environment.
Let’s now talk about the use cases of feature flags in more detail.
Top 5 feature flag use cases
Software organizations may have widely varying reasons for needing feature flags. Ultimately, they are looking for greater control over feature availability, coupled with the convenience of not needing to update or redeploy application code. The following use cases of feature flags are some of the most common.
1. Controlled rollout
A common use case for feature flags is controlled rollout, in which you roll out a feature gradually—to a subset of users, rather than to all your users at once. You turn on a feature for some of your users, and then you monitor that feature’s performance. This gives you the margin to make any necessary adjustments before releasing the feature to your entire user base.
2. Experimentation and testing
You might use feature flags to perform feature experimentation or A/B testing. By exposing different versions of your application (for example, with feature A but not feature B, or with feature B but not feature A) to different groups of users, you can gather insights into user behavior and preferences. These insights will help you gauge the potential success of features, guiding your planning and prioritization.
3. Permissioning
You can also use feature flags to permit or restrict access to features based on user permissions. Permissions might depend on user roles (for example, chat channel members versus moderators or administrators) or on subscriptions to premium features.
4. Decouple deployment from release
As discussed above, feature flags facilitate the deployment of a feature that might not be fully ready for release to your user base. By decoupling deployment from release, you can thoroughly test deployed—but hidden—features in your production environment.
5. Kill switch
Another common use case for feature flags is to act as a “kill switch.” Used this way, a feature flag can instantly disable a feature that’s causing issues in production. Your team can swiftly minimize the potential damage or disruption by flipping the switch on a feature. With the feature turned off, your team can fully investigate the issue without the burden of an active emergency.
In outlining these use cases, we’ve probably made many of the benefits of feature flags quite clear. Let's go over them in more detail.
Top 5 benefits of feature flags
Here are the top benefits of using feature flags as part of software development:
1. Release features quickly: With feature flags, software teams are empowered to release new features quickly and safely, without the stress of an all-or-nothing launch. Developers can work directly on the main branch and use feature flags to hide their work-in-progress features from users.
2. Increase development velocity: By reducing long-lived branches and minimizing merge conflicts, feature flags help teams increase development velocity.
3. Experiment with application features: Feature flags also allow experimentation, as we’ve seen. You can test different variations of a feature to see what works best and which appeals the most to your user base. Insights from this experimentation may especially appeal to GTM teams.
4. Mitigate risk: Feature flags mitigate risk by providing the ability to toggle off features that do not behave as expected in production. By serving as a safety net, feature flags also reduce stress on your development team.
5. Simplify version control: Feature flags simplify version control, allowing you to test feature code in the production environment while keeping those features hidden from your users.
Top 5 challenges of feature flags
Although feature flags offer numerous benefits, teams that use them also encounter some challenges.
1. Hard-to-read code: When used excessively, feature flags can lead to an explosion of conditional statements, resulting in messy and hard-to-read code. This can lead to potential confusion and increased maintenance overhead. Over time, your codebase may become difficult to maintain and understand.
2. Technical debt: The accumulation of obsolete feature flags can lead to technical debt. To avoid this, you should establish a plan for cleaning up old feature flags that are no longer needed.
3. Need for strict conventions: Finally, implementing feature flags requires a robust infrastructure and strict conventions. Without these, inconsistencies and bugs can creep in, especially as multiple feature flags interact with one another.
4. Performance overhead: Each feature flag introduces conditional checks that can add to the application’s runtime complexity. In performance-sensitive applications, the cumulative effect of numerous flags can degrade performance, requiring careful implementation and optimization to minimize impact.
5. Security and access control: Mismanagement of feature flags can lead to security issues if sensitive features are accidentally exposed or enabled for unauthorized users. Properly securing and controlling access to feature flags is crucial to prevent inadvertent exposure of experimental or restricted features, which can compromise application integrity and user data.
Feature flag versus feature branch
Feature flags and feature branches are both techniques used for isolating new development work from the main branch of stable code. A feature branch is a copy of the main branch where developers can work on new features without affecting the main code. As soon as feature development is complete, the feature branch can be merged back into the main branch. With feature branches, code deployment is tied to feature release.
On the other hand, a feature flag is used to hide or show features—even those still under development—that are in the main branch and deployed to production. With feature flags, code deployment is decoupled from feature release.
Now that we’ve covered the use cases, benefits, and challenges of feature flags, let’s examine how to implement a simple system with feature flags.
The importance of feature flags in continuous delivery
Feature flags play a crucial role in continuous delivery within DevOps by enabling more flexible and controlled release processes.
Trunk-based development and continuous delivery
In a DevOps environment, trunk-based development is a strategy where developers commit their code changes directly to the main branch, or frequently integrate their changes from short-lived feature branches. This approach minimizes the complexities and risks associated with long-lived branches, such as merge conflicts and integration problems. However, integrating incomplete or experimental features into the main branch can disrupt production if not managed properly.
Feature flags resolve this by allowing developers to merge new code into the main branch without immediately enabling the new features. By toggling feature flags, teams can keep these features inactive in production until they are fully tested and ready for release. This practice maintains a clean and deployable codebase, supporting the continuous delivery pipeline where code changes are automatically built, tested, and deployed. This streamlines the path from development to production, enabling faster and more frequent deployments with less risk.
Reducing risk in software releases with feature flags
Traditional software releases often involve significant stress due to the risk of new features causing issues in production. Rolling back changes can be complex and time-consuming, especially if multiple features are bundled in a single release. Feature flags mitigate this stress by decoupling deployment from release.
With feature flags, new features can be deployed to production but remain hidden from users until they are activated via the flag. If a problem arises, the feature can be turned off instantly by toggling the flag, without requiring a full rollback or redeployment. This capability is vital for maintaining system stability and quickly resolving incidents, thus reducing the overall stress and operational risk associated with software releases.
Facilitating progressive delivery and incremental rollouts
Feature flags support progressive delivery, an approach in which new features are gradually rolled out to users. This controlled release process can start with internal testers or a small group of users, gradually expanding to a broader audience. It allows teams to monitor the impact of new features in real-world conditions, gather feedback, and make necessary adjustments before a full-scale launch.
This incremental rollout capability is particularly valuable for large-scale systems for which immediate full deployment can be risky. It provides a buffer to handle unexpected issues and ensures a smoother user experience.
Supporting rapid iteration and experimentation
One of the defining characteristics of DevOps is dynamism and the ability to quickly iterate and experiment. Feature flags enable this by allowing multiple versions of a feature to coexist in the codebase, controlled by different flags. Teams can conduct A/B testing, where different user groups are exposed to different feature variations to determine which variation performs better. This experimentation is conducted without affecting the main application flow for all users, fostering a culture of continuous improvement and innovation.
Enhancing collaboration across teams
Feature flags also enhance collaboration across development, operations, and business teams. Product managers and non-technical stakeholders can control the activation and deactivation of features based on business needs, without requiring direct developer intervention. This decouples technical and business timelines, allowing teams to align feature releases with market strategies and user demands more effectively. Technical and business teams can thus work together more effectively.
How to implement feature flags
Although you can use feature flag management platforms, such as LaunchDarkly and Split, you can also implement feature flags by adding conditional statements in your code. These conditional statements may look to environment variables or user attributes to govern whether a piece of functionality is available. For example, consider this code for a Node.js application:
In the above example, screen sharing in a Sendbird livestream event is feature-flagged, with its availability dependent on the value of an environment variable, FEATURE_SCREEN_SHARE
.
Consider the following example, in which the availability of in-app voice calls for chat users—a premium feature—depends on their subscription level:
Conditions—which can be as simple as an environment variable or a user attribute—are checked at runtime, determining whether or not an application feature is available.
Do you use feature flags in application development?
Feature flags are a powerful tool that you can use to gain a high degree of control over the behavior of your software. With feature flags, you can significantly speed up deployment, test features in production with confidence, and reduce the risks that often come with releasing new features. As we’ve seen, implementing a basic feature flag system is simple and straightforward. Although employing feature flags introduces some challenges, thoughtful planning and management of your feature flags will make the benefits worth the effort.
When you’ve built and tested your app’s features using feature flags, you might consider building in-app customer communication capabilities. When your business decides that the time is right to build in-app communication such as chat, calls, or omnichannel business messaging, Sendbird is ready to provide customer communications solutions - including a chat API, a business messaging solution, and fully customizable AI chatbot - that you can build on. You can send your first message today by creating a Sendbird account to get access to valuable (free) resources with the Developer plan.
Become a part of the Sendbird developer community to tap into more resources and learn from the expertise of others. You can also browse our demos to see Sendbird Chat in action. If you have any other questions, please contact us. Our experts are always happy to help!