What is a “feature flag”?

后端 未结 12 571
天涯浪人
天涯浪人 2020-11-30 17:32

High Scalability mentions feature flags here:

5 things toxic to scalability, \"5. Lack of Feature Flags\"

What exactly are feature flags?

12条回答
  •  有刺的猬
    2020-11-30 18:10

    From a coding point of view a feature flag can be as simple as an if statement which wraps around a new piece of code which you are writing. When the if statement evaluates to true (the feature flag is on) then the new code will be executed.

    In a real-world example of delivering software, the if statement described above would evaluate differently depending on environment the software is running in. For example if the application is being executed on your QA server the feature flag will return true and the new feature will be seen. If it's being executed on your production server the feature flag will return false and the feature will be hidden.

    From my personal experience during my career I have used feature flags in the following ways:

    1. Decoupling code deployments from releasing of features to customers. This was my first initial use of feature flags in our development process. We used it to remove the dependency between our marketing and product team and the engineering team that was doing the development and releases. Feature flags allowed us to deploy our code weeks in advance of a launch whereas previously we were deploying code the night before a release!

    2. Testing in production. Before we used feature flags when we released our code it was an all or nothing event, either all our customers got the feature or none of them did. We used feature flags to allow us to roll out a new feature to a small percentage of the users at a time. This allowed us to collect valuable feedback and data about a new feature without risking any potential issues to the entire customer base.

    3. Enabling/disabling a feature per environment in the development life-cycle. We used this extensively in development for allowing a much smoother deployment process - we have a CI/CD pipeline in which the use of feature flags is vital.

    4. Creating a kill switch. We have wrapped certain features of our application with a feature flag which allows us to 'kill' that feature in the event of any issues we are having with the application at the time. For example if we find ourselves under heavy load we are able to turn off certain non-essential features of the website to help with the issue.

    You can read more about feature flags here.

    You can add feature flags to your code in multiple ways.

    1. You can create your own or use a third-party feature flag library and add your feature flag data into a config file which can be included in your deployment package.
    2. You can create your own or use a third-party feature flag library and add your feature flag data into a config file which can be loaded at run-time.
    3. You can use a cloud-based feature flag management service to manage all your feature flag needs for you.

    Writing your own library may seem a good idea at first and usually it can start out that way. However you can soon run into issues when you want to implement the more advanced use cases of feature flags like rolling out to a percentage of users or targeting specific user groups. Another issue with creating your own feature flag implementation is if you are using multiple languages you will need to implement your code multiple times.

    The best and easiest way to use feature flags is to use an online feature flag management service such as Floodgate. This way you can leverage on the platform to all the heavy lifting which then allows you to concentrate on creating the feature for your application.

    Here is an example of how to add a Floodgate feature flag to an application using the .NET SDK.

    using FloodGate.SDK;
    
    var floodgateClient = new FloodGateClient("API-KEY");
    
    var flag = floodgateClient.GetValue("a-new-feature", false);
    
    if (flag)
    {
      // Execute the code for my new feature here...
    }
    

    If you are working in a development team and you are not using feature flags and you are experiencing issues in deployments and code management within the team. Using feature flags can be a great way to resolve these issues. There is also a nice side effect of feature flags speeding up your teams development velocity.

    Martin Fowler gives a very in-depth write-up of feature flags here which I recommend you to read.

提交回复
热议问题