How to implement feature flags
Published: 2023-11-09 03:00:04

Original Blog: https://www.featbit.co/blogs/how-to-implement-feature-flags

[Feature flags](https://www.featbit.co), also known as feature toggles, have become an essential tool for modern software development. They enable developers to control the release of new features and updates in a flexible and controlled manner. In this blog post, we'll explore what feature flags are, their classical and modern use cases, and how to implement them effectively in front-end, mobile, and server-side applications. We'll also delve into the concept of overlapping design A/B testing and how feature flags play a crucial role in building robust experimentation.

## Classical Use Cases: Derisk Your Deployments

1. Testing in Production: Feature flags enable you to test new features in a production environment without exposing them to all users. This reduces the risk associated with untested code.
2. Canary Release: Gradually roll out new features to a subset of users before making them available to everyone, ensuring a smoother deployment.
3. Allow/Block List: Control feature access for specific users or groups, giving you fine-grained permission management.
4. Beta Testing: Invite a select group of users to try out new features, gathering feedback before a full release.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ayxvgdrhuep0qvxn2zc.png)



## Modern Use Cases: SRE and Growth

1. Observability: Gain insights into feature usage, performance, and user behavior through feature flag data and analytics. This allows the system to automatically trigger a feature rollback operation at the first sign of trouble. For more information, you can visit [FeatBit's DataDog integration documentation](https://docs.featbit.co/integrations/datadog) or blog article 《[Use of Feature Flags and Observability Tools for Gradual Database Migration](https://www.featbit.co/blogs/feature-flags-observation-tool-gradual-db-migration)》
2. Experimentation: Implement feature flags to A/B test new ideas, gather data, and make data-driven decisions about feature effectiveness. Feature flags give you a more flexible solution to meet the traffic splitting requirments in a complex experimentation design, such as Orthogonal Layering, Mutually Exclusive, etc. You can combine a feature flag tool with your data analysis tool to create a better experimentation system rather than buying an expensive third-party AB testing service.
3. Software Monetization: Use feature flags to control access to premium features, subscriptions, or other monetization strategies. This allows you to adapt your pricing model and offerings based on user behavior and feedback. You can read following article to get a realworld example, 《[Sentinel EMS Workflow with Feature Flag Management Platforms](https://docs.sentinel.thalesgroup.com/softwareandservices/EMS/EMSdocs/UG/featureflags.htm#:~:text=Use%20Sentinel%20EMS%3F-,What%20are%20Feature%20Flags%3F,runtime%2C%20without%20deploying%20new%20code.)》
4. And so on.

## Standardizing with OpenFeature

The trend in feature flag implementation is to standardize with solutions like [OpenFeature](https://openfeature.dev/) that provide SDKs and templates to streamline the process. Standardization is beneficial for 2 reasons:

1. Avoid Vendor Lock-In: Many third-party [feature flag tools](https://www.featbit.co/blogs/Free-and-Open-Source-Feature-Flag-Tools) now support the OpenFeature SDK standard, reducing the risk of vendor lock-in and giving you more flexibility.
2. Essential Interfaces: OpenFeature provides essential interfaces to help you build a robust feature flag SDK. These interfaces help ensure you follow best practices and maintain consistency.

## Implementing Feature Flags Across the Stack

A feature flag can be implemented across all your software applications: Web and Mobile APP, Back-end Services, Algorithms, and so on. Here's an example of how to use a feature flag across an entire project. Suppose you have a new method to tell users when to buy Google stock. The new version changes the user interface, the backend data source, and the calculation algorithm, so you need to keep all pages consistent for the same user.

You can create one or more feature flags to control the new version release. Here we will create a single boolean feature flag to control the release of a new feature. First, we will create a feature flag called "The Art of Buying Stocks". The figure below shows what a feature flag looks like in FeatBit's user interface.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2if80wni7f4kyee9u8z0.png)

Then let's see how we implement this feature flag in our front-end and back-end applications. 

Suppose you have a UI that suggests when to buy Microsoft stock. You have two components, each presenting a different UI view for a different version of "The art of buying stocks". You use a feature flag to control which component is displayed or not. Here's an example in VUE code (if you're using the feature flag React, you'll just need to modify the code with React style):

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5r3yiwovkwq3le8o3xba.png)

To make the user see the same UI in both Web APP and Mobile APP, you should also implement such kind of work in your Mobile APP. 

Suppose the new method (m2) needs to read additional data different from m1. You now have several ways to implement it. Here's my favorite ways (one or another):

- Add feature flag that wraps and controls the different data retrieval functions in its parent function. But this adds some extra work to clear the function flag in both front-end and back-end code.
- Add a new API for the new method (m2), so we don't need to add a feature flag in the backend anymore. But:
  - Adding a new API may cause extra work for the API gateway.
  - You have to delete the whole API when you finally choose the new method.

Here's an example in asp.net core. If the feature flag returns "m2", the program will retrieve the data source using the "RetriveDataFromFeatBiDB()" function.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7e6jj9ljxj1xe6nusws.png)

After you implement the code in your application, the rest things you need to do is just control and release the feature in the UI. As shown in the figure shown below, you can set a custom rule to release new methods only to users who live in California. To minimize the potential risk, you can reduce the blast radius to just 10% of users. (This is also called progressive rollout)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fu1rv70qkmpoxs2r2y10.png)



## Building Robust Overlapping Design A/B Testing

Overlapping design A/B testing involves running multiple experiments simultaneously, where users can be part of multiple experiments simultaneously. A robust feature flags service should meet the following requirements:

1. Able to create Orthogonal layers to stacking tests in an independent, non-overlapping manner. Each variation remains isolated, simplifying analysis and minimizing conflicts and unintended consequences.
2. Should be able to avoid conflicts, it's essential to make parameters mutually exclusive. Feature flags are instrumental in achieving this, allowing precise control over which users experience specific variations or changes.
3. Able to divide your user base into distinct groups based on relevant attributes or behavior. This allows you to run experiments on specific user segments, providing insights into how changes affect different groups.
4. And so on.

Many [feature flag tools](https://www.featbit.co/blogs/Free-and-Open-Source-Feature-Flag-Tools) have tried to provide features to support this. FeatBit is a good open source solution to achieve this goal. Figure below shows how FeatBit empowers the AB test design with orthogonal layers and mutually exclusive split feature flags traffic in a single layer.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9c7tin7e53pkx9xqnudm.png)

## Feature Flag Open Source Tools

I wrote before another article about the tools, read article 《[Best Free and Open Source Feature Flag Tools](https://www.featbit.co/blogs/Free-and-Open-Source-Feature-Flag-Tools)》.

## Conclusion

[Feature flags](https://www.featbit.co) are a critical component of modern software development, allowing you to derisk deployments, run experiments, and maintain a flexible release strategy. By standardizing with solutions like OpenFeature and implementing feature flags consistently across your stack, you can achieve a robust and adaptable approach to software development and testing. Embrace the power of feature flags to build better products and make data-driven decisions.