Feature flags are a powerful tool in software development, allowing developers to control the behavior of an application at runtime without deploying new code. They enable teams to test new features, perform A/B testing, and roll out changes gradually.
In Ruby on Rails, feature flags can be managed using diverse tools, the most popular being the Flipper gem. This article will explore implementing and measuring the impact of feature flags in a Solidus storefront using Flipper and AppSignal's custom metrics.
What Are Feature Flags in Rails, Again?
If you are looking for an introduction to the subject, check out the post Add Feature Flags in Ruby on Rails with Flipper.
In a nutshell, though, feature flags are a way to influence how your application behaves at runtime, without having to deploy new code. The simplest type of feature flags are environment variables. Every Ruby on Rails application uses them out of the box. One example is the configuration of application server concurrency using ENV['WEB_CONCURRENCY']
.
However, there are other ways to manage feature flags, such as using a persistence layer like ActiveRecord or Redis. A comprehensive way to do this is offered by the Flipper gem.
The following snippet exemplifies how the performance_improvement
feature flag is evaluated for a given user:
Next, we will set up a Solidus storefront to start experimenting with feature flags.
Our Example App: A Solidus Storefront
To measure the impact of feature flags in a somewhat realistic scenario, let's quickly bootstrap a Solidus store:
This generator will guide you through the process and ask you a few setup questions.
- Choose the starter frontend when queried for the frontend type.
- Skip setting up a payment method.
- Choose to mount your Solidus application at
/
, since we are using it as a standalone app.
Afterward, run bin/dev
from your terminal and you should be good to go. When you go to http://localhost:3000
, you'll see this screen:
Implement Feature Flags with Flipper
Now let's implement two exemplary use cases for feature flags:
- A performance improvement.
- An attempt at conversion rate optimization.
First of all, though, we have to add the flipper
gem along with its active_record
storage adapter:
This will set up the required database tables to look up Flipper "gates", i.e., concrete conditionals to evaluate when checking a feature flag.
Testing a Performance Improvement
To assess this scenario, we will simulate a slow request in the storefront by adding a sleep 1
call for the unoptimized case:
Now, we will use a "percentage of time" strategy to roll out the optimization across a random set of requests. Open a Rails console and key in the following:
Using the oha load testing tool, we can confirm that indeed half of the requests take one second longer than the others:
Testing Conversion Rate Optimization
When dealing with user-facing features, for example, changes in the UI, it's often advisable to use a "percentage of actors" strategy to roll out flags. This way, every user is consistently offered the same experience.
So to start, we'll create two users for our e-commerce application. Fire up a Rails console and issue the following commands:
This creates two sample users and ensures that the feature flag is consistently enabled for one of them.
To simulate a feature attempting to drive conversion rates up, we'll make the checkout button pulsate:
If we log in with both users and arrange the browser windows side by side, we can observe that indeed the effect is active for one (the left) user:
Use AppSignal Custom Metrics to Measure the Impact of Feature Flags
The best feature flag system is useless if there's no way to evaluate its impact. In our example scenario, we simply want to know:
- Has the performance improvement led to a significant latency reduction?
- Has our pulsating checkout button led to a significantly higher conversion rate?
We will use AppSignal's custom metrics to measure the pay-off of these optimizations.
First of all, create a new application in your AppSignal organization and connect it to your app by following the instructions:
Measuring Latency with a Measurement Metric
We have verified how effective our improvement is with the oha
CLI above, but to make valid judgments we'll install server-side telemetry that reports latency to AppSignal. A measurement metric allows for exactly that: we will send over response times in milliseconds, and add a metric tag indicating whether our performance optimization was active for a specific request.
There's a small gotcha here: because we're employing the "Percentage of Time" metric, we have to capture the flag's state in an instance variable so that the same value is used for execution and for reporting:
Now let's repeat the local load testing from above:
We'll look at charting and evaluating this metric in a bit. Before that, let's turn to our second feature flag.
Tallying Conversions with a Count Metric
We'll use a counter metric to count conversions. This is a great choice if all you want to do is just keep a tally of an event.
To do this, we'll have to open CartsController
, and, for demonstration purposes, add an increment_counter
call if the checkout button is clicked:
Now let's test this by manually opening respective browser windows and clicking the "Checkout" button 3 times, and in another case only once. In this way, we can see if the optimization flag is active.
Set Up Custom Dashboards in AppSignal
Our final step is to create informative graphics to make data-informed business decisions. We'll use AppSignal's dashboards to achieve this. Let's go through this step by step:
- In the left sidebar, click "Add dashboard" and name it "Feature Flag Evaluation".
- Click "Add Graph" and the
products_response_time
metric. Select "mean" to display only averages and apply theperformance_improvement_enabled
tag.
- Click "Add new Graph" to add a chart for the checkout counts. Again, apply the
optimization_active
tag.
Now your custom dashboard is ready. In the line graph on the left, you can assert that your performance improvement was effective. On the right, observe how the higher count of checkouts in the optimized case was recorded.
And that's it!
Wrapping Up
We've seen how feature flags offer a flexible and efficient way to manage and deploy new features in a Ruby on Rails application. By using tools like the Flipper gem and AppSignal's custom metrics, developers can not only control feature rollouts, but also measure their impact on performance and user behavior.
This approach ensures that new features are thoroughly tested and optimized before being fully deployed, ultimately leading to a more stable and user-friendly application. Finally, it can lead to more informed business decisions when gauging the effectiveness of alternative approaches.
Happy coding!
P.S. If you'd like to read Ruby Magic posts as soon as they get off the press, subscribe to our Ruby Magic newsletter and never miss a single post!