“Feature Flags and LaunchDarkly”- what, why and how?

i-see-you-like-feature-flags-so-were-going-to-copy-a-feature-flag-to-another-feature-flag

In this blog, I am going to discuss:

  1. Feature-Flags.
  2. How to start working with Feature Flags.
  3. LaunchDarkly.
  4. Optimizing Feature-Flag full potential and power with LaunchDarkly.

So let’s start with an overview of what a feature flag is and its uses.

1). Feature-Flags.

(a) Introduction: Feature-Flags is a software development best practice of gating functionality. Functionality can be turned on/off via the feature flags, separate from deployment. Feature flags can help manage the entire life-cycle of a feature, where a feature is an increment in program functionality.

(b) Getting Started with Feature-Flags: Feature Flag gives a software organization the power to reduce risk, iterate quicker, and get more control allowing to move fast and not breaking things. Feature flags allow us to decouple feature roll-out from code deployment that gives an unprecedented control to us, of who sees what, when and independent of release. Allowing the control over a release unlocks the true power of our software.

Feature Flagging is a method by which developers wrap a new feature in an if/then statement to gain control over its release. By wrapping a feature with a flag its possible to isolate its effect on the system and to turn that flag on/off independent from deployment. Feature Flagging is a core component of continuous delivery that empowers software organizations to release quickly and reliably.

FeatureFlags

2). How to start working with Feature Flags.

The process of feature flagging is fairly straightforward: we wrap our features in conditionals for full control over that feature’s visibility. At an enterprise scale, organizations must confront the complexities of mitigating technical debt, managing developer workflows, compliance, and controlling the life-cycle of feature flags. To meet these challenges, an enterprise-grade feature flag platform built specifically for development teams is required, LaunchDarkly provides such a platform.

3). LaunchDarkly.

LaunchDarkly is a continuous delivery and feature flags as a service platform that integrates into a company’s current development cycle. The platform allows companies to continuously deliver and deploy software to their users in a faster, more reliable way. We wrap our features in LaunchDarkly flags, which allows us to have full control over who sees the feature (users or user groups). It helps companies perform gradual feature rollouts, disable buggy features without re-deploying, and reduce development costs with a more streamlined development cycle.

So moving ahead we’ll explore LaunchDarkly and how does it help supporting FeatureFlags.

launchdarkly-dark-bg

4). Optimizing Feature-Flag full potential and power with LaunchDarkly.

LaunchDarkly provides several benefits in Feature-Flag-Driven development. Starting off with its General Uses.

Usage1: Early Access.

It allows us to run beta programs on our live application by explicitly including the people we want to see a new feature. It is a common best practice to roll out to a specific group of persons before others. LaunchDarkly provides with this feature to be available to specific people beforehand its actual delivery.

Usage2: Kill Switch.

A well-wrapped feature means we can quickly turn it off if it’s performing poorly. This can be the difference between a public relations disaster and minimal impact. When we combine this with percentage rollouts, it’s even more powerful. we can rollout to say 1% of our users and get feedback. If something goes wrong we’ve disrupted the smallest possible audience. If we’re doing this right, we can essentially take risk off the table.

Usage3: Incremental Roll Outs.

We can do phased rollouts to percentages of our users to verify there are no scalability issues. We can roll out to a random percentage of users to verify to get feedback. This is particularly good for infrastructure projects. If we’re bringing on a new back-end and want to make sure it can handle the load in the real world. Instead of releasing the new functionality at 4Am in case it didn’t work well, we can do this during normal business hours. Ramp up to 2% at 9Am to ensure it’s all working properly and keep ramping up during the day. We can even turn it completely off to leave for lunch when we aren’t around to monitor it.

Usage4: Block Users.

While a feature flag can enable a specific user or user group to access some element of our site, there is also the capability to protect features from users by excluding them from ever seeing them. Sometimes it’s someone in our own organization who we don’t want to see a certain feature until it’s more baked. We may also want to block an IP or anyone in a certain domain, which we can easily do by LaunchDarkly UI.

LaunchDarkly UI offers great control over our feature flags in a way that is easy and simple in terms of understanding and flags manipulation. It provides us with a Flag management dashboard to manage the lifecycle of our features from local development to QA, to production.

Let’s Start off by creating our own feature flag 🙂 ,

Step1: Go to the signup page of LaunchDarkly, and signUp for your 30-day free trial account.

SignUp_Here

Now log in to your freshly created LaunchDarkly account.

Ps:- LaunchDarkly is not a free platform service after your trial has ended you are required to select a plan as per your requirements. 😛

Step2: Create your first feature flag.

