Serverless Apps and CloudBees Rollout Feature Flags
Tags: monitoring, serverless, programming, devops
Published: 2019-11-18 18:20:36

---
title: Serverless Apps and CloudBees Rollout Feature Flags
published: true
description: Discover how simple it is to use feature flags in a serverless environment and why CloudBees Rollout is a great option for it.
tags: monitoring, serverless, programming, devops
canonical_url: https://rollout.io/blog/serverless-apps-and-cloudbees-rollout-feature-flags/
cover_image: https://p78.f0.n0.cdn.getcloudapp.com/items/o0uxO421/Image+2019-11-08+at+10.40.26+AM.png?v=5a6c54b4062b5816cf7a01665ad20ed0
---

Serverless applications (ones that you would deploy to AWS Lambda or Google Cloud Functions or similar) are often the most convenient way to ship a modest piece of functionality: perhaps a web app or event handler.

In this post, I will cover how simple it is to use feature flags in a serverless environment, and why CloudBees Rollout works very well for them by design.

## An example

Cloning [_this_](https://github.com/michaelneale/rollout.io-serverless) example from GitHub, you can deploy it as easily as:

```
gcloud functions deploy my_function --trigger-http --runtime "python37"
```

Once you setup your environment id. This will give you an HTTPS URL you can hit up and then control via a feature flag experiment ([CloudBees](https://support.rollout.io/docs/initial-setup) [Rollout quickstart here](https://support.rollout.io/docs/initial-setup)). It’s very simple in this case with Google Cloud functions, but it will work elsewhere too.

The first time you access it, you probably don’t notice, but there is a small delay (usually subsecond) as the function is warmed up – and the flags loaded. The biggest hit will be the preparation of the function dependencies and environment.

It is that simple – and for the most part, you can follow that pattern and there isn’t much more to it.

Take a look a bit closer and this sample function at where you setup CloudBees Rollout vs where you use flags:

![rollout feature flags](https://rollout.io/blog/wp-content/uploads/sites/2/2019/10/image2-4.png)

In the top of your function code (which is uploaded along with requirements.txt in this case) you have the imports and the setup code to initialize CloudBees Rollout. It is important this goes here, not in the function below (which actually does the interesting thing and uses your flags) – as you only want to initialize CloudBees Rollout once when the function is initialized by the cloud function service you are using. In Amazon Lambda this is sometimes called the [execution context](https://docs.aws.amazon.com/lambda/latest/dg/running-lambda-code.html) – which is all the stuff that needs to be loaded before your function can be invoked (and is reused between different invocations of the function… there is more you can read on this in the links later on if you are curious).

This is really all most people need to know – follow the above pattern and you will be making good use of the serverless pattern along with feature flags, without any real downside.

## CloudBees Rollout flag config loads fast

One of the nice aspects of the stateless design of the CloudBees Rollout service is that when you load flags in your application, there is no “computation” going on – the flags are loaded from heavily cached and nearby CDN nodes (not on the Rollout servers). This speeds up and simplifies loading times a lot. Should flags change, updates are pushed to your application as needed.

The CloudBees Rollout docs have a fairly [comprehensive guide](https://support.rollout.io/docs/flags-update-flow) on the “update flow” for updating flags on the client:

![updating feature flags](https://rollout.io/blog/wp-content/uploads/sites/2/2019/10/image3-3.png)

On the far right is the rollout storage service – which still doesn’t require “computation” – so even in the case where you flags aren’t locally stored somehow, it is still coming from a cache, that is the “worst” case (which is pretty good!).

So for most people the example above is fine. There is a small (subsecond) overhead at worst for loading the flags, but if this is too much for you… read on there is more we can do to optimize.

## On cold starts and getting fastest load times

As mentioned in the above example, when a function first loads (and in most cloud function services, it means each time it is loaded for a concurrent invocation, as a function only serves one request at a time) there is a “cold start” where the execution context is loaded. Most of the cost here would be dependencies and environmental, but the Rollout flag config could add a fraction of a second to this (just the first time that concurrent execution) using the example as above. If this is too much for your use cases, there are a few options:

**Option 1: Load rollout asynchronously**

This is the easiest: remove the “result()” call from the rollout load so it looks like:

```
Rox.setup("..id here..")
```

This will mean that the loading of the flags happens in the background. The downside of this is that when your function is first invoked, it may have the default value of the flag, not what it has been set to remotely: this is not always what you want, but if it works for you – great. This could work if your function does a bunch of things before it gets to checking any flags. If this does not work for you – the next option could.

**Option 2: Use config as code for embedded experiments/flags**

Another interesting capability of CloudBees Rollout is config as code: this means that any time you change the state of flags in an “experiment” the change can be stored back to a source code repository. In **App Settings>Integrations** you setup a connection to a repository:

![GitHub repository](https://rollout.io/blog/wp-content/uploads/sites/2/2019/10/image1-3.png)

The flags will be stored as (YAML) files in your (GitHub) project (the Production Rollout environment will be stored on the master branch, if you have a Staging, it will be stored on the Staging branch, etc).

Every flag change can then trigger a CI/CD pipeline to republish your function along with the new flag settings. CloudBees Rollout calls this [embedded experiments](https://support.rollout.io/docs/embedded-experiments) and there are build tool plugins from available to automatically package up the Rollout config for you so that when you then use your flags are runtime – it looks locally for the embedded config first.

This is a perfect match for Serverless environments if you absolutely must eliminate startup latency, but obviously is a little more work than the other options above.

Signup for a [free CloudBees Rollout trial](https://app.rollout.io/signup?utm_source=devto&utm_medium=referral&utm_content=nov_sponsorship&utm_campaign=rollout_trial_devto).

## Additional resources:

*   [Detailed docs on CloudBees Rollout feature flag update flow behind the scenes](https://support.rollout.io/docs/flags-update-flow)
*   [CloudBees Rollout embedded experiments>](https://support.rollout.io/docs/embedded-experiments)
*   [AWS Lambda execution contexts](https://docs.aws.amazon.com/lambda/latest/dg/running-lambda-code.html)
*   [Solving serverless cold starts](https://dashbird.io/blog/can-we-solve-serverless-cold-starts/)
*   [Why cold starts matter for concurrency and user experience](https://hackernoon.com/im-afraid-you-re-thinking-about-aws-lambda-cold-starts-all-wrong-7d907f278a4f)