#239 Feature flags and A/B testing

02/08/2025
admin features Gem

Any mature Rails app needs feature flags.

  • Usecase #1: Product & QA people can test features before releasing them. They can enable/disable features without developer involvement.
  • Usecase #2: A/B testing (partially release features)

Flipper is a great gem to manage features. It provides handy backend methods, but most importantly a great UI to manage features.

Transcript
Hello friends, all the serious applications that I worked with, meaning, uh, that there are lots of users, lots of, uh, developers working on the application. So all these applications were using feature flags to manage feature releases. Like let's say developers built a feature and product people or fault assurance, people want to test the feature before releasing it to users. So you would use feature flags or if, uh, you want to ab test features, let's say you release the feature to part of the users, but don't release the feature to another part of the users. So it'll still be using feature flags. And in all these cases, if we're using the Gem Flipper to manage, uh, features in the Rub rail application. So in this episode, we're going to install Flipper in Urban Rails application and see how it works. And we're going to do it, uh, in my case, on top of my Mono Gun Ribbon Rails poly application. You can use it or you can add this, uh, flippy jam to your own Ribbon rail application. So let's try to install Flipper. Uh, they have really nice docks. I'm going to docks. And in getting started, they're going to install not the cloud version first, but they're going to install the cloudless. So like just the gem that you can use. So bundle add Flipper, active record, Uh, then Flipper set up So it adds an, uh, initializer and adds immigration. So Flipper features are the names of all the features like, uh, search or I know, uh, billing, whatever. And Flipper Gates is, uh, whether a feature is enabled globally or not, whether it is enabled for a specific user or not. So like all the enablings of a feature. So we've kind of installed a flipper, let's say Rails did Beam migrate and see how it works in the console. Let's say Rails Console. Let's open Flipper, flipper dot. Uh, let's create a new feature. Let's name it Search Flipper. Search. Do you see State is off by default. Enable, let's again, call Flipper. Search State is on. So it is enabled. Let's check it if it is enabled. Enabled, it is true Search Disabled Enabled is false. So we created a feature flag and we can use it in our backend or front end. So let's, uh, start the rail server and try to, uh, hide something behind the feature flag. Let's try hiding search behind the feature flag. Now, um, in many cases you just hide in the front end behind the feature flag is enough. Uh, but you can sometimes also hide backend, like sometimes you have different calculations in the backend, uh, based on the feature flag. But, uh, now we'll just hide the, the front end search, then going to hide the backend part of it. So let's go to our NEF bar And uh, we have our search. So I will say if flip search is enabled, we can do it for this syntax or we can do it for the the Syntax Flipper. Um, search enabled, uh, either should work, uh, so if flip a search is enabled, then show the search. So you see now it is disabled, uh, I will enable it, flip a search, enable I refresh, and now it is, uh, enabled. So now we globally enabled or disabled, uh, uh, feature. But, uh, the whole point of installing this kind of gems is not that the developers can, uh, uh, enable all disabled features, uh, in the Rails console or in the code. The whole point is, uh, to let the non-developers see a list of features and, uh, they can enable or disable features as they see fit. So let's say the product person, then you hand over the feature to him, he, uh, test the feature and says, okay, the feature is good to go. Uh, I'm going to enable it. So the developer is already not responsible for enabling the feature. It's already like handing off the responsibility to, to another person in the team. So, uh, you would want to have a UI value, uh, product. People can see all the features that, uh, exist and they can enable or disable the features as they see fit. And here Flipper starts shining because they have this, uh, uh, separate flipper ui, uh, gem. Here's the link to the Gem Flipper ui. Let's try installing it. You see, it'll give you a list of features and you can see whether the feature is enabled or disabled and the non, uh, developers can manage, uh, the features. So let's add the gem, flip a UI, and we'll need to edit to our roots. Uh, now I can edit to my roots, but here I already have, uh, a separate folder for admin route. So inside my route I have a file named admin and here already have this, uh, um, concern Authenticate user, uh, user admin. So this authenticate user is provided by, uh, device and it checks if the user has a admin bullet set to true. And I'm going to add the Mount flipper to, not flipper, but feature flags. This is less branded. So I will, uh, now restart the server and uh, go to slash uh, feature flags. And here I have, uh, uh, the list of our features. So I have the feature search, let's start designing it in the console. I refresh here. You see it is disabled and I can now manage the feature from the ui and it is much more comfortable than managing these features through the console. So again, I will go back here. Now it's disabled. Now it is, uh, enabled. And now let's try enabling the features, not globally, but for specific users or for specific organizations. So let's say we are going to enable search only for, uh, one specific user for, uh, this user and disable search for all the other users. Let's say we want to enable it only to like the person who is testing the feature. So, um, I'm going to find this user, user, uh, find by email.id and it's the user with ID four. And I want to enable, uh, search only for user of ID four. So going to Flipper, uh, docs, we have this Flipper Enable Actor search for, uh, user find Four. Okay, and uh, going back to the Flipper ui, uh, um, yeah, I'll disable it globally and enable it only for this, uh, user. So that's again, run this command and you see it is enabled only for user with the ID for if I refresh. You see, it is not enabled. Why is not enabled? Because I need to register, uh, this kind of actors in the our Flipper Initializer. So going to our flipper, initializer, I'm going to say, um, flipper Register users, um, actor. And here I'm going to say value starts, uh, with users. And yes, it also gives me an example of how we can register features on an organization level. So, uh, now we can enable features for specific users or specific organizations. I will restart the server And see if this user has the feature. He doesn't have the feature yet because I need to check if it's enabled for the current user. So I add current user to this, uh, check, and now it is enabled for the current user. I can, uh, also do it here, search current user. So like there's a two different syntaxes that should both work and you can use either, uh, that you find better for yourself. So you see now search is enabled for this user. Let's just double check that it is disabled for another user. Let's, um, try sending in as another user. Okay? And for this user, uh, search is disabled. So like this, you can generally manage features for specific users or for specific organizations. Let's say, uh, we have an organization and you want to hide projects. So, uh, uh, I know let's go to Project Controller and uh, uh, set. Yeah, we can, um, add this set feature flag or whatever you can name it in any way. Uh, uh, return, uh, redirect to, uh, route Path, uh, unless Flip Projects enabled for Go organization. So this I can enable projects only for a specific organization. I refresh. Now you see it redirects me back to organizations each time I, uh, go to uh, projects. So now I will, uh, enable projects for a specific organization. Um, let's, uh, go, uh, and find organization id. Um, yeah, we can find it by this identifier Organization. Find id. Uh, so let's enable the feature flag. Uh, named search. Oh, named Project for a specific organization. Let's, uh, create, uh, projects and enable it. Uh, let's first try globally. So now I go back, you see projects works here, I create an organization, projects works here. Now I will disable it. So now it is disabled. And now I will enable, uh, organization. I think, uh, It should be organization and the idea of the organization will be five. Okay, now going back here, projects FE and here projects do not work. So you see this where I can generally enable, uh, features on a user level, on an organization level all globally. So, uh, you see we have, uh, both features partially enabled. Now let's enable search globally and everybody has access to search. So this is, uh, the basics of using the, uh, feature flags with the Flipper. Now another small quality of life improvement you can make is hide the marketing information in the Flipper ui. So, um, to do this, we can go to our Flipper RB and uh, we can see what, uh, is the configuration for Flipper ui. So flipper ui dot configuration. And here's a bunch of different configuration that we can manage. So I will say Flipper ui, configure the config. Fun, false. Uh, so like the text is going to be less if there is any cloud recommendations, false, uh, and show feature flag descriptions if there are descriptions of, of feature flag control. So, uh, I will restart once again, refreshing. You see the marketing information is, uh, missing and it looks more professional. So that's, uh, it I really recommend using Flipper. It's a great gem.
1
Join the conversation
Sign in to leave comments and save lessons to your watchlist.
We collect your email address, name and username to create your account. We do not share your email address with anyone else.