Feature Flipping with Rails and Rollout

Photo by Cameron Venti on Unsplash

Sometimes you want the ability to flip a feature on or off, or only give certain users access to new features in your application. Maybe you want to get user feedback before the final release? Maybe you want the ability to revert to the previous functionality with a single command.

With the Rollout Gem, this type of functionality is extremely easy and straight forward to set up in a Rails application.

Rollout allows you to enable or disable features without having to revert changes in the repository or promote different branches to production.

It does this by saving a “feature flag” in Redis which is either on or off. You can enable this feature flag globally, on a per-user basis or toggle the feature on and off for specific groups of users.

In order for Rollout to work, we need to have a Redis client set up in our application. I’ll walk through how I set up Redis for the demo app I created for this write-up. But first, let me show you what the app is.

The demo app is a very simple to-do app. A user can create and delete todos, and mark them as complete.

Setting up Redis

As mentioned above, Rollout uses Redis to store the state of feature flag. So let’s set that up.

Add the and gems to you .

Make sure to install these new gems with .

Create an env file and add the key for the Redis URL. Here’s what I did for local development.

Create the file.

Add this to the file we just made.

Next, I added an initialization file for Redis

and add this to that file.

You can check that this is working by opening up the Rails console and typing . You should see something like this in the console.

Right on. Now that Redis is set up, let’s see how you can implement and use Rollout.

Adding Rollout

I’m going to add a feature that allows a user to mark a todo as important. The only thing here is that I’m going to “wrap” this feature markup in Rollout so I can turn it on or off extremely. Let’s set up Rollout first.

Add the rollout gem to your

and install with .

Now we need to update to get rollout working.

Make sure you restart your rails server. You can check if this worked by starting up the rails console and running .

You should get something back like this.

pre>

Rollout is now ready to use!

Wrapping Features

I’ve added the column to the table and updated the view and controller. Here is what the view file looks like.

You can see that I’ve wrapped the toggle important link in an if statement.

The method checks Redis to see if the `important_todos` feature is…active. If it’s not, this will return false and nothing will be rendered.

Without this feature enabled, the app looks and performs the same as before the new code was added.

If we want to activate this feature, all we have to do is open up the rails console and run

This will save the feature in Redis and set it to active. The next time a user visits the todos page, they will see the link to toggle important todos.

The great thing about this is that everything happens in a live environment. We don’t need to push a different branch if, for whatever reason, this new feature needs to be disabled, or restart any dynos to see the changes take effect.

All we have to do is start up the rails console and run and boom! No more important todos feature.

Here is a short clip of all this in action.

There’s more to Rollout than just flipping a single feature on or off. Like I said before, you can enable features for specific users or place a bunch of users into groups (user testing) and enable features for the whole group. Be sure to check out the Rollout Docs to see how this additional functionality works.

More about:

Gabriel Martin