Stack Overflow Feature Flag Architecture: SQL-Backed In-Process Cache
Stack Overflow's feature flag system uses SQL Server storage with a 15-second in-process cache, enabling zero-hop evaluation, percentage rollouts, and emergency kill-switches without external hot-path dependencies.
29 Oct 2025, 18:36 UTC

The Problem: Continuous Deployment Without Coordinated Releases
Stack Overflow deploys to production dozens of times per day. The challenge is not deployment velocity but risk isolation when new code reaches live traffic. A feature flag system must let engineers toggle functionality per user, team, or percentage rollout without redeploying, while providing an emergency kill-switch for misbehaving features. Every change must be audited for accountability.
Requirements Driving the Design
- Deploy isolation: Ship code continuously; activate features independently.
- Granular targeting: Enable for specific users, roles, teams, or IP ranges.
- Percentage rollouts: Ramp traffic gradually (1%, 5%, 25%, 100%).
- Emergency kill-switch: Disable a feature globally in seconds.
- Audit trail: Record who changed a flag, when, and the previous state.
- Zero external dependency on hot path: Flag evaluation must not add a network hop.
Smallest Suitable Design: SQL Server + In-Process Cache
The core is a single SQL Server table (FeatureFlags) storing flag definitions as key/value/rule objects. A lightweight in-process ConcurrentDictionary caches flags, refreshed every ~15 seconds. Evaluation runs via a tiny library call:
// In ASP.NET Core middleware or controller
if (FeatureFlag.IsEnabled("new-search-ui", context)) {
// render new UI
}
Context includes authenticated request data: user ID, roles, team membership, IP address. No external service call occurs during evaluation — the library reads the local cache.
Flag Definition Schema (Simplified)
| Column | Type | Purpose |
|---|---|---|
| Name | nvarchar(128) | Unique key, e.g., "new-search-ui" |
| Enabled | bit | Global on/off |
| RolloutPercentage | tinyint | 0–100 for gradual rollout |
| TargetRules | nvarchar(max) | JSON: user IDs, roles, teams, IP CIDRs |
| IsHighRisk | bit | Requires two-person approval to enable globally |
| UpdatedAt | datetime2 | Last modification timestamp |
| UpdatedBy | nvarchar(64) | Operator identity |
Trust and Data Boundaries
- Flag store: Lives in the same SQL Server cluster as core Stack Overflow data. Same backup schedule, same ACLs, same failover behavior.
- Evaluation boundary: Runs inside the web process. No network hop, no sidecar, no gRPC.
- Context source: Passed to
IsEnabledis already-authenticated request data — no additional trust decision. - Admin writes: Go through a separate audit table (
FeatureFlagAudit) capturing operator, timestamp, before/after state, and approval reference for high-risk flags.
Operational Checks and Observability
A dashboard surfaces:
- Current flag state (on/off, rollout %, targeting rules)
- Last changed by, timestamp, and audit link
- Real-time evaluation counts (enabled vs. disabled per minute)
Alerts trigger on:
- Flag flip affecting >5% of traffic within 60 seconds (catches accidental global enables)
- Evaluation latency exceeding 2 ms p99 (cache pressure or rule complexity)
Deployment pipeline requires a flag rollback plan for every new flag — documented in the PR template.
Failure Modes and Mitigations
| Failure Mode | Behavior | Mitigation |
|---|---|---|
| Stale cache serves old flag state | Up to 15 s inconsistency | Short TTL; explicit cache-bust API called on admin write |
| SQL unavailability | Library fails closed (default off), logs warning | Site stays up; feature simply unavailable |
| Rule evaluation exception | Caught, logged, treated as off | Defensive coding in rule parser; unit tests for targeting logic |
| Accidental global enable | High-risk flags require two-person approval in UI | UI enforces dual-confirmation; audit alert on high-risk change |
Fail-closed assumption: Features are additive (new UI, experiment, beta). Safety-critical flags (e.g., payment gateway fallback) may need fail-open — evaluate per flag.
Conditions That Would Change the Design
- Multi-region active-active with sub-second global consistency: Would require a distributed flag store (etcd, Consul, or DynamoDB global tables) with push invalidation.
- Complex targeting (ML-based, real-time analytics): Would add a sidecar evaluation service (gRPC) to keep web process lightweight and enable richer context.
- Regulatory audit requiring immutable flag history: Would add an append-only event log (Kafka, EventStore) alongside the audit table.
Limitations and Verification Notes
- Description reflects architecture as publicly discussed circa 2019–2023 (Nick Craver blog posts, NDC talks). Internal details may have evolved.
- Cache TTL (15 s) trades consistency for latency. Tighter SLAs need shorter TTL or push invalidation via SignalR/Redis pub-sub.
- SQL-backed store couples flag availability to primary DB. If DB maintenance windows cause flag outages, consider a separate store (Redis, etcd).
- Fail-closed default assumes additive features. Verify per flag whether fail-open is safer.
Practical Verification Steps
- Read Stack Overflow blog: "Feature Flags at Stack Overflow" (Nick Craver, 2019) and "How We Deploy Stack Overflow" (2021).
- Watch Nick Craver's NDC/Conference talks on deployment pipeline (YouTube).
- Inspect open-source FeatureFlag library usage in StackOverflow.Exceptional or similar repos on GitHub.
- Confirm current cache TTL and fail-closed behavior by checking recent source if public, or asking on Stack Overflow Meta.
Concrete Example: Rolling Out a New Search UI
Engineer adds flag new-search-ui with RolloutPercentage = 5, TargetRules empty, IsHighRisk = 0. Dashboard shows 5% of authenticated users see new UI. After 24 hours with no error-rate increase, engineer updates to 25% via admin UI — cache bust fires, all web nodes refresh within 15 s. If error rate spikes, engineer sets Enabled = 0 globally; kill-switch propagates in <15 s. Audit log records both changes with operator ID and timestamp.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.