After you log in, first select the environment. LaunchDarkly provides us with two default environments (Production and Test), select test environment. You will land on the Dashboard Page when you have selected the test environment. Click on the “New+” button to add a new feature flag to your environment.

A panel opens up on the right side of our window, fill in the details as per the requirements, for help see the below image:

createFeatureFlag

Press save Flag to save your newly created feature flag. In the above image, I have generated a sample flag just for demonstration purpose and I have set it to be a Boolean flag(you can have a multivariate flag too) so that I can use it in my application as a conditional constraint.

Step3: Set properties for your feature flag.

After completing the step2 you can see your Flag has been created and can be accessed through the Dashboard just click over your feature Flag and you will see a new window that allows you to set properties for your Flag, see below:

TargettingFeatureFlag

Property1 –  Targeting:

From Targetting properties we can specify the individuals to whom we want to have our feature available to or whom we don’t want to have access to our feature(Specify this in TargetIndividualUsers and UsersWhoMatchTheseRules area). Also, we can just pass on a default rule in case none of the conditions are satisfied, in our case let’s just set our boolean flag to “Serve False” as a Default rule.

We also can turn “Targetting” on/off and make our flag to behave differently in both the cases, for example; in our case, we can specify `If targetting is off, serve = False`.

Property2 – Variations:

In the variations tab next to the Targeting tab we can set the possible variations of our flag and also can name them so that we can use them in the DefaultRule section under targetting tab.

Property3 – History:

In the History tab next to the Variations tab we can see a complete history of any modifications or any details of access to our feature flag.

Property4 – Settings:

Under the Settings tab, one can see details of the feature flag, like who created that feature flag(Maintainer), and the tags associated with the Flag. We can also delete the flag from here so that it gets deleted from all the environments in the project.

Now then we are done with creating our feature flag 🙂 , it’s time for us to start accessing this feature flag through our application, so why wait :-/ let’s start off with the implementation:

Hands-On with the LaunchDarkly client:

When we create an account at LaunchDarkly we are by default provided with two environments, both the environments (Test and Production) have their own set of SDK-key, which is used to connect the LaunchDarkly SDK to a specific environment. Each feature flag that we create has its own set of targeting rules for each environment. This means that we can change our flag rollout rules in a development or staging environment for QA testing before rolling out to production.

Here we will be discussing Flags maintenance only with reference to Java SDK, however, LaunchDarkly provides support for `Python, Ruby, Go, Node.js, PHP, .Net, iOS and Android` too.

1) The first Step we need to do is to add the SDK to our project.

<dependency>
  <groupId>com.launchdarkly</groupId>
  <artifactId>launchdarkly-client</artifactId>
  <version>2.3.2</version>
</dependency>

Next thing to do is to import the LaunchDarkly client in our application.

2) Import LDClient.

import com.launchdarkly.client.*;

3) Configure the LaunchDarkly Client with our SDK and create the client.

LDClient ldClient = new LDClient(Enter_YOUR_Environment_SDK_KEY);
4) At the end, we require creating a LaunchDarklyUser which will then access the Flag from LaunchDarkly.
LDUser user = new LDUser("Your_User_Email");
boolean showFeature = ldClient.boolVariation("your.feature.key", user, false);
if (showFeature) {
  // application code to show the feature 
}
else {
  // the code to run if the feature is off
}

Here “your.feature.key” is the feature key of the flag that we created through the LaunchDarkly UI. For example, here we created a feature flag named `ShowEnabledFeature` and correspondingly its feature key was `show-enabled-feature`, so I would write something like:

boolean showFeature = ldClient.boolVariation("show-enabled-feature", user, false);

There, we are all done with setting up the necessary connections to the LaunchDarkly.

For more details on how the code is working or how we are fetching from LaunchDarkly or how FeatureFlags are working, one can see how I have implemented the whole, I have pushed my code here- LaunchDarklyImplementationCode .

Hope this helps in understanding feature flags and Launchdarkly, in case any doubts please feel free to put them in the comment section.

Happy Reading. 🙂


knoldus-advt-sticker


 

Written by 

Prashant is a Software Consultant having experience of more than 1.5 years. He is familiar with Object Oriented Programming Paradigms and has worked upon Java and Scala-based technologies. He is a good team player and always eager to learn new and advance concepts in order to expand his horizon and apply them in project development with his existing knowledge. His hobbies include Teaching, Playing Sports, Cooking, watching Sci-Fi movies and travelling with friends.

1 thought on ““Feature Flags and LaunchDarkly”- what, why and how?

Leave a Reply

%d bloggers like this: