MongoDB
Designing a Feature Flag System
Build a feature flag system for controlled rollouts — percentage rollouts, user targeting, kill switches, and A/B experiment integration.
S
srikanthtelkalapally888@gmail.com
Designing a Feature Flag System
Feature flags decouple code deployment from feature activation, enabling safe and gradual rollouts.
Types of Feature Flags
Release toggle: Enable/disable in-development features
Kill switch: Emergency off switch for problematic features
Experiment toggle: A/B test variants
Permission toggle: Feature only for specific users/plans
Ops toggle: Runtime behavior tuning
Data Model
{
"flag_key": "new_checkout_flow",
"enabled": true,
"rules": [
{
"type": "percentage",
"percentage": 10
},
{
"type": "user_ids",
"ids": ["user_123", "user_456"]
},
{
"type": "attribute",
"attribute": "plan",
"operator": "equals",
"value": "enterprise"
}
],
"default": false
}
Evaluation Algorithm
def evaluate(flag_key, user):
flag = get_flag(flag_key)
if not flag.enabled:
return flag.default
for rule in flag.rules:
if rule.type == 'user_ids':
if user.id in rule.ids:
return True
elif rule.type == 'percentage':
# Deterministic hash: same user always gets same result
hash_val = hash(f"{flag_key}:{user.id}") % 100
if hash_val < rule.percentage:
return True
return flag.default
Architecture
Flag Config Store (PostgreSQL)
↓
Flag Service API
↓
SDK (in each service) — evaluates locally
↓
Cache (Redis) — configs synced every 30s
↓
Application code
Performance
Flag evaluation must be <1ms — sync flags to in-process cache:
Startup: Load all flags into memory
Refresh: Poll API every 30s for changes
Fallback: Last known config if API unavailable
Experimentation Integration
Flag evaluation event logged:
{ flag: 'new_checkout', variant: true, user_id: 123 }
→ Joined with business metrics (conversion, revenue)
→ Statistical significance calculated
→ Winning variant identified
→ Rollout to 100%
Conclusion
Feature flags are the enabler of continuous deployment. Local evaluation with periodic sync ensures <1ms overhead and resilience to central service failure.