DEV Community

Atlas Whoff
Atlas Whoff

Posted on

Feature Flags: Ship Code Safely With Runtime Control

Feature Flags: Ship Code Safely With Runtime Control

Feature flags let you deploy code without releasing it. Merge to main, deploy to production, and turn the feature on for 1% of users until you're confident.

The Core Pattern

async function processPayment(order: Order) {
  const useNewProcessor = await flags.isEnabled('new-payment-processor', {
    userId: order.userId,
  });

  return useNewProcessor
    ? newPaymentProcessor.charge(order)
    : legacyPaymentProcessor.charge(order);
}
Enter fullscreen mode Exit fullscreen mode

Simple Redis Implementation

class FeatureFlags {
  async isEnabled(flag: string, context?: { userId?: string }): Promise<boolean> {
    if (context?.userId) {
      const userOverride = await this.redis.get(`flag:${flag}:user:${context.userId}`);
      if (userOverride !== null) return userOverride === '1';
    }
    const global = await this.redis.get(`flag:${flag}`);
    return global === '1';
  }
}
Enter fullscreen mode Exit fullscreen mode

Percentage Rollout

function getUserBucket(userId: string): number {
  let hash = 0;
  for (const char of userId) {
    hash = ((hash << 5) - hash) + char.charCodeAt(0);
    hash |= 0;
  }
  return Math.abs(hash) % 100;
}

// Enable for 5% of users
const enabled = getUserBucket(user.id) < 5;
Enter fullscreen mode Exit fullscreen mode

The Ship Fast Workflow

  1. Merge feature behind a flag (dark launch)
  2. Enable for internal team
  3. Enable for 1% → 10% → 50% → 100%
  4. Remove the flag after full rollout

Feature flags, CI/CD, preview environments, and automated testing are all in the Ship Fast Skill Pack as ready-to-use Claude Code workflows.

Top comments (0)