We are going to design a feature flag system with gradual rollouts.
The system will have:
Server and client SDKs.
Config delivery (how the flags are delivered to the clients).
Evaluation caching (to reduce latency and load on the server).
Metrics collection (to monitor flag performance and rollouts).
Rollback paths (ability to quickly disable a feature if something goes wrong).
Let's break down the design:
Server and Client SDKs:
The server SDK will be used by backend services to evaluate flags (if needed) and to report metrics.
The client SDK (for web, mobile, etc.) will evaluate flags on the client side, cache the evaluation, and report metrics.
Config Delivery:
We'll have a central configuration service that holds the flag configurations and their rollout rules.
The server SDK can fetch the configuration from the central service (or we can have a push model?).
The client SDK will typically get the configuration from the server (or from a CDN) and then cache it.
Evaluation Caching:
Both server and client SDKs will cache the flag configuration and the evaluations to avoid repeated calls to the central service.
The cache should have a TTL (time-to-live) and also update when the configuration changes (we can use a streaming mechanism or long polling for updates?).
Metrics:
The SDKs will collect metrics such as:
Flag evaluation count (for each flag, and for each variation)
Errors (e.g., flag not found, network issues)
Custom events (if the flag is associated with a business metric, we might want to track that)
The metrics will be sent back to a metrics collection service (could be aggregated by the central service or a separate time-series database).
Rollback Paths:
The system must allow operators to quickly disable a flag or change the rollout percentage in the central configuration.
The change should propagate to the clients and servers as soon as possible (within the cache TTL or via push updates).
We should also have an emergency kill switch (a way to turn off a flag immediately for all users).
Let's design the central configuration service:
It stores for each flag:
Name
Description
Creation/update timestamps
Rules for evaluation (targeting rules, percentage rollouts, etc.)
Status (enabled/disabled) - for emergency kill switch.
We can have an admin UI to change the flags.
The service must expose an API for:
Fetching the configuration (for server SDKs and for client SDKs via a CDN or directly)
Updating the configuration (for admin UI)
For real-time updates, we can use:
Server-Sent Events (SSE) or WebSockets for server SDKs (if they are long-lived) and for client SDKs (if the client is a web app and we can keep a connection).
Alternatively, the client can poll at short intervals (but that might be heavy).
However, for mobile apps, we might not have a persistent connection. So we can have a combination:
The client SDK fetches the config at startup and then polls every N minutes (or uses a push notification to tell the client to update?).
We can also consider using a CDN to cache the configuration and then have a way to purge the CDN cache when the config changes.
Evaluation Logic:
Each flag has a set of rules. The rules are evaluated in order (or we can have a priority). The first rule that matches determines the variation.
Example rules:
Target specific users (by user ID, email, etc.) -> serve a specific variation.
Percentage rollout: for the remaining users, assign a variation based on a percentage.
The evaluation must be deterministic: same user (same key) should get the same variation in the same rollout period.
We can use a consistent hash (like a hash of the user ID) to assign a bucket (0-99) and then check the rollout percentage.
Example:
code
If we have two variations: A (50%) and B (50%). Then if hash(user_id) % 100 < 50 -> A, else -> B.
But note: if we have multiple rules, we have to break them down:
code
Rule 1: For users with email ending in @example.com, serve variation A.
Rule 2: For users in country US, serve variation B.
Rule 3: For 10% of the remaining users, serve variation C.
So the evaluation:
code
if (user matches rule1) -> A
else if (user matches rule2) -> B
else if (hash(user_id) % 100 < 10) -> C
else -> default (maybe the control variation)
Client SDK:
The client SDK will:
code
- At initialization, fetch the configuration (from the central service or CDN) and store it in memory and in local storage (for persistence across reloads).
- Set up a listener for configuration updates (if using SSE/WebSocket) or start a polling timer.
- When a flag is evaluated for a user:
- Check if the user is already in the cache (with a TTL) for that flag. If yes, return the cached value.
- Otherwise, evaluate the flag using the current configuration and cache the result (with the TTL of the configuration or a separate TTL for evaluation?).
- The evaluation should be deterministic based on the user and the configuration.
The client SDK should also:
code
- Report metrics (asynchronously) to the metrics endpoint.
Server SDK:
Similar to client, but:
code
- The server SDK might not have a persistent user (it might be per request). So we need to pass a user context (or use the request context) for each evaluation.
- The server SDK might fetch the configuration at startup and then poll periodically (or use a streaming update) to keep the configuration fresh.
The server SDK should also:
code
- Cache the configuration and evaluations (maybe in a shared cache like Redis if multiple instances? Or each instance caches its own?).
- Report metrics (for each request that evaluates a flag).
How to handle rollout changes?
When the configuration changes (e.g., the rollout percentage for a flag is increased from 10% to 20%), then:
code
- The new configuration will be delivered to the SDKs (via push or poll).
- The next time the SDK evaluates the flag for a user, it will use the new configuration.
However, note that the evaluation is deterministic by user. So if a user was in the 10% bucket before, they might now be in the 20% bucket? Actually, the bucket is fixed by the hash. So if we change the percentage, the same user might get a different variation because the condition (hash % 100 < new_percentage) might now be true for that user.
But what if we want to make sure that users who were in the old rollout stay in the same variation? That's a more advanced rollout strategy (like canary). In our simple percentage rollout, changing the percentage will change the assignment for users at the boundary.
If we want to keep the old users in the same variation, we need to use a more sophisticated method (like assigning a fixed bucket and then having the rollout percentage decide which buckets are in). But that's beyond the scope of this basic design.
Rollback:
We can have an emergency kill switch that sets the flag status to "disabled". Then the evaluation should return the default variation (or control) for all users, regardless of the rules.
Alternatively, we can set the rollout percentage to 0% for all variations that are not the control.
The kill switch should take effect immediately. So when the SDK receives the new configuration (with the flag disabled), it should stop evaluating the flag and return the disabled variation (or control) for all users.
We can also have a "force enable" for all users (set percentage to 100% for a variation) but that's just a configuration change.
Metrics:
We want to track:
code
- How many times each flag was evaluated (and for each variation).
- How many times the flag was evaluated and resulted in an error (e.g., flag not found, configuration error).
- Custom events: when a user performs an action that is associated with a flag, we might want to track that the user saw the feature and then performed the action.
The SDKs should batch the metrics and send them periodically (or immediately for critical ones) to the metrics service.
The metrics service should be able to aggregate by flag, variation, user attributes, time window, etc.
Scalability:
The central configuration service must be scalable because all SDKs will fetch the configuration from it (or via CDN). We can use a CDN to cache the configuration and only hit the central service when the configuration changes (to update the CDN).
Alternatively, we can have the central service push updates to a message queue and have the SDKs subscribe to the queue? That might be complex for client SDKs.
Another idea: the central service can generate a configuration file (JSON) and store it in object storage (like S3) and then serve it via CDN. When the configuration changes, we update the file and invalidate the CDN cache. Then the SDKs fetch the file from the CDN.
This way, the central service doesn't have to handle the load of all SDKs fetching the configuration.
But then how do we push updates? The SDKs would have to poll the CDN for changes (by checking the file's last modified or having a version number). We can set a short TTL for the CDN cache (like 1 minute) and then the SDKs will fetch at most once per minute.
Alternatively, we can use a hybrid: the SDKs fetch the configuration from the CDN and then use a WebSocket (or SSE) to get updates. The WebSocket server can be fed by the central service (via a message queue) when the configuration changes.
Given the complexity, let's assume:
code
- For server SDKs: we can have a streaming connection (SSE) to the central service (or a dedicated streaming service) that pushes configuration updates. The server SDKs can also fallback to polling.
- For client SDKs: we use a CDN for the initial fetch and then poll every N minutes (or use a push notification to tell the client to update?).
But note: if we use a CDN, the configuration is cached and we need a way to update the CDN when the config changes. We can have the central service update the CDN and then notify the SDKs via a separate channel (like a WebSocket) that the config has changed? That might be overkill.
Alternatively, we can have the SDKs fetch the configuration from the central service (not CDN) and let the central service cache the configuration in memory and serve it with a short TTL (like 30 seconds). Then the SDKs will poll every 30 seconds. This is simple but might put load on the central service.
Given the requirement for gradual rollouts and metrics, we need to design for scale.
Let's design the central service as:
code
- A set of API servers that serve the configuration (with caching in memory and maybe Redis).
- The configuration is stored in a database (like PostgreSQL) and then cached in Redis.
- When the configuration is updated, we update the database and then invalidate the Redis cache (or update it) and also push an update to a message queue.
- The SDKs can either:
Option 1: Poll the API every N seconds (with exponential backoff?).
Option 2: Use a streaming connection (SSE) to a separate streaming service that listens to the message queue and pushes updates.
For client SDKs, we might not want to keep a streaming connection open (battery, etc.). So polling is acceptable.
We can set the polling interval to 60 seconds by default, but allow it to be configurable.
For server SDKs, we can use streaming if the server is long-lived (like a web server) and use polling for short-lived processes.
Alternatively, we can have the same SDK support both: try to use streaming and fallback to polling.
But to keep it simple, let's assume:
code
- All SDKs (server and client) will poll the configuration endpoint every 60 seconds (configurable).
Then the central service must be able to handle the polling load. We can use a CDN in front of the configuration endpoint? But note that the configuration might be different per user? Actually, the configuration is the same for all users (the rules are the same, but the evaluation depends on the user). So the configuration is global.
Therefore, we can cache the configuration in a CDN with a short TTL (like 30 seconds). Then the SDKs will hit the CDN, and the CDN will hit the central service only when the cache expires.
This way, the central service is protected by the CDN.
So:
code
SDK -> CDN (cached for 30s) -> Central Service (only when CDN cache misses)
But note: when the configuration changes, we want the SDKs to get the update as soon as possible. With a 30s TTL, the worst-case delay is 30s.
We can also have a way to purge the CDN cache when the configuration changes (so that the next fetch gets the new config). But then the CDN might not have the new config until we purge? Actually, if we update the origin (central service) and then purge the CDN, then the next fetch will get the new config. But if we don't purge, the CDN will serve the old config until TTL expires.
So we can do:
code
- When the configuration is updated, we update the central service and then issue a cache purge for the configuration file (or endpoint) to the CDN.
Then the next fetch by any SDK will get the new config.
This is a good approach.
So the flow:
code
Admin updates flag -> central service updates DB -> purges CDN cache for config -> SDKs (next fetch) get new config.
The SDKs are polling every 60s, so the maximum delay is 60s (plus CDN fetch time). But with the purge, the next fetch will get the new config immediately.
However, if the SDKs are fetching at different times, some might get the new config earlier (if they fetch after the purge) and some later (if they fetch before the purge and then cache the response?).
But note: the SDKs are not caching the configuration beyond the TTL of the configuration? Actually, the SDKs should cache the configuration in memory and also in local storage (for client) and only re-fetch when the cache expires (or when they get a notification?).
We can have the configuration response include a version number (or last updated timestamp). The SDKs can compare the version they have with the version in the response and only update if the version is newer.
So the SDKs:
code
- Store the configuration and its version (and the time it was fetched).
- When polling, they send the current version (or ETag) and the server can respond with 304 Not Modified if the version hasn't changed.
This way, even if the CDN has the old config (because of TTL) but the version is the same, the SDK will not update.
But if the version changed, then the SDK will update.
Alternatively, we can have the SDKs fetch the configuration without conditions and then compare the version. But using ETag/If-Modified-Since is more efficient.
- At startup, fetch the config (without If-Modified-Since) and store the version.
- Then, every 60 seconds, fetch the config with If-Modified-Since header set to the last modified time of the stored config (or using the version as ETag).
- If the response is 304, do nothing.
- If 200, update the stored config and version, and then re-evaluate any cached evaluations? Or just let the next evaluation use the new config.
But note: the evaluation cache might have stored results based on the old config. So when the config updates, we should invalidate the evaluation cache? Or we can keep the evaluation cache and let it expire?
Since the evaluation is deterministic by the config and the user, if the config changes, the same user might get a different result. So we should invalidate the evaluation cache when the config updates.
Alternatively, we can include the config version in the evaluation cache key. Then when the config changes, the cache key changes and we get a cache miss.
So the evaluation cache key: (flag name, user identifier, config version)
Then when the config version changes, the cache key changes and we re-evaluate.
This is safe.
But note: the evaluation cache might be large. We can set a TTL for the evaluation cache (like 5 minutes) and also invalidate on config change.
However, if we use the config version in the key, then when the config changes, the old cache entries become unreachable (because the key changes). But we don't want to keep the old entries forever? We can have a cleanup job that removes old entries (by config version) or we can use a cache that has a TTL.
Given that the config might change frequently, it's acceptable to let the old cache entries expire naturally.
And set a TTL of, say, 1 hour? But note: if the config changes, we want the new evaluation to happen. So the TTL should be less than the polling interval? Not necessarily.
Actually, the evaluation cache is per user per flag per config version. When the config version changes, the key changes and we get a cache miss. So we don't need to invalidate the old cache, we just stop using it.
But the old cache entries will remain until they expire. That's okay.
Alternatively, we can have the evaluation cache not use the config version and then invalidate the entire cache when the config updates. That might be simpler but less granular.
Since we are already using the config version in the key, we don't need to invalidate the entire cache on config update.
So the plan:
code
- The SDKs poll the configuration endpoint every 60 seconds (configurable) and update the config version if changed.
- The evaluation cache uses (flag_name, user_id, config_version) as key.
- When evaluating a flag, check the cache. If miss, evaluate using the current config and store in cache with TTL (say 1 hour).
But note: if the config changes, the same user might get a different result because the rules changed. And that's what we want.
However, if the config changes and then changes back, the user might get a different result? Because the config version is different. But that's acceptable because the rules are the same? Actually, if the rules are the same, then the evaluation should be the same. But the config version is different, so we will re-evaluate and get the same result. That's okay.
But if the rules are the same but the order of rules changed? Then the result might be different. That's a problem of the configuration, not the cache.
So the design seems sound.
Metrics:
We want to track:
code
- For each flag and variation, how many evaluations resulted in that variation.
- How many times the flag was not found (error).
- Custom events: when a user does something that we want to associate with a flag.
The SDKs should:
code
- When evaluating a flag, record a metric (asynchronously) for the flag and the variation (or error).
- When a custom event occurs, record the event with the current flag evaluations (so we can correlate).
The metrics should be sent in batches to the metrics service to avoid too many requests.
The metrics service:
code
- Receives metrics from SDKs.
- Aggregates them (by flag, variation, time window, user attributes, etc.) and stores in a time-series database (like Prometheus, InfluxDB) or a data warehouse.
The SDKs will batch events and send every N events or every M seconds.
The metrics service should be idempotent (in case of retries) and scalable.
Rollback:
The emergency kill switch sets the flag to "disabled". Then the evaluation should return the default variation (or control) for all users.
In the configuration, we can have:
code
"enabled": false
Then the SDKs, when they get the new config (with enabled=false), will stop evaluating the flag and return the default variation (which we can define as the first variation in the list? or a specific one?).
Alternatively, we can have a rule that says: if the flag is disabled, then return the control variation.
So the evaluation logic:
code
if flag is disabled -> return control variation.
But note: the flag might be disabled in the config, but the SDK might have an old config (cached). So we need to make sure that when the flag is disabled, the SDKs update as soon as possible.
We can set the config polling interval to a low value (like 10 seconds) for critical flags? But that might increase load.
Alternatively, we can have a separate "emergency" channel that uses a WebSocket and is only for emergency updates. But that complicates the design.
Given that we are using CDN with purge, the CDN purge will make the new config available immediately. Then the next poll (which could be up to 60 seconds) will get the new config.
So the worst-case rollback time is 60 seconds (plus the time to purge the CDN and for the SDK to poll). That might be acceptable for non-critical features.
For critical features, we can set the polling interval to 10 seconds.
We can also have the SDKs support a "forced" update: when they get a signal (like a WebSocket message) that a critical flag has changed, they can force an immediate fetch.
But to keep it simple, we'll stick with polling and CDN purge.
Another idea: the SDKs can use a combination of polling and cache invalidation via a version number. If the version number changes, they update immediately (without waiting for the next poll). But that requires a way to notify the SDKs of the version change. We don't have that without a push channel.
So let's stick with polling for now.
We can have the admin UI set the polling interval per flag? Or per SDK? That might be too complex.
Alternatively, we can have the configuration endpoint return a "nextUpdate" time (based on the polling interval) and the SDKs can adjust their polling interval based on the flag's criticality? But that's also complex.
Given the trade-offs, we'll design:
code
- The SDKs have a configurable polling interval (default 60s).
- The central service can set a "emergency" flag that, when set, tells the SDKs to poll more frequently? But that would require the SDK to know about the emergency flag? Not necessarily.
Actually, the emergency flag is just a flag that is disabled. So when the SDKs get the new config (with the flag disabled), they will stop evaluating that flag. So the rollback is effective as soon as the SDK gets the new config.
So the key is to minimize the time to get the new config. We can:
code
- Use a short polling interval for all SDKs (like 10s) but that increases load.
- Use a push mechanism for emergency updates.
Let's consider a hybrid:
code
- The SDKs poll every 60s by default.
- When a flag is updated (especially if it's an emergency disable), the central service can send a push notification (via WebSocket or SSE) to all connected SDKs (server SDKs) and for client SDKs, we can use a push notification service (like Firebase Cloud Messaging) to tell the client to update immediately.
This way, the rollback can be near real-time.
But for client SDKs, we might not have a WebSocket connection (if the app is in the background). So push notifications might be the way.
However, that requires integrating with platform-specific push services and might not be feasible for all clients.
Given the complexity, let's stick with:
code
- Server SDKs: use streaming (SSE) for real-time updates and fallback to polling.
- Client SDKs: use polling with a default of 60s, but allow the admin to set a shorter interval for critical flags? Not per flag, but maybe the SDK can be configured to use a shorter interval for all flags? That's up to the implementation.
Alternatively, we can have the configuration include a "updateFrequency" field that tells the SDK how often to poll? But that would require the SDK to change its polling interval dynamically, which is possible.
So:
code
The configuration response can have:
{
"version": "123",
"flags": [...],
"sdk": {
"pollingInterval": 10 // in seconds, for the next poll
}
}
Then the SDK updates its polling interval accordingly.
This way, when an emergency update is made, the admin can also set the polling interval to 1 second for a short time? But that might be dangerous if many SDKs are polling.
Alternatively, we can have a separate "emergency" endpoint that the SDKs can check (with a shorter TTL) for emergency updates? That's getting too complex.
Let's keep it simple:
code
- The SDKs poll every 60 seconds (configurable at initialization).
- The central service, when updating a flag, can also update the polling interval? But that would require the SDK to change its behavior based on the config, which is a bit of a chicken-egg problem.
Given the time, we'll stick with fixed polling interval (configurable by the SDK user) and rely on CDN purge for fast propagation.
So the rollback time is bounded by the polling interval.
We can also have the admin set the TTL of the CDN cache to a low value (like 5 seconds) for the configuration. Then even if the SDKs poll every 60s, the CDN will fetch from the origin every 5s? No, the CDN cache TTL is for the CDN edge. The SDKs are polling the CDN. So if the CDN has a 5s TTL, then every 5s the CDN will update its cache from the origin. Then when the SDKs poll (every 60s), they might get the old config if the CDN hasn't updated? Actually, the CDN will update its cache every 5s, but the SDKs are polling every 60s. So the worst-case is 60s (if the SDK polls just before the CDN updates, then it gets the old config, and then the next poll is 60s later). But if we purge the CDN, then the CDN will fetch the new config immediately and the next SDK poll (even if it's within 5s) will get the new config.
So with CDN purge, we can achieve near real-time rollback.
Therefore, the emergency rollback procedure:
code
1. Admin disables the flag in the central service.
2. Central service updates the DB and purges the CDN cache for the configuration.
3. The next time any SDK polls (which could be immediately if they are polling at that moment) they will get the new config (because the CDN will fetch from the origin after purge) and then disable the flag.
The worst-case is that an SDK polls just before the purge and then the next poll is 60s later. But if we set the polling interval to 10s for critical flags, then it's 10s.
We can also have the SDKs do a "forced" update when they receive a signal (like a WebSocket message) for emergency. But that's an optimization.
Given the above, let's design the components:
Components:
code
1. Central Configuration Service (with admin UI and API)
2. CDN (in front of the configuration API)
3. Metrics Service (to collect and aggregate metrics)
4. Server SDK (for backend services)
5. Client SDK (for web, mobile, etc.)
Data Flow:
code
- Admin updates flag -> Central Service updates DB -> purges CDN cache.
- SDKs poll CDN for config (with If-Modified-Since) -> CDN returns 304 if unchanged, or 200 with new config.
- SDKs evaluate flags using the config and cache the evaluation (with key including config version).
- SDKs report metrics (batched) to the Metrics Service.
Evaluation Cache:
code
We'll use an in-memory cache (like a LRU cache) with TTL. For server SDKs that are multi-threaded, we need a thread-safe cache. For client SDKs (single-threaded) we can use a simple object.
Metrics Batching:
code
The SDKs will have a buffer for metrics. When the buffer reaches a certain size (e.g., 100 events) or a time interval (e.g., 10 seconds), they will send the metrics to the Metrics Service.
Error Handling:
code
If the SDK fails to fetch the config (network error), it will use the last known config (if available) and log an error.
If the SDK fails to send metrics, it will retry with exponential backoff and eventually discard if too old.
This is a high-level design. Now let's write some pseudo-code for the SDKs.
Pseudo-code for Client SDK:
code
For server SDK, it's similar but:
code
- The user context comes from the request (e.g., request.user).
- The server SDK might be shared across requests (so we need to be careful with caching and user context). We might want to have a per-request instance? Or we can have a global cache that is keyed by (flag, user, configVersion) and then the server SDK's evaluate method takes a user object.
Also, the server SDK might not have a fixed polling interval because the server might be restarted. We can have the server SDK fetch the config at startup and then poll in the background.
Alternatively, we can have the server SDK not cache the config in memory but use a shared cache (like Redis) so that all server instances can share the same config and evaluation cache? That would reduce memory usage and ensure consistency.
But for simplicity, let's assume each server instance caches its own config and evaluation.
Given the complexity, we'll leave the server SDK design similar to the client, but without the user being fixed (we pass the user in each evaluation).
We also need to consider that the server might have many users, so the evaluation cache might grow large. We can use an LRU cache with a size limit.
For the central service:
code
We'll have:
POST /admin/flags -> create/update flag
GET /api/v1/config -> get config (with version)
The config endpoint:
- Check the Redis cache for the config (by version? or by a key "current_config").
- If not in Redis, load from DB and cache in Redis with a TTL (or until invalidated).
- When an admin updates a flag, we update the DB, then update the Redis cache and purge the CDN.
The admin UI will call the POST endpoint.
This is a basic design. We haven't covered:
code
- Security (authentication/authorization for the admin UI and API).
- How to handle multiple environments (dev, staging, prod) - we can have the config endpoint be environment-specific.
- How to target users by attributes (we need a way to define the rules in the admin UI and store them in the DB).
But for the purpose of this exercise, we have a design that covers:
code
- Server and client SDKs.
- Config delivery via CDN and polling.
- Evaluation caching with config version in the key.
- Metrics collection and batching.
- Rollback via emergency disable and CDN purge.
We can improve:
code
- For real-time updates, add a streaming endpoint (SSE) for server SDKs and push notifications for client SDKs (if possible).
- More sophisticated targeting rules (e.g., regex, segment-based).
- A/B testing analysis (using the metrics).
But this is a good starting point.
I'll design a comprehensive feature flag system with gradual rollouts that addresses all your requirements. This is a production-grade architecture that balances flexibility, performance, and safety.
-- Example queries for analytics
SELECT
flag_key,
variation_key,
COUNT(*) as evaluations,
COUNT(DISTINCT user_id) as unique_users,
DATE_TRUNC('hour', timestamp) as hour
FROM flag_evaluations
WHERE timestamp > NOW() - INTERVAL '24 hours'
GROUP BY flag_key, variation_key, hour
ORDER BY hour DESC;
-- Rollout health check
SELECT
flag_id,
current_percentage,
target_percentage,
(current_percentage / target_percentage * 100) as rollout_progress,
error_rate_last_hour
FROM flag_rollout_status
WHERE status = 'active';
class EmergencyDashboard {
async getCriticalFlags() {
return await this.db.query(`
SELECT f.*,
COUNT(CASE WHEN fe.variation != 'control' THEN 1 END) * 100.0 / COUNT(*) as rollout_percentage,
AVG(CASE WHEN fe.error = true THEN 1 ELSE 0 END) as error_rate,
MAX(CASE WHEN fe.crash = true THEN 1 ELSE 0 END) as crash_rate
FROM flag_configs f
LEFT JOIN flag_evaluations fe ON f.id = fe.flag_id
AND fe.timestamp > NOW() - INTERVAL '5 minutes'
WHERE f.status = 'active'
AND f.rollout_percentage > 0
GROUP BY f.id
HAVING error_rate > 0.05 OR crash_rate > 0.01
ORDER BY error_rate DESC;
`);
}
async emergencyDisable(flagId, reason, operator) {
await this.configService.updateFlag(flagId, {
status: 'paused',
rollout: { percentage: 0 }
});
await this.auditLog({
action: 'emergency_disable',
flagId,
reason,
operator,
timestamp: new Date()
});
// Immediate cache purge
await this.cache.purge(flagId);
// Send alerts
await this.sendPagerDutyAlert(flagId, reason);
}
}
9. Implementation Considerations
Cache Invalidation Strategy
Time-based TTL: Config cached for 30s-5min based on environment
Version-based: Use config version in cache keys
Event-driven: Invalidate on config change via pub/sub
Emergency purge: Immediate CDN + cache purge on rollback
Client-side Constraints
javascript
// Bundle size optimization
const SDK_VARIANTS = {
full: 'All features including metrics, real-time updates',
lite: 'Only evaluation, no metrics or real-time',
minimal: 'Just evaluation with in-memory cache'
};
// Offline support
const offlineQueue = new PersistentQueue('ff_metrics');
const fallbackConfig = { /* last known good config */;
Security & Access Control
javascript
// API Gateway Middleware
const checkPermissions = async (req, flagKey, action) => {
const user = req.auth.user;
const flag = await configService.getFlag(flagKey);
// RBAC
if (user.role === 'admin') return true;
// Environment-based access
if (user.environments.includes(flag.environment)) {
// Fine-grained permissions
return user.permissions.includes(`flag:${action}`);
}
return false;
};