Web Analytics Made Easy - Statcounter
BaseKV
Sign InSign Up
Back to Articles

Simple Feature Flags Implementation with Key-Value Store

Don't pay for expensive SaaS feature flags. Build your own robust toggle system using a simple persistent key-value store.

BaseKV Team5 min read
feature-flagsdevopstutorial

Feature flags (or feature toggles) are a powerful technique to decouple deployment from release. They allow you to turn features on or off without redeploying your code.

But where should you store these flags?

  • In code / Environment variables: Too hard to change at runtime. Requires a redeploy.
  • In a heavy SQL database: Overkill. Adds latency to every request.
  • In a third-party SaaS: Can be expensive and adds an external dependency for every user interaction.

The ideal solution is a feature flags key-value store.

Why Key-Value Stores are Perfect for Feature Flags

Feature flags are simple key-value pairs. enable_new_ui: true show_promo_banner: false

You need to read them fast (often on every request) and update them instantly (to turn off a buggy feature).

1. Microsecond Latency

A local or fast remote key-value store can return flags in milliseconds. BaseKV, for example, is designed for high-performance reads using the Redis protocol.

2. Persistence is Critical

Unlike a cache, you cannot lose your feature flags. If your store restarts and reverts to defaults, you might accidentally enable a half-finished feature for all users. This is why standard Memcached isn't enough. You need disk-backed persistence.

Implementation Example

Here is how you might implement a simple feature flag check using a Redis-compatible client with BaseKV.

import Redis from 'ioredis';

// Connect to your persistent KV store
const flags = new Redis(process.env.BASEKV_URL);

async function isFeatureEnabled(featureName, userId) {
  // Check specifically for this user first
  const userFlag = await flags.get(`flag:${featureName}:${userId}`);
  if (userFlag !== null) return userFlag === 'true';

  // Fallback to global flag
  const globalFlag = await flags.get(`flag:${featureName}:global`);
  return globalFlag === 'true';
}

// Usage
if (await isFeatureEnabled('dark_mode', 'user_123')) {
  renderDarkMode();
}

Advanced Patterns: Percentage Rollouts

You can also store configuration for percentage rollouts in your key-value store.

flag:new_algorithm:rollout_percentage = 20

Your application code reads this number and hashes the User ID to decide if they fall into the 20% bucket. This keeps the logic in your app but the control in your database.

Cost Considerations

Using a dedicated SaaS for feature flags can cost hundreds of dollars a month at scale. Using a simple, persistent key-value store is often orders of magnitude cheaper. You are just storing a few kilobytes of configuration data.

For a deeper dive into cost-effective architectures, see our guide on Application Configuration.

Conclusion

Don't overcomplicate your feature flag system. You don't need a complex dashboard or a heavy SQL migration. You just need a fast, reliable place to store simple true/false values.

Start controlling your releases with confidence. Get your BaseKV instance and build a robust feature flag system today.