Feature Toggle Introduction. | Better Programming

Picture by Arthur Mazi on Unsplash

I’ve been utilizing trunk-based development for a while to enhance productiveness within the group I supervise. With a view to carry out trunk-based growth easily, characteristic toggle performs an vital position in it, so I take advantage of this text to introduce characteristic toggles, what sorts of toggles are included and the right way to use them.

However earlier than we get to that, let’s speak about an idea that’s simply confused. What’s the distinction between characteristic toggle and configuration?

First, wherever a configuration is saved, equivalent to a file, or a central storage system, equivalent to Consul by HashiCorp, it’s thought of static. When the system is initialized, all the configuration file is saved in every occasion’s reminiscence to scale back pointless I/O overhead. That’s, if a setting is to be modified, the related occasion have to be restarted with the intention to reload the setting into reminiscence.

Alternatively, a characteristic toggle doesn’t work this fashion; a characteristic toggle calls the administration system every time to get the present settings for the occasion and even the characteristic. Subsequently, the characteristic toggle is extra dynamic than the configuration.

Let’s use code for instance.

If it’s a configuration file, it’s going to appear to be the next.

const config = loadConfigFromSomewhere();
perform foo()
if (config.featureA) doA();
else doB();

As for the characteristic toggle, it’s a little totally different.

const connection = initFeatureToggle();
perform bar()
if (connection.isEnabled("featureA")) doA();
else doB();

As you may see from the above instance, the configuration is mounted after loadConfigFromSomewhere, after which the worth is simply taken from the variable. Nevertheless, the characteristic toggle might get a unique end result every time when calling isEnabled, relying on the present setting.


There are 4 sorts of characteristic toggles.

  1. Launch Toggles
  2. Ops Toggles
  3. Experiment Toggles
  4. Permission Toggles

There are two dimensions within the diagram, dynamism and longevity.

  • Dynamism mentioned that the frequency of the toggle can be modified, the nearer to the best facet means the extra continuously the toggle can be modified.
  • Longevity refers to how lengthy the toggle will keep within the supply code, the nearer to the underside the shorter the retention time.

The discharge toggle within the blue space is comparatively static and is barely used when a characteristic is launched, and can be eliminated when the characteristic is secure. Alternatively, the toggles within the inexperienced space are comparatively dynamic and will change in keeping with varied wants.

Launch toggle is essentially the most generally used type of toggle and is meant to regulate the impression scope of every launch.

Assuming there’s a new requirement for this launch, the code for that new requirement must be absolutely encapsulated by the toggle for this launch. In the meanwhile of launch, the default toggle must be off, in different phrases, the conduct of this launch and the final launch must be precisely the identical.

When the discharge is profitable, the toggle might be turned on progressively on demand. At first, it might be 1% of the calls will get turned on, then 5%, then 10% and so forth. On this means, the performance is progressively opened to 100%. Such an method is also referred to as the canary launch.

Alternatively, the toggle is turned on all the way in which in the beginning. As soon as any downside is encountered, the toggle is turned off fully to keep away from a catastrophe affecting all the system. The follow is named blue-green deployment.

It is very important observe that the life cycle of those launch toggles must be only a brief time frame. These toggles must be faraway from the code as quickly as it’s decided that the performance is secure. In the event you don’t do that, the code can be full of increasingly more toggles, which in return will trigger a upkeep effort.

Ops toggles are totally different from launch toggles as a result of launch toggles are designed to react to every launch, however ops toggles are designed to deal with adjustments to the infrastructure. When the infrastructure needs to be upgraded or migrated for some objective, it may be managed by ops toggles.

For instance, if a system begins with a distributed monitoring system known as Elastic APM and desires to interchange it with jaeger for price range or upkeep causes, then an ops toggle can be utilized to change between the 2 techniques. Till we’re certain that jaeger might be operated appropriately, the 2 techniques will coexist, presumably with a half-monitoring ratio of 50-50. The ops toggle will stay in place till we’re assured that we are able to substitute the unique system with jaeger, so the period can be for much longer than the discharge toggle.

One other instance of utilizing ops toggle is a handbook circuit breaker. For a excessive throughput system, a price restrict algorithm is often carried out, however as soon as the site visitors reaches a sure stage, it’s essential to instantly lower off the surplus site visitors to keep away from impacting the entire system. It’s splendid for the system to have the ability to regulate and get well itself, nonetheless, such a mechanism is very advanced and troublesome to do effectively in the beginning, so ops toggle is an effective selection.

This toggle will even exist for a very long time till the developer has a method to implement an automated mechanism.

