The simplest Feature Flags implementation
🎛️

The simplest Feature Flags implementation

import crypto from "crypto"; import config from "./config"; type Environment = string; type FeatureFlagKey = string; type FeatureFlagValue = boolean | number | string; class FeatureFlagStore { private _flags: Record<FeatureFlagKey, FeatureFlagValue> = {}; constructor(flags?: Record<FeatureFlagKey, FeatureFlagValue>) { this._flags = {}; if (flags) { this._flags = flags; } } get flags(): Record<FeatureFlagKey, FeatureFlagValue> { return this._flags; } get(key: FeatureFlagKey): FeatureFlagValue { return this._flags[key]; } set(key: FeatureFlagKey, value: FeatureFlagValue): void { this._flags[key] = value; } hash(str: string): number { return parseInt(crypto.createHash("md5").update(str).digest("hex"), 16) % 10000; } } const flagsPerEnv: Record<Environment, Record<FeatureFlagKey, FeatureFlagValue>> = { test: { // We may want to use PERMYRIAD unit to fine control the rollout. // PERCENTAGE means 1/100, PERMYRIAD means 1/10000. REDIS_SESSION_STORE_ROLLOUT_PERMYRIAD: 10000, }, local: { REDIS_SESSION_STORE_ROLLOUT_PERMYRIAD: 10000, }, development: { REDIS_SESSION_STORE_ROLLOUT_PERMYRIAD: 10000, }, testing: { REDIS_SESSION_STORE_ROLLOUT_PERMYRIAD: 10000, }, stage: { REDIS_SESSION_STORE_ROLLOUT_PERMYRIAD: 10000, }, production: { REDIS_SESSION_STORE_ROLLOUT_PERMYRIAD: 500, }, }; const featureFlagStore = new FeatureFlagStore(flagsPerEnv[config.environment]); export default featureFlagStore;
 
Separating deployment and feature rollouts can improve team productivity and facilitate effective issue prevention and response. Feature flags are a necessary tool to achieve this goal. Even in small projects, implementing feature flags in a cost-effective manner can provide a sense of peace of mind.
Developers don't just write and deploy code. They need to test and validate the code before deployment, and then maintain and update it afterward. These steps can cause significant stress for developers. In particular, when it comes to feature rollouts, additional testing is required to verify that no features have been omitted or included incorrectly. To address these issues, it is necessary to separate deployment and feature rollouts.
A feature flag is a flag that controls a feature. By using feature flags, specific features can be turned on or off at specific times. For example, when adding a new feature or changing an existing feature, the feature can be made into a feature flag so that developers can turn the feature on or off. This makes it easier to roll out features because all features don't have to be deployed at once. Furthermore, the feature can be turned off immediately if unforeseen issues arise.
Using feature flags allows developers to develop features more freely. Additionally, because the time required to develop a feature is reduced, development productivity increases. Moreover, since feature flags allow for testing of features in advance, issue prevention and response become more manageable.
Feature flags are easy to use even in small projects. To use feature flags, developers should:
  1. Determine what features they want to use flags for.
  1. Implement flags in the code.
  1. Create a system to control the flags.
  1. Turn flags on and off as needed.
In conclusion, feature flags are a necessary tool for separating deployment and feature rollouts. They provide developers with the freedom to develop features more freely while increasing development productivity. Additionally, they make issue prevention and response more manageable, providing a sense of peace of mind for developers.