Feature flags are an important part of Lessy. They are used to bring piece of code even if feature is not complete. A feature flag gives the possibility to disable or enable a feature for given users, a certain percentage of users or for everyone.
First, you need to know that feature flags are handled by Flipper and we make use of the ActiveRecord adapter.
Here is a simple example to help you to start: imagine that you want to ship a
new statistics feature which is not stable yet. Let's create it in a new
migration and call this feature flag stats
:
class AddStatsFeatureFlag < ActiveRecord::Migration[5.1]
include FlipperMigration
def up
create_flag :stats, enabled: false
end
def down
destroy_flag :stats
end
end
Now, you can check if your users have access to this new feature:
irb> User.first.flipper_enabled? :stats
=> false
Obviously, none of your users have access to it since it has not been enabled for any of them yet. To enable a feature flag, there are several possibilities but we will cover only two of them.
The first is to enable the feature only for specific users:
irb> Flipper.enable_actor :stats, User.first
irb> User.first.flipper_enabled? :stats
=> true
irb> User.second.flipper_enabled? :stats
=> false
The second is to enable the feature for all or none of them based on a boolean system:
irb> Flipper.enable :stats
irb> User.first.flipper_enabled? :stats
=> true
irb> User.second.flipper_enabled? :stats
=> true
irb> Flipper.disable :stats
irb> User.first.flipper_enabled? :stats
=> false
irb> User.second.flipper_enabled? :stats
=> false
Once the feature has been shipped for everybody and you don't make any use of
it anymore, you can remove the flag based on the same migration system than for
creation (just inverse up
and down
!)
Note this system is quite new so we'll probably need to improve it.
A feature flag system can quickly goes out of control. When you create a flag, please always follow this naming convention and add the flag in the list below.
Flags which are expected to always exist must start by feature_
. Existing
flags are:
feature_registration
(default:true
): if true, users can register within the application
Temporary flags are used to help to build a feature and are expected to be
destroyed once finished. They must start by tmp_
. There are no temporary
flags for the moment.