---
title: 'Building a Simple Feature Flag Service'
description: 'A small flag store with deterministic rollout, local overrides and an audit trail — without adopting a platform.'
pubDate: 2026-04-23
tags: ['Tooling', 'Feature Flags', 'Node', 'Architecture']
canonical: 'https://callmeenji.com/blog/building-a-simple-feature-flag-service'
---

Most teams need three things from feature flags: turn a feature on for one user, roll it out to a percentage, and know who changed what. That fits in a modest service. You do not need a vendor to ship a boolean.

## Store rules, not states

A flag is a list of rules with precedence: explicit user overrides first, then segment rules, then the default. Evaluate in that order and stop at the first match. Keeping the default last means a flag can never silently disappear from a response.

```ts title="evaluate.ts"
export function evaluate(flag: Flag, ctx: Context): boolean {
  for (const rule of flag.rules) {
    if (rule.users?.includes(ctx.userId)) return rule.enabled;
    if (rule.percent && bucket(flag.key, ctx.userId) < rule.percent) return rule.enabled;
  }
  return flag.defaultEnabled;
}
```

## Make rollouts deterministic

Hash the flag key and user id into a stable bucket. Random sampling on every request makes a single user flip between variants, which produces incomprehensible bug reports. Determinism turns a rollout into a reproducible experiment.

## Cache at the edge of the application

Fetch the rule set once per process and refresh on a short interval or a webhook. Evaluating a flag should never add a network hop to a request. Include the rule-set version in logs so a behaviour change can be tied to a specific configuration.

## Write the audit trail yourself

Every change should record who made it, the previous value and a reason. A small append-only table is enough. Six months later, the question is never "what is the flag set to" but "why did we turn it on that day".
