Function flags or function toggles are settings that management how software program runs and which options are enabled or disabled. Function flags must be configured on the identical host our software program runs on utilizing structured information codecs to make sure that they’re performant, secure and straightforward to handle.
Why Use Function Flags
Through the use of function flags it’s attainable to combine incomplete performance into our software program with out breaking the entire system. Function flags additionally enable us to check in manufacturing environments and roll out performance to small teams of customers at a time. This may be useful when aiming to repeatedly combine and ship new code on a frequent foundation.
Function flags could be so simple as enabling options based mostly on the surroundings, or as complicated as modifying what performance to make use of for various customers at totally different deadlines.
Keep away from Community Requests
After we configure function flags we are able to accomplish that on the host that’s working our software program or some place else over the community. Nevertheless, we must always keep away from making community requests to get our function flags as a result of it may enhance outages, decelerate our methods and make modifications more durable to handle.
If function flags have to be accessed over a community, any time a bunch handles a request it may trigger failures resulting from throttling. This may be frequent for processes working in serverless environments or environments that scale up and down continuously. The issue of throttling could be mitigated by getting the values and caching them domestically or in a knowledge retailer, however doing so can enhance code complexity while additionally introducing extra points associated to caching.
When function flag configuration is accessed through a community name the latency of any request will increase based mostly on the response time of that system. This could trigger lengthy and unpredictable latency in comparison with the consistency of creating a request to the native surroundings.
If function flags are saved externally the values and system that manages the configuration could change individually to the host that wants the flags. This could result in configuration modifications being modified in manufacturing with potential outages if issues are misconfigured. As an alternative it’s higher to vary the function flag configuration with the service and check them earlier than releasing the software program.
Keep away from Atmosphere Variables
Atmosphere variables are sometimes used to cross values to command-line interface software program that wants configuration. This can be a fast and straightforward approach to change settings however we must always keep away from utilizing them to configure function flags.
Atmosphere variables are restricted to to string solely, so any software program that should write or learn surroundings variables must parse the strings into applicable information sorts. This complexity is additional work on each the software program and any tooling that manages the software program, which should not be essential.
Atmosphere variables should not structured and are globally scoped inside a course of, which signifies that any structured keys have to be flattened. For instance, to set the property C of group B inside software program A to the worth 9090 we would want to flatten the surroundings variable as:
A_B_C="9090"
This additional parsing and world scope can result in much more complicated dealing with of the surroundings variables.
It’s onerous to bundle and embrace surroundings variables with software program as they’re loaded in at run time. To set the values we have to run instructions or a script setting the values on the host for the shell that the software program runs in. One choice is to load them from a file by exporting every part in it simply earlier than working the software program. This works, however there’s a higher approach …
The Resolution
We will configure the function flags for our software program by together with a structured information file equivalent to JSON/YAML inside our software program listing as config.json/config.yaml. We will then load this file when our software program begins and parse it with in-built tooling. We will use in-built sorts in addition to structured config for various flags.
config.json
{ "options": { "customer_icon": { "enabled": true }, "billing_alerts": { "release_date": "2022-08-06" }, "date_format": "%Y-%m-%d", "console": { "model": 1.0 } } }
config.yaml
--- options: customer_icon: enabled: true # We will additionally write feedback in yaml billing_alerts: release_date: '2022-08-06' date_format: "%Y-%m-%d" console: model: 1.0 # Must be up to date to 2.0 quickly
We will keep away from community points by storing the function flag configuration on the host working the software program. By together with it as a file on the host we cut back the latency to virtually 0 and keep away from outages attributable to community failures.
We will additionally keep away from the restrictions of surroundings variables by utilizing structured information codecs equivalent to JSON and YAML. This lets us construction our information appropriately and use in-built sorts apart from strings.
To make use of these function flags we simply load them in and swap performance on or off relying on the values. Here’s a Python instance:
import datetime import json # Loading JSON File configuration = json.hundreds(open('./config.json', 'r').learn()) # Date Format date_format = configuration['features']['date_format'] print("Utilizing Date Format:", date_format) # Todays Date date_today = datetime.datetime.in the present day() print("Todays Date:", date_today.strftime(date_format)) # Billing Alerts billing_alerts_release_date = datetime.datetime.strptime( configuration['features']['billing_alerts']['release_date'], date_format ) print( "Billing Alerts Launch Date", billing_alerts_release_date.strftime(date_format) ) if billing_alerts_release_date < date_today: print("Billing Alerts Are Enabled") else: print("Billing Alerts Aren't Enabled But") # Buyer Icon print( "Displaying Buyer Icon", configuration['features']['customer_icon']['enabled'] ) # Console Model print( "Utilizing Console Model", configuration['features']['console']['version'] )
That is how we are able to allow or disable options, launch performance on a specified date, and use in-built sorts to handle our configuration.
Abstract
Structured configuration information are the easiest way to handle function flags with out worrying about community points or parsing every part out and in of surroundings variables. This will also be used to handle any surroundings particular settings inside our system as a central level of management.
There are lots of extra constructive unwanted side effects of configuring software program with structured information that we’ve not coated but. Tell us if this resolution works for you or in case you have any additional questions.
Observe me right here for extra content material or contact me on: