Feature flags as a deploy strategy
Deploying code and releasing a feature are two different things. A feature flag is a runtime toggle that lets you ship code to production with the feature turned off, then turn it on when you are ready. If something breaks, you flip the flag back and the incident is over. No rollback, no redeploy, no panic.
interface Flags {
newCheckoutFlow: boolean;
cursorPagination: boolean;
}
const defaults: Flags = {
newCheckoutFlow: process.env.FLAG_NEW_CHECKOUT === 'true',
cursorPagination: process.env.FLAG_CURSOR_PAGINATION !== 'false',
};
let flags: Flags = { ...defaults };
export function isEnabled(name: keyof Flags): boolean {
return flags[name];
}
export function setFlag(name: keyof Flags, value: boolean) {
flags[name] = value;
}A minimal flag store with environment-backed defaults.
import { isEnabled } from '../flags';
import { newCheckout } from '../use-cases/checkout/new-flow';
import { oldCheckout } from '../use-cases/checkout/old-flow';
export async function checkoutHandler(req, res, next) {
try {
const result = isEnabled('newCheckoutFlow')
? await newCheckout(req.body)
: await oldCheckout(req.body);
res.json(result);
} catch (err) {
next(err);
}
}Check the flag at runtime and pick the right code path.
A couple of rules keep flag hygiene from becoming a nightmare. Delete flags when the feature is fully shipped, usually within a sprint or two. And track the flag in your issue tracker so it has a clear owner and a sunset date. Flags that live forever become their own maintenance burden and a source of dead code paths.
Quiz: Quiz
Loading practiceโฆ