← Back to SQL, page 20

sql-0397 · Medium mid

Design storage for feature flags that can target specific users, tenants or a percentage of traffic.

A flags table holds the key, default state and rollout percentage; a flag_rules table holds targeting rules such as tenant or user lists, evaluated in priority order. Percentage rollout hashes the user id with the flag key so each user gets a stable result. Applications cache flags in memory and refresh on change.

Full answer
text
flags(key PK, description, enabled_default BOOLEAN, rollout_percent INT CHECK BETWEEN 0 AND 100,
      updated_at, updated_by)
flag_rules(id, flag_key FK, priority, attribute, operator, value, result BOOLEAN)
flag_audit(id, flag_key, change jsonb, changed_by, changed_at)

Evaluation for a user:

  1. Apply rules by ascending priority (for example tenant_id IN (7, 9) -> true, country = 'IN' -> false); the first match wins.
  2. Otherwise, if hash(flag_key || user_id) % 100 < rollout_percent, enable.
  3. Otherwise use the default.

Hashing with the flag key gives independent rollouts per flag, and the same user stays in or out as the percentage grows. Audit every change, because flags change production behaviour without a deploy. Remove flags after full rollout so stale rules do not pile up.

Key points

  • Flags with defaults and rollout percentages
  • Ordered targeting rules
  • Stable hashing for percentage rollout
  • Cache in applications
  • Audit and retire flags

Common mistakes

  • Random per-request rollout, which flickers
  • Never deleting finished flags

Follow-ups

  • How would you run an A/B test with flags?
  • How would changes reach all servers quickly?

Tips

  • Explain stable bucketing