Experiment toggles, because the title suggests, are toggles which might be used to carry out experiments. When a characteristic has two totally different behaviors and we need to consider the effectiveness of those two behaviors, we use experiment toggles, and the method of experimentation is known as A/B testing.

It operates a bit like a launch toggle, however in contrast to a launch toggle that enables outdated and new behaviors to coexist, an experiment toggle permits two new behaviors to coexist. It could even be potential to usher in extra experimental parameters by experiment toggles to make the entire experimental course of extra versatile and environment friendly.

Sure, a lot of the characteristic toggles can carry further parameters, not simply true or false. Subsequently, this could play essentially the most accelerating position for the always altering experimental setting.

Identical to different toggles, the experiment toggles must be faraway from the code after the experiment is completed and the outcomes are confirmed.

This final one is essentially the most advanced relevant case. From the diagram, we are able to see that it’s each dynamic and long-lived.

Then, what precisely is the existence of this toggle?

From my viewpoint, it has two eventualities, one is the system-wide entry management and one other is the product stage entry management.

System-wide entry management signifies that there are specific capabilities or operations which might be solely out there to particular customers, thus we handle these restricted capabilities by permissions toggles. Somewhat exhausting to think about? I’ll present a pseudo code.

perform restrictedFunction() 
const metadata = userId, userLevel, userRole;
if (connection.isEabled("featureA", metadata))
else return;

From the above instance, we are able to know that this particular perform is barely allowed for sure customers, and the consumer should confirm his Id, Stage and Function to verify he meets the eligibility standards earlier than the perform is offered, in any other case, will probably be skipped.

Such a toggle appears to be very secure, proper? Actually, it’s not, the verification methodology might change, initially solely Id and Stage possibly required, till in the future resulting from demand and add Function. So the foundations of the toggle might change with necessities, and the utilization context of the toggle can also change as necessities increase.

We’ve already seen what the code appears like when utilizing the characteristic toggle.

const connection = initFeatureToggle();
perform bar()
if (connection.isEnabled("featureA")) doA();
else doB();

When increasingly more toggles are made, there can be increasingly more if-else in between, which really violates a lot of the clear code ideas. Subsequently, you have to be additional cautious when utilizing characteristic toggle and comply with a couple of fundamental ideas.

  1. Use solely the place wanted.
  2. management the variety of toggles.
  3. Toggles don’t overlap with one another.
  4. Often clear the ineffective toggles.

As well as, the code of utilizing toggles will ultimately turn into as follows.

perform bar() 

Due to this, it is extremely vital to have the ability to take away toggles simply. I like to recommend utilizing the Manufacturing unit Technique and encapsulating the code with a characteristic toggle in order that solely the product contained in the Manufacturing unit Technique is eliminated when eradicating toggles and never all of the exterior callers are touched. Right here is an easy demonstration.

perform manufacturing facility()

if ( connection.isEnabled("featureA", metadata) )
return new NewHandler();
return new OrigHandler();

// qwer.js
manufacturing facility().doA();
// asdf.js
manufacturing facility().doB();
// zxcv.js
manufacturing facility().doC();

Wrapping the entire characteristic toggle judgment within the manufacturing facility methodology, the exterior caller doesn’t must know whether or not he will get a brand new or outdated handler. when eradicating the characteristic toggle sooner or later, we solely wants to change the manufacturing facility methodology and drop the OrigHandler.

perform manufacturing facility()

return new NewHandler();

Consequently, whether or not it’s qwer.js, asdf.js or zxcv.js don’t want to vary.

This time we talked concerning the many eventualities the place characteristic toggles can be utilized and the right way to deal with them correctly, and it is best to know why trunk-based growth wants characteristic toggles a lot as a result of new variations are launched continuously and to keep away from affecting the web setting. So all unstable modifications will need to have a mechanism to isolate them. Characteristic toggles are precisely such a task.

One other generally talked about use case in utilizing characteristic toggles is that after a toggle is modified, it should instantly react to some on-line performance. Such a requirement might be achieved by Observer Sample, however I don’t advocate this sort of conjunction. In spite of everything, we all know that these toggles will ultimately be eliminated they usually shouldn’t be concerned in the entire area mannequin, as a substitute, they play the position of coordinator, coordinating between folks and techniques.

On this article, we now have solely mentioned the usage of characteristic toggles and their concerns, with out mentioning particular options. Subsequent time I’ll introduce two well-known suppliers of characteristic toggle, LaunchDarkly and Unleash, and supply some feedback and tips primarily based on my expertise with them respectively.

More Posts