Multi-Version Rollouts
Tags: reliability, releases, software
Published: 2022-03-21 11:51:49

---
title: Multi-Version Rollouts
published: true
date: 2022-03-18 15:15:00 UTC
tags: Reliability,Releases,Software
canonical_url: https://kevincox.ca/2022/03/18/multi-version-rollout/
---

<main><aside><p>This is part of a three part series on rollouts.</p>
<ul>
<li>
<a href="https://kevincox.ca/2022/03/18/multi-version-rollout/">Multi-Version Rollouts</a> (this article)</li>
<li>When to Submit a Change (to be written)</li>
<li>Delinearized Rollouts (to be written)</li>
</ul></aside><hr>
<p>Testing code is hard, and no matter how hard you test it there is no test like shipping it to production. The amount of use, edge cases and stress found in production is beyond what you can imagine in your automated tests. So let’s talk about the best ways to test code in production.</p>
<aside><p><strong>Note</strong>: I’m not saying that you should <strong>only</strong> test your code in production. But simply arguing that you should embrace the value of production testing and accept that your QA process will not catch every bug.</p></aside><h2 id="feature-flags"><a href="https://kevincox.ca/2022/03/18/multi-version-rollout/#feature-flags">Feature Flags</a></h2>
<p>Feature flags are an industry-standard approach to managing the rollout of new features. You add the new code behind a feature flag then can turn on and off the flag to switch between the new and old versions.</p>
<div><pre><code><span>if</span> <span>feature_enabled</span><span>(</span><span>"do new thing"</span><span>):</span>
	<span>new_code</span><span>()</span>
<span>else</span><span>:</span>
	<span>old_code</span><span>()</span>
</code></pre></div>
<p>The idea is simple, but feature flags have some <strong>major</strong> problems.</p>
<h3 id="error-prone-implementation"><a href="https://kevincox.ca/2022/03/18/multi-version-rollout/#error-prone-implementation">Error-prone Implementation</a></h3>
<p>Implementing the flag is usually manual. This means that it is easy to accidentally modify the existing behaviour when adding the new code.</p>
<p>Let’s take an example function:</p>
<div><pre><code><span>def</span> <span>get_user</span><span>(</span><span>cookie</span><span>):</span>
	<span>decoded</span> <span>=</span> <span>jwt</span><span>.</span><span>decode</span><span>(</span><span>cookie</span><span>,</span> <span>key</span><span>=</span><span>SIGNING_KEY</span><span>)</span>

	<span>user</span> <span>=</span> <span>User</span><span>.</span><span>get</span><span>(</span><span>decoded</span><span>.</span><span>data</span><span>.</span><span>user_id</span><span>)</span>

	<span>if</span> <span>decoded</span><span>.</span><span>meta</span><span>.</span><span>issued</span> <span>&lt;</span> <span>user</span><span>.</span><span>tokens_revoked</span><span>:</span>
		<span>clear_login_cookie</span><span>()</span>
		<span>return</span> <span>None</span>

	<span>return</span> <span>user</span>
</code></pre></div>
<p>And add a new feature behind a feature-flag:</p>
<div><pre><code><span>def</span> <span>get_user</span><span>(</span><span>cookie</span><span>):</span>
	<span>decoded</span> <span>=</span> <span>jwt</span><span>.</span><span>decode</span><span>(</span><span>cookie</span><span>,</span> <span>key</span><span>=</span><span>SIGNING_KEY</span><span>)</span>

	<span>if</span> <span>feature_enabled</span><span>(</span><span>"revocation cache"</span><span>):</span>
		<span>tokens_revoked</span> <span>=</span> <span>cache_get</span><span>(</span><span>decoded</span><span>.</span><span>data</span><span>.</span><span>user_id</span><span>)</span>
	<span>else</span><span>:</span>
		<span>user</span> <span>=</span> <span>User</span><span>.</span><span>get</span><span>(</span><span>decoded</span><span>.</span><span>data</span><span>.</span><span>user_id</span><span>)</span>
		<span>tokens_revoked</span> <span>=</span> <span>maybe_user</span><span>.</span><span>tokens_revoked</span>

	<span>if</span> <span>decoded</span><span>.</span><span>meta</span><span>.</span><span>issued</span> <span>&lt;</span> <span>tokens_revoked</span><span>:</span>
		<span>clear_login_cookie</span><span>()</span>
		<span>return</span> <span>None</span>

	<span>return</span> <span>User</span><span>.</span><span>get</span><span>(</span><span>decoded</span><span>.</span><span>data</span><span>.</span><span>user_id</span><span>)</span>
</code></pre></div>
<p>Did you see the bug? The new version fetches <code>User</code> from the database twice for a valid token. If this code runs for every request it could easily overload the database. The author wanted to share the cookie-clearing code and was thinking about what the code would look like after the flag was removed. But they failed to thoroughly consider the performance of the old path. Other common errors include failing to initialize variables on some paths or forgetting to properly clear a variable between uses on one path.</p>
<p>Often it is best to just duplicate all affected code. It is pretty simple in this self-contained example but can be a huge mess when the change affects multiple functions. This is also a problem for review as it can be hard to tell what “new” lines are actually new vs the lines that are just copies of the previous code with slightly different interfaces or other changes.</p>
<h3 id="implicit-dependencies"><a href="https://kevincox.ca/2022/03/18/multi-version-rollout/#implicit-dependencies">Implicit Dependencies</a></h3>
<p>Often a feature flag will implicitly depend on another feature flag being enabled (or disabled). This can result in issues if you try to roll back the “base” flag not knowing about the dependant flag.</p>
<h3 id="difficult-to-test"><a href="https://kevincox.ca/2022/03/18/multi-version-rollout/#difficult-to-test">Difficult to Test</a></h3>
<p>Having multiple implementations of some features makes it difficult to run tests. If you test with the feature flag disabled you are shipping untested code to production but if you test with the flag enabled you aren’t testing the fallback.</p>
<p>You might want to test both versions but doing this manually is tedious and error-prone. But testing each configuration becomes expensive if you have 100 feature flags (especially if some of them interact).</p>
<p>Feature flags are 90% testing in prod. You use a feature flag to sneak the code past the tests then slowly ramp it up in prod while squinting at your monitoring carefully.</p>
<h2 id="multi-version-testing"><a href="https://kevincox.ca/2022/03/18/multi-version-rollout/#multi-version-testing">Multi-version Testing</a></h2>
<p>What if we just used regular releases to test features?</p>
<p>Instead of releasing once a week with feature-flags in place we just release once a day (or once an hour). However instead of a quick 0% → 10% → 100% rollout we extend the lifetime. For example imagine that every day we release a new version to a fraction of traffic and remove the oldest version.</p>
<p><img src="https://cloudflare-ipfs.com/ipfs/QmXy2YtkgybUNVaSkW4vBa5SXDCw7mGCcocYdE5AQuqxGY/linear.png" alt="visualization of a linear rollout" style="-ms-interpolation-mode:nearest-neighbor;image-rendering:-moz-crisp-edges;image-rendering:pixelated" crossorigin=""></p>
<p>This is a simple linear rollout scheme, but other schemes can start slower and accelerate once confidence is built.</p>
<p><img src="https://cloudflare-ipfs.com/ipfs/QmXwySgJjwLDcDcfZUEXXq35uz5NpQTqbkPnU7NceXdjDA/exponential.png" alt="visualization of an exponential rollout" style="-ms-interpolation-mode:nearest-neighbor;image-rendering:-moz-crisp-edges;image-rendering:pixelated" crossorigin=""></p>
<p>Or you can combine exponential with a holdback so that you have at least 7 days of history running. This can help identify when a problem was introduced.</p>
<p><img src="https://cloudflare-ipfs.com/ipfs/QmYyExgTnSdzz58TT8W8a5QAgpmEHRQaq6H6hjesHUeXh2/exp-with-holdback.png" alt="visualization of an exponential rollout with holdback" style="-ms-interpolation-mode:nearest-neighbor;image-rendering:-moz-crisp-edges;image-rendering:pixelated" crossorigin=""></p>
<h3 id="benefits"><a href="https://kevincox.ca/2022/03/18/multi-version-rollout/#benefits">Benefits</a></h3>
<h4 id="version-skew-testing"><a href="https://kevincox.ca/2022/03/18/multi-version-rollout/#version-skew-testing">Version Skew Testing</a></h4>
<p>Version skew tolerance is effectively mandatory. Since your change will always be running next to older code for a while it needs to be compatible otherwise it will be rejected.</p>
<p>People often try to skirt this issue which is fundamental to distributed systems (which any website is) and it can go largely unnoticed until a major incident occurs and rollbacks only cause more failures. Running a spread of versions at the same time thoroughly tests this scenario so that it can be caught and resolved before disaster strikes.</p>
<h4 id="reliable-feature-control"><a href="https://kevincox.ca/2022/03/18/multi-version-rollout/#reliable-feature-control">Reliable Feature Control</a></h4>
<p>Unlike feature-flagging every change is automatically tested with a slow rollout. There is no need to add noise to code reviews with conditionals, flag names and duplicating code. Even large type-level refactors that touch most of the codebase can be tested.</p>
<h4 id="fast-time-to-production"><a href="https://kevincox.ca/2022/03/18/multi-version-rollout/#fast-time-to-production">Fast time-to-production</a></h4>
<p>Time-to-production is an important metric for developer productivity. Even 1% of production traffic exposure is enough to start collecting some basic metrics, logs and other information to find out how a feature is working. With support for routing select users to specific versions they can fully test their new code or even enable it for a customer which is having issues.</p>
<p>By limiting the traffic the risk of production deploy is reduced, so shipping quick is acceptable.</p>
<h4 id="full-testing"><a href="https://kevincox.ca/2022/03/18/multi-version-rollout/#full-testing">Full Testing</a></h4>
<p>Because each version only contains one implementation of each feature you can simply run integration tests and get good coverage.</p>
<h3 id="drawbacks"><a href="https://kevincox.ca/2022/03/18/multi-version-rollout/#drawbacks">Drawbacks</a></h3>
<h4 id="long-time-to-dead"><a href="https://kevincox.ca/2022/03/18/multi-version-rollout/#long-time-to-dead">Long time-to-dead</a></h4>
<p>The flip side is that because rollouts are slow old code will run for longer. This means that cleaning up old data, database schemas or otherwise will have to wait until the last version using them is completely obsolete (not running and not valuable as a possible rollback target).</p>
<h4 id="difficulty-identifying-bad-versions"><a href="https://kevincox.ca/2022/03/18/multi-version-rollout/#difficulty-identifying-bad-versions">Difficulty Identifying Bad Versions</a></h4>
<p>Almost all of your monitoring needs to support easy breakdown by version. This is because when you start seeing an error you will want to see what versions are affected. If the problem only appears in some versions you can resolve the issue by rolling everything to a working version.</p>
<p>There are also upsides to the more complex monitoring. Incident response times can be significantly improved because it is easier to identify the last good version. Especially if you have a holdback you can just see which of the holdback versions have the issue. This makes it quickly identify if a rollback will help and which version need to be rolled back to. This also makes it quicker to identify the actual commit that caused the issues as you only need to look at the changes in a small release instead of a large release.</p></main>