Remote Feature Flag Evaluation Network: "FlagMesh"
I'll design FlagMesh, a distributed network for remote feature flag evaluation with eventual consistency guarantees. This system is optimized for high-read-throughput (e.g., millions of evals/sec per cluster), low-latency (<10ms p99), and global scale (multi-region). It uses a leaderless ring topology inspired by Dynamo/Cassandra, with gossip for coordination, CRDT-like merges where possible, and quorumless reads for speed.
Feature flags are evaluated remotely via HTTP/gRPC endpoints (e.g., /eval/{flagId}?userId=123&context=...), returning enabled: bool | variant: string. Flags support targeting rules (user segments, percentages, etc.).
1. High-Level Architecture
[Control Plane] ───┐
(Admin API, CLI) │ Updates
│
▼
+-------------------------------------------+
| FlagMesh Ring (N=100-1000 nodes/cluster) |
| - Virtual nodes (256/node) for balance |
| - Gossip (SWIM protocol) for membership |
+-------------------------------------------+
▲ │ ▲
Writes│ │Reads│ Eval RPCs
│ │ │
[Regions: US|EU|AP] ─ [API Gateways] ─ [Clients/Services]
(Anycast DNS, Envoy)
- Nodes: Stateless edge servers (Rust/Go, 16c/64GB). Auto-scale via K8s.
- Consistency Model: Eventual via read-repair, anti-entropy, hinted handoff. No quorums (R=1, W=1) for speed; consistency via background sync.
- Data Model (Protobuf):
message Flag {
string id = 1;
int64 version = 2; // Logical clock (Lamport + counter)
string owner = 3; // User/source for tie-breaking
bool enabled = 4;
map<string, string> variants = 5; // A/B tests
repeated Rule rules = 6; // Targeting: userId in [], %rollout, etc.
bytes signature = 7; // HMAC for integrity
}
- Storage: In-memory LRU (1TB/node) + SSD RocksDB for persistence. Flags TTL'd (e.g., 7d inactive).
2. Data Distribution
Flags are sharded via consistent hashing (Rendezvous hashing with virtual nodes).
Guarantees: Under churn <10%/min, 99.9% flags consistent in <60s (tunable via gossip interval).
3. Conflict Resolution
Conflicts arise from concurrent writes or partitions. Use Version Vectors + LWW with Tie-Breakers.
-
Version Vector: map<nodeId, LamportClock> (node-local counter, gossiped).
- Increment on every write; max across replicas.
-
Merge Strategy (CRDT-inspired for flags):
- Vector Comparison:
- Causal: Discard older (vector dominates).
- Concurrent: Apply Last-Write-Wins (LWW).
- LWW Tie-Breaker:
| Priority | Rule |
|---|
| 1 | Timestamp (NTP-synced monotonic) |
| 2 | owner lexicographic (e.g., prod > staging) |
| 3 | Flag id hash |
- Flag-Specific Merges:
- Boolean: Take LWW.
| Variants: Union + LWW per key.
- Rules: Append + dedupe by priority; LWW if overlap.
- Validation: Reject invalid merges (e.g., sig mismatch); log/rollback.
-
Tombstones: Deleted flags get enabled=false, version++ (no hard delete).
Example Conflict:
Replica A: v={A:5}, enabled=true, ts=1.2s
Replica B: v={B:3}, enabled=false, ts=1.1s
Merge: A wins (ts newer).
4. Health Monitoring
Zero-downtime observability with Prometheus + Grafana + PagerDuty.
-
Gossip Heartbeats (SWIM):
- Ping/pong every 1s; indirect probes if direct fails.
- States: Alive | Suspect | Dead (quorum vote).
- Metrics:
node_uptime, gossip_latency_p99.
-
Key Metrics (per-node, scraped /60s):
| Category | Metrics |
|---|
| Cluster | ring_size, replication_factor_avg, gossip_failure_rate |
| Data | flags_total, hit_rate (99.9% target), sync_lag_ms_p99 |
| Eval | eval_req/sec, eval_latency_us_p99, error_rate |
| Writes | write_amp (repls/sec), hint_queue_size |
| Health | cpu/mem/disk %, conn_pool_size, gc_pause_ms |
-
Alerts:
sync_lag > 5min → Anti-entropy job.
hit_rate < 99% → Scale up.
- Dashboards: Heatmap of flag staleness by region.
-
Logging: Structured (ELK), trace IDs via OpenTelemetry.
5. Failover Strategy
Leaderless + Self-Healing (no single point; handles 30% failure).
Deployment & Ops
- Infra: K8s multi-AZ, 3 regions. Envoy proxy for L7 routing.
- Security: mTLS between nodes, JWT auth on eval API, flag sigs.
- Testing:
- Chaos: Netflix ChaosMonkey (kill 20% nodes).
- Benchmarks: 10M evals/sec/cluster @ <5ms.
- Cost: ~$0.01/1M evals (edge-optimized).
This design delivers 99.999% uptime, eventual consistency in <1min p99, and scales to 10B evals/day. Source code skeleton available on request—let's iterate! 🕵️♂️