Comparing integration of Feature Flags between ConfigCat and LaunchDarkly
Tags: featureflags, python, testing, beginners
Published: 2020-05-06 14:41:02

Feature flags are great pieces of software. They give you a controlled way to introduce features and capabilities so you don’t end up deploying a feature, only to find out it doesn’t work meaning you need to roll back your change. Instead, it’s as simple as turning on and off your change. This has undeniably saved many software engineers some heartbreak over the years.

My name is Charlie, I'm new to feature flags as a whole and thought I would run through my first-hand experience of setting them up. If you are unfamiliar with using feature flags, just as I am, or are just looking for the right service to put them to use them, you are in the right place. Today we are going to compare the integration process between [ConfigCat](https://configcat.com/) and [LaunchDarkly](https://launchdarkly.com/), using a simple program I created with Python. If you want to follow along, and python isn’t your language of choice. Not to worry! Both companies offer many SDK’s.

First, let’s talk about what we are going to build. For those who are familiar with ConfigCat and their [feature flags](https://configcat.com/featureflags) section of their website, chances are you saw the `isCoolCatEnabled` toggle and thought “wow, if only I create something as neat”, well if so, you are in luck! That’s exactly what we are going to be demonstrating today. Well, sort of. We are creating a program which when our flag is on will import an image and invert the color, saving the result to a new file. And when the flag is off, our program will delete the file.

Now you know what you’re in for, let’s take a look at integrating on the ConfigCat side. To get started, sign in to your ConfigCat account and select Add Feature Flag, create a name (in my case I have gone with “catinvert”), then click Add Feature Flag. Yes really, it's that simple.

<img src="https://dev-to-uploads.s3.amazonaws.com/i/b7fvioe95r4w2gcrpf64.png" alt="ConfigCat add feature flag"/ width="350" height="350">

From here we need to install the `ConfigCat` library so open up your terminal and enter. 

```bash
pip install configcat-client
```
If you’re following along with this post you’ll need another library too, so go ahead install the `Pillow` library with:

```bash
pip install Pillow
```
Now with everything installed, we can get into the fun stuff. Open your favorite IDE, create a new .py file, add an image into the same folder(I decided in the spirit of this post to go with the ConfigCat logo), then we can get started! To implement ConfigCat simply enter the following lines. Again, if your following along I import some more libraries, but this will be reflected as you read on.
<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/i/up6igiq3eh3yqr1an44l.jpg" alt="ConfigCat imported image"/ width="150px" height="150px">
  <figcaption> My imported image </figcaption>
</figure>


```python
import  configcatclient
configcat_client = configcatclient.create_client_with_auto_poll( 'YOUR_SDK_KEY',poll_interval_seconds=10);
YOUR_FEATURE_FLAG = configcat_client.get_value('YOUR_FEATURE_FLAG', False)

```
Our top-line imports the library we need. Our second line is what determines how often ConfigCat will download and store the values from your program, there are four options you can select for how ConfigCat goes about this, but we have gone with the poll_interval option. The final line is what allows you to access your feature flags and settings.

:bulb: Please note your SDK key and feature flag name will automatically be added. For security, please never share your SDK key. Don’t worry, we deleted our Feature Flag after we were finished with this post.

Now, we have that out of the way let's have a look at the final program.

```python
import configcatclient
from PIL import Image, ImageOps
import time
import os

configcat_client = configcatclient.create_client_with_auto_poll(
    '1uXXCAAgHzREa0_HhlhMIw/qLn4CgPtwEuTGys-Ropbmg', poll_interval_seconds=1); # <-- This is the actual SDK Key for your Production environment

while True:
    catinvert = configcat_client.get_value('catinvert', False)
    if catinvert:
        im = Image.open('logo.jpg') # imports file
        im_invert = ImageOps.invert(im) # inverts image
        im_invert.save('inverted-logo.jpg', quality=95) # saves new file
    else:
        try:
            os.remove('inverted-logo.jpg') # deletes created file
        except os.error:
            pass        
    time.sleep(1)
```
With everything in place, let’s run the program and head back to our feature flag. Simply turn the flag on, click save changes, and watch the magic unfold! 
<img src="https://dev-to-uploads.s3.amazonaws.com/i/8lhnslaqz76j0vj7tndw.png" alt="ConfigCat  feature flag"/ width="700" height="700">

The program does exactly as we intended. When the flag is on, the imported image gets inverted, and when off, the image gets deleted.
<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/i/08ygqgg7nqzieylt4w09.jpg" alt="ConfigCat inverted image"/ width="150px" height="150px">
  <figcaption> My result image </figcaption>
</figure>

With all that out the way, let’s hop over to LaunchDarly and implement the same program. Everything starts off similarly, select Create Flag, select a name (I went with “darklyinvert”), and choose your language of choice, again we went for python. Now create a folder and a .py file.
So LaunchDarkly can function, we need to install their SDK library, open up a terminal, and enter the below line.

```
echo "launchdarkly-server-sdk==6.13.0" >> requirements.txt && pip install -r requirements.txt
```
Now let's look at what lines of code we need to implement.

```python
import ldclient
if __name__ == "__main__":
  ldclient.set_sdk_key("YOUR_SDK_KEY")
user = {
  "key": "UNIQUE IDENTIFIER",
  "firstName": "Bob",
  "lastName": "Loblaw",
  "custom": {
    "groups": "beta_testers"
  }
}

show_feature = ldclient.get().variation("YOUR_FEATURE_FLAG", user, False)
ldclient.get().close()
```
The top line imports the LaunchDarkly library. Our second line sets our SDK key. Next, our user information is sent in, we are just sticking to the default information. Following that show_feature allows you to access our feature flags and settings.

:bulb: Your SDK key and Feature Flag are automatically added.

Once we have done all this, we can save and test our program. Here LaunchDarkly will show you in real-time if they have received an event for your flag before moving on, which is a nice touch to make sure everything is working as intended.

<img src="https://dev-to-uploads.s3.amazonaws.com/i/b0cfy8gpibb7gnlefjvy.png"/ width="600" height="600">

Now, let's use the same program from before to see how everything runs. Below we have the full program code so you can have a clear look at what has been changed, remember, if you're following along you’ll need to add an image into the same folder so LaunchDarkly can get to work. To be fair, let's change our image to LaunchDarkly's logo.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/i/f1f8jfh7cq97hbjcjfjm.jpg" alt="LaunchDarkly imported image"/ width="270" height="270">
  <figcaption> My imported image </figcaption>
</figure>

```python
import ldclient
from PIL import Image, ImageOps
import time
import os

if __name__ == "__main__":
  ldclient.set_sdk_key("sdk-17e098af-f9df-417b-b846-a29d65144779")

user = {
  "key": "UNIQUE IDENTIFIER",
  "firstName": "Bob",
  "lastName": "Loblaw",
  "custom": {
    "groups": "beta_testers"
  }
}


while True:
    show_feature = ldclient.get().variation("darklyinvert", user, False)
    if show_feature:
        im = Image.open('logo.jpg') # imports file
        im_invert = ImageOps.invert(im) # inverts image
        im_invert.save('inverted-logo.jpg', quality=95) # saves new file
    else:
        try:
            os.remove('inverted-logo.jpg') # deletes created file
        except os.error:
            pass        
    time.sleep(1)

ldclient.get().close()
```
As you can see, there’s not too much that has changed. The clear difference is the user information LaunchDarkly needs. Let’s run our program, head over to our new feature flag, and turn it on.
<img src="https://dev-to-uploads.s3.amazonaws.com/i/7dpvfbmes7b33ahs8v3z.png" alt="LaunchDarkly flag on"/ width="500" height="500">

The main difference to doing this on LaunchDarkly’s end compared to ConfigCat is that to turn on your flag you must enter the name of your production environment and a comment.
<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/i/43fx2fcl260gra529ph1.jpg" alt="LaunchDarkly inverted image"/ width="270" height="270">
  <figcaption> My result image </figcaption>
</figure>


As expected, everything worked as intended. Now we have looked into integration on both ends, let’s summarise the key differences:
- ConfigCat offers 4 different ways off the bat to download your values, LaunchDarkly has options for this, but you’ll need to delve into their [customization documentation]
(https://docs.launchdarkly.com/sdk/server-side/python#customizing-your-client) to set up some extra stuff with their parameters
- LaunchDarkly tests your connection in the startup process, prompting you to try again if the connection has failed. ConfigCat displays if your initial request was successful by displaying  'last accessed datetime’ message below your feature flag
- LaunchDarkly needs extra User information in your program. ConfigCat does not require this step
- Switching your flag on with ConfigCat is as simple as on-off, LaunchDarkly requires you to input some extra information

While both pieces of software ultimately aim to provide the same result of implementing feature flags, there are clear pros and cons to either. With LaunchDarkly being the market leader they offer more support options and you have the security of using a long-standing product, however since you can only use your account on a trial basis this may not be ideal for students, or people just looking to try out feature flags, as well it takes a few more steps to setup. ConfigCat, on the other hand, has a free account type which you can use for as long as you see fit, with the cheapest account type also coming in cheaper than LaunchDarkly’s cheapest account type. And hey, there's also a cute cat as the logo. Just kidding, both pieces of software are great in their own merit and I hope this article has given some extra insight into which one you should use.