<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Michael Neale</title>
    <description>The latest articles on DEV Community by Michael Neale (@michaelneale).</description>
    <link>https://dev.to/michaelneale</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F272156%2Fd13c1f45-c826-4f94-8d8a-69701fffe033.png</url>
      <title>DEV Community: Michael Neale</title>
      <link>https://dev.to/michaelneale</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/michaelneale"/>
    <language>en</language>
    <item>
      <title>Serverless Apps and CloudBees Rollout Feature Flags</title>
      <dc:creator>Michael Neale</dc:creator>
      <pubDate>Mon, 18 Nov 2019 18:20:36 +0000</pubDate>
      <link>https://dev.to/cloudbees/serverless-apps-and-cloudbees-rollout-feature-flags-5om</link>
      <guid>https://dev.to/cloudbees/serverless-apps-and-cloudbees-rollout-feature-flags-5om</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  An example
&lt;/h2&gt;

&lt;p&gt;Cloning &lt;a href="https://github.com/michaelneale/rollout.io-serverless" rel="noopener noreferrer"&gt;&lt;em&gt;this&lt;/em&gt;&lt;/a&gt; example from GitHub, you can deploy it as easily as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud functions deploy my_function --trigger-http --runtime "python37"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 (&lt;a href="https://support.rollout.io/docs/initial-setup" rel="noopener noreferrer"&gt;CloudBees&lt;/a&gt; &lt;a href="https://support.rollout.io/docs/initial-setup" rel="noopener noreferrer"&gt;Rollout quickstart here&lt;/a&gt;). It’s very simple in this case with Google Cloud functions, but it will work elsewhere too.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

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

&lt;p&gt;Take a look a bit closer and this sample function at where you setup CloudBees Rollout vs where you use flags:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frollout.io%2Fblog%2Fwp-content%2Fuploads%2Fsites%2F2%2F2019%2F10%2Fimage2-4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frollout.io%2Fblog%2Fwp-content%2Fuploads%2Fsites%2F2%2F2019%2F10%2Fimage2-4.png" alt="rollout feature flags" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/running-lambda-code.html" rel="noopener noreferrer"&gt;execution context&lt;/a&gt; – 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).&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  CloudBees Rollout flag config loads fast
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The CloudBees Rollout docs have a fairly &lt;a href="https://support.rollout.io/docs/flags-update-flow" rel="noopener noreferrer"&gt;comprehensive guide&lt;/a&gt; on the “update flow” for updating flags on the client:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frollout.io%2Fblog%2Fwp-content%2Fuploads%2Fsites%2F2%2F2019%2F10%2Fimage3-3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frollout.io%2Fblog%2Fwp-content%2Fuploads%2Fsites%2F2%2F2019%2F10%2Fimage3-3.png" alt="updating feature flags" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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!).&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  On cold starts and getting fastest load times
&lt;/h2&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 1: Load rollout asynchronously&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the easiest: remove the “result()” call from the rollout load so it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rox.setup("..id here..")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 2: Use config as code for embedded experiments/flags&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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 &lt;strong&gt;App Settings&amp;gt;Integrations&lt;/strong&gt; you setup a connection to a repository:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frollout.io%2Fblog%2Fwp-content%2Fuploads%2Fsites%2F2%2F2019%2F10%2Fimage1-3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frollout.io%2Fblog%2Fwp-content%2Fuploads%2Fsites%2F2%2F2019%2F10%2Fimage1-3.png" alt="GitHub repository" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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).&lt;/p&gt;

&lt;p&gt;Every flag change can then trigger a CI/CD pipeline to republish your function along with the new flag settings. CloudBees Rollout calls this &lt;a href="https://support.rollout.io/docs/embedded-experiments" rel="noopener noreferrer"&gt;embedded experiments&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Signup for a &lt;a href="https://app.rollout.io/signup?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_content=nov_sponsorship&amp;amp;utm_campaign=rollout_trial_devto" rel="noopener noreferrer"&gt;free CloudBees Rollout trial&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional resources:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://support.rollout.io/docs/flags-update-flow" rel="noopener noreferrer"&gt;Detailed docs on CloudBees Rollout feature flag update flow behind the scenes&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://support.rollout.io/docs/embedded-experiments" rel="noopener noreferrer"&gt;CloudBees Rollout embedded experiments&amp;gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/running-lambda-code.html" rel="noopener noreferrer"&gt;AWS Lambda execution contexts&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://dashbird.io/blog/can-we-solve-serverless-cold-starts/" rel="noopener noreferrer"&gt;Solving serverless cold starts&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://hackernoon.com/im-afraid-you-re-thinking-about-aws-lambda-cold-starts-all-wrong-7d907f278a4f" rel="noopener noreferrer"&gt;Why cold starts matter for concurrency and user experience&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>monitoring</category>
      <category>serverless</category>
      <category>programming</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
