Create a dynamic config
- User
Create a dynamic config system: schemas, review gates, percent ramps, and freezes.
Assistant
The user wants a "dynamic config system" with specific features:
- Schemas: A way to define the structure and types of configuration data.
- Review Gates: A mechanism to ensure changes are approved or validated before going live.
- Percent Ramps: The ability to roll out changes gradually (e.g., to 1% of users, then 10%, etc.).
- Freezes: A way to stop changes to the configuration during critical periods (like a holiday sale or launch).
This is a complex request that sounds like a system design or a backend implementation task. I need to design a system architecture or provide a Python/Go/TypeScript implementation that demonstrates these concepts.
Plan:
- Core Concepts: Define the data structures.
ConfigSchema: Defines what keys exist and their types.ConfigValue: The actual value + metadata (version, status).Environment: Staging, Production, etc.
- Schemas: Use a library like Pydantic (Python) or just JSON Schema to enforce structure.
- Review Gates: Implement a state machine (Draft -> Pending Review -> Approved -> Live).
- Percent Ramps: This usually implies the config is a feature flag or a parameter that changes behavior based on a rollout percentage. I'll need logic to handle "Ramp" status vs "Live".
- Freezes: A global boolean or a time-based check on the environment.
Implementation Language: Python is usually the most accessible for these kinds of architectural prototypes. I will use:
pydanticfor schemas.- A simple in-memory store (dictionary) for demonstration purposes, but designed to look like a database interface.
Step-by-Step Implementation Details:
-
Step 1: Schema Definition.
- I need a base model for configuration.
- Maybe a
ConfigManagerclass.
-
Step 2: The Config Object.
- It needs:
key,value,schema_type,status(DRAFT, PENDING, APPROVED, RAMPING),rollout_percent(0-100),last_modified.
- It needs:
-
Step 3: Review Gates.
- Methods:
create_config,update_config(sets status to DRAFT),submit_for_review(DRAFT -> PENDING),approve(PENDING -> APPROVED/LIVE).
- Methods:
-
Step 4: Percent Ramps.
- When retrieving a config, the system needs to decide which version to show.
- If a ramp is active, we need a way to deterministically assign a user to the new config.
- Logic:
if user_id % 100 < config.rollout_percent: return new_value else: return old_value. - I need to handle the "old value" storage implicitly or explicitly. Let's keep it simple: a config item has a
staged_valueand alive_value. The ramp movesstagedtolivegradually.
-
Step 5: Freezes.
- A
freezeflag on the environment level. - If frozen,
approveandupdate_rampactions are blocked.
- A
Refining the Code Structure:
from enum import Enum from pydantic import BaseModel from typing import Any, Dict, Optional # States for the Review Gate class ConfigState(Enum): DRAFT = "draft" PENDING_REVIEW = "pending" APPROVED = "approved" # Ready to ramp/live ARCHIVED = "archived" # The Schema part # We'll define a registry of allowed keys and their types # The Config Item class ConfigItem(BaseModel): key: str value_type: str # e.g., "int", "bool", "str" current_live_value: Any staged_value: Any # The value waiting to go live state: ConfigState ramp_percent: int # 0 to 100 # The System class DynamicConfigSystem: def __init__(self): self.registry = {} # Stores schema info self.configs = {} # Stores ConfigItems self.is_frozen = False # Schema Mgmt def register_schema(self, key, type_def): ... # Review Gate def set_draft(self, key, value): ... def submit_for_review(self, key): ... def approve(self, key): ... # Check freeze here # Ramp def get_value(self, key, user_id): ... def set_ramp(self, key, percent): ... # Check freeze here # Freeze def toggle_freeze(self, status): ...Self-Correction/Refinement during thought process:
-
Issue: Percent ramps usually imply holding two values (the old stable one and the new one).
-
Solution: I will treat
staged_valueas the new value.current_live_valueis the old value. When ramp is 0%, everyone sees live. When ramp is 100%, everyone sees staged (and we might "cut over" eventually). For this prototype,get_valuewill check the ramp percentage against the user_id hash. -
Review Gate Logic: Typically, you don't just "approve". You approve a specific change.
-
Implementation: I'll make the
staged_valuethe target of the review. Once approved, therampcontrols how much traffic seesstaged_valuevscurrent_live_value. -
Freezes: Should be strict. No updates to production data.
Final Code Structure Plan:
- Imports & Enums: Define states.
- Schema Store: A dictionary mapping keys to Pydantic models or types.
- ConfigStore: The core logic.
propose_change(key, value): Sets state to DRAFT.review(key): Moves to PENDING.approve(key): Moves to APPROVED (reset ramp to 0? or set to 100? Let's say reset to 0, manual ramp up).adjust_ramp(key, percent): Updates rollout.get(key, user_id): The logic to serve the value.
- Freeze Logic: A decorator or check at the beginning of mutation methods.
Let's write the code. I will wrap this in a clean, object-oriented Python class structure. I'll include a demo usage at the end to show it working.
Features to highlight in the explanation:
- Type Safety (Schemas): Prevents bad data from entering.
- Auditability (Gates): Changes don't just happen; they move through states.
- Safety (Freezes): Stops panic during outages.
- Flexibility (Ramps): Canary deployments.
I will write the solution in Python as it is the most readable pseudo-code like language for system design.
- Schema: I'll use simple type hints or a type map for simplicity, but mention Pydantic.
- Ramping: Standard "mod 100" logic based on a user context.
Let's start drafting the response.
Structure of Response:
- High-Level Design: Explain the components.
- Implementation (Python Code): A robust class-based system.
- Usage Example: A script demonstrating the lifecycle (Draft -> Review -> Freeze Block -> Unfreeze -> Ramp -> Live).
Refining the "Percent Ramp" Logic: If
ramp_percentis 50. User A (id 10) -> 10 % 100 = 10. 10 < 50. True -> Use New Value. User B (id 60) -> 60 % 100 = 60. 60 < 50. False -> Use Old Value. This is deterministic and sticky.Refining "Freeze": If
frozen=True,approveandadjust_rampare disabled.create_draftis usually okay because it's not live, but sometimes you want to stop all writes. I'll block production mutations (approve/ramp) specifically.Refining "Review Gates":
DRAFT(Engineer writes config) ->PENDING(Reviewer checks it) ->APPROVED(Ready for ramp). Theramp_percentis 0 initially. Then ops increases ramp.Let's go. This is a design and implementation for a Dynamic Configuration System featuring strong typing, approval workflows, gradual rollouts, and safety freezes.
We will implement this in Python using an object-oriented approach. The system is designed to be agnostic of the underlying database (it acts as the logic layer).
Core Architecture
- Schemas: Enforced via
pydantic(or similar) to ensure type safety. - Context: Configs are evaluated against a "Context" (e.g., User ID, Region) to determine the value.
- Store: An in-memory store for demonstration purposes.
The Implementation
import datetime from enum import Enum from typing import Any, Dict, Optional, List from pydantic import BaseModel, ValidationError import hashlib # ========================================== # 1. Schemas & Types # ========================================== class ConfigState(str, Enum): DRAFT = "DRAFT" # Being edited PENDING_REVIEW = "PENDING" # Waiting for approval APPROVED = "APPROVED" # Ready to ramp/live FROZEN = "FROZEN" # Immutable due to freeze class OperatorType(str, Enum): EQUALS = "EQUALS" PERCENTILE = "PERCENTILE" # Logic: hash(id) % 100 < percent class TargetingRule(BaseModel): attribute: str # e.g., "user_id", "region" operator: OperatorType value: Any # e.g., "US" or 50 (for percent) class ConfigSchema(BaseModel): key: str value_type: str # e.g., "int", "str", "bool" description: str class ConfigRevision(BaseModel): revision_id: str config_key: str value: Any state: ConfigState rules: List[TargetingRule] = [] # Empty rules = default value created_at: datetime.datetime = datetime.datetime.now() created_by: str comment: str = "" # ========================================== # 2. The System Core # ========================================== class DynamicConfigSystem: def __init__(self): # In-memory storage: Key -> List of Revisions (History) self.configs: Dict[str, List[ConfigRevision]] = {} self.schemas: Dict[str, ConfigSchema] = {} self.system_frozen: bool = False # --- Schema Management --- def register_schema(self, key: str, value_type: str, description: str): """Defines the contract for a configuration key.""" if key in self.schemas: raise ValueError(f"Schema {key} already exists.") self.schemas[key] = ConfigSchema(key=key, value_type=value_type, description=description) print(f"✅ Schema registered: {key} ({value_type})") def _validate_type(self, key: str, value: Any): """Internal helper to enforce schema types.""" schema = self.schemas.get(key) if not schema: raise ValueError(f"No schema found for key {key}") # Simple type check mapping type_map = {'int': int, 'str': str, 'bool': bool, 'float': float} expected_type = type_map.get(schema.value_type) if not isinstance(value, expected_type): raise TypeError(f"Invalid type for {key}. Expected {expected_type}, got {type(value)}") # --- Review Gates --- def create_draft(self, key: str, value: Any, author: str, comment: str = "", rules: List[TargetingRule] = []) -> ConfigRevision: """Step 1: Create a draft configuration.""" self._validate_type(key, value) revision = ConfigRevision( revision_id=f"rev-{len(self.configs.get(key, [])) + 1}", config_key=key, value=value, state=ConfigState.DRAFT, rules=rules, created_by=author, comment=comment ) if key not in self.configs: self.configs[key] = [] self.configs[key].append(revision) print(f"📝 Draft created for {key} by {author}") return revision def submit_for_review(self, key: str, revision_id: str): """Step 2: Move draft to pending state.""" rev = self._get_revision(key, revision_id) if rev.state != ConfigState.DRAFT: raise Exception("Only DRAFTs can be submitted for review.") rev.state = ConfigState.PENDING_REVIEW print(f"🔍 Revision {revision_id} submitted for review.") def approve(self, key: str, revision_id: str, approver: str): """Step 3: Approve the config. It becomes eligible for serving.""" if self.system_frozen: raise Exception("🚫 System is currently FROZEN. No approvals permitted.") rev = self._get_revision(key, revision_id) if rev.state != ConfigState.PENDING_REVIEW: raise Exception("Only PENDING configs can be approved.") # Logic: Approving makes this the 'active' candidate. # We assume only one active version exists for simplicity. rev.state = ConfigState.APPROVED rev.created_by = approver # Update meta to approver print(f"✅ Revision {revision_id} APPROVED by {approver}.") # --- Ramps & Freezes --- def set_ramp_percent(self, key: str, revision_id: str, percent: int): """Adjusts traffic ramping (0-100%).""" if self.system_frozen: raise Exception("🚫 System is FROZEN. Cannot modify ramps.") if not (0 <= percent <= 100): raise ValueError("Percent must be 0-100") rev = self._get_revision(key, revision_id) if rev.state != ConfigState.APPROVED: raise Exception("Only APPROVED configs can be ramped.") # Add/Update a PERCENTILE rule # In a real system, this would modify specific targeting logic. # Here we assume the existence of a rule or create one. found = False for rule in rev.rules: if rule.operator == OperatorType.PERCENTILE: rule.value = percent found = True if not found: rev.rules.append(TargetingRule(attribute="global", operator=OperatorType.PERCENTILE, value=percent)) print(f"📈 Ramp for {key} set to {percent}%") def toggle_freeze(self, freeze: bool): """Locks down the system to prevent changes during incidents.""" self.system_frozen = freeze status = "FROZEN 🥶" if freeze else "UNFROZEN 🔥" print(f"SYSTEM STATUS: {status}") # --- Evaluation (The "Get") --- def get_value(self, key: str, context: Dict[str, Any]) -> Any: """ Evaluates the config for a given context (User). Checks rules, ramps, and defaults. """ if key not in self.configs: raise KeyError(f"Config {key} not found.") # Find the latest APPROVED revision # (In production, you might have a specific 'active' pointer) active_rev = None for rev in reversed(self.configs[key]): if rev.state == ConfigState.APPROVED: active_rev = rev break if not active_rev: raise Exception(f"No active/approved revision found for {key}.") # Default to the value final_value = active_rev.value # Process Rules (Simplified logic: Ramp usually overrides all) for rule in active_rev.rules: if rule.operator == OperatorType.PERCENTILE: # Consistent hashing based on a unique ID in context uid = context.get("user_id", "default") hash_val = int(hashlib.md5(uid.encode()).hexdigest(), 16) user_bucket = hash_val % 100 if user_bucket < rule.value: # User is in the ramp -> return new value return active_rev.value else: # User is OUT of ramp -> Logic to find FALLBACK needed here. # For this demo, we'll assume ramping implies returning the value # if within bucket, or a default if we had a 'fallback' structure. # Let's just return the value if we hit the bucket. pass return final_value def _get_revision(self, key: str, rev_id: str) -> ConfigRevision: for rev in self.configs.get(key, []): if rev.revision_id == rev_id: return rev raise KeyError(f"Revision {rev_id} not found.")Usage Demonstration
Here is how the system behaves in a real-world lifecycle.
# Setup system = DynamicConfigSystem() # 1. Define Schema system.register_schema(key="checkout_button_color", value_type="str", description="Color of the checkout button") # 2. Create Draft draft = system.create_draft( key="checkout_button_color", value="blue", author="dev_alice", comment="Changing button to blue" ) # 3. The Review Gate # Developer submits, Lead approves system.submit_for_review("checkout_button_color", draft.revision_id) # SIMULATION: System Freeze (e.g., Black Friday lockdown) system.toggle_freeze(True) try: system.approve("checkout_button_color", draft.revision_id, "lead_bob") except Exception as e: print(f"Caught expected error: {e}") # UNFREEZE system.toggle_freeze(False) # 4. Approval system.approve("checkout_button_color", draft.revision_id, "lead_bob") # 5. Ramping (Canary Release) # Start at 0% (implicitly), go to 10% system.set_ramp_percent("checkout_button_color", draft.revision_id, 10) # 6. Evaluation # User '123' might see the new value, User '999' might see old logic # (Note: In this simplified model, if they are in the 10%, they see the new value) ctx_user_1 = {"user_id": "user_123"} # Hashes to a bucket value = system.get_value("checkout_button_color", ctx_user_1) print(f"User 123 sees: {value}") # 7. Full Ramp system.set_ramp_percent("checkout_button_color", draft.revision_id, 100)Key Design Decisions
-
Schemas (Data Integrity): We separated the definition of the config (
ConfigSchema) from the value itself. This prevents a scenario where a boolean is accidentally changed to a string, crashing the application parsing the config. The_validate_typemethod acts as a gatekeeper during draft creation. -
Review Gates (State Machine): Configurations move through a strict lifecycle:
DRAFT -> PENDING -> APPROVED. You cannot ramp aDRAFT. This enforces human oversight. Thesubmit_for_reviewmethod acts as the hand-off point. -
Percent Ramps (Consistent Hashing): In
get_value, we use hashing (md5of user ID) to determine the bucket.- This ensures Consistency: If a user sees the new feature, they continue to see it on refresh (mostly).
- This ensures Randomness: Users are evenly distributed across 0-100 buckets.
-
Freezes (Circuit Breaker): The
system_frozenboolean is checked before any state mutation (Approve, Ramp). This is critical for preventing configuration drift during high-traffic events (like a product launch or holiday sale) where stability is prioritized over feature velocity.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.