Skip to main content

On This Page

Implementing Real-Time Feature Flags in Node.js for Express and Fastify

3 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

Feature Flags in Node.js: Express and Fastify Guide

Node.js developers can use the Rollgate SDK to manage production releases without redeploying code. The system utilizes Server-Sent Events to apply flag changes across distributed services within approximately 50ms.

Why This Matters

In high-performance Node.js environments, relying on environment variables or database lookups for feature gating introduces deployment bottlenecks and latency. Modern feature flagging systems move logic to local, in-process evaluation, allowing teams to toggle features for specific user segments without the 10-20 minute overhead of a full CI/CD rollback cycle during production incidents.

Key Insights

  • Local evaluation allows thousands of flag checks per request without added network latency, as rules are cached in memory by the Rollgate Node.js SDK.
  • Server-Sent Events (SSE) provide real-time rule updates within 50ms of a dashboard change, as seen in the Rollgate implementation.
  • Deterministic bucketing ensures sticky user experiences during gradual rollouts, preventing UI flickering as traffic percentages scale from 1% to 100%.
  • The Rollgate Node.js SDK includes built-in resilience features like circuit breakers and stale cache fallbacks to maintain availability during API outages.
  • Shutdown management via rollgate.close() is required to flush telemetry and close SSE connections during Kubernetes pod rotations.

Working Examples

Basic Rollgate SDK initialization and flag check.

import { RollgateClient } from '@rollgate/sdk-node';
const rollgate = new RollgateClient({
  apiKey: process.env.ROLLGATE_API_KEY!,
  enableStreaming: true,
});
await rollgate.init();
if (rollgate.isEnabled('new-checkout', false)) {
  console.log('New checkout flow enabled');
} else {
  console.log('Legacy checkout');
}

Express middleware for attaching feature flags to the request context.

app.use((req, res, next) => {
  const userId = req.headers['x-user-id'] as string | undefined;
  req.flags = {
    isEnabled: (key: string, fallback = false) =>
      rollgate.isEnabled(key, fallback, userId ? { userId } : undefined),
  };
  next();
});

Fastify hook for decorating requests with flag evaluation capabilities.

fastify.addHook('onRequest', async (request) => {
  const userId = request.headers['x-user-id'] as string | undefined;
  request.flags = {
    isEnabled: (key: string, fallback = false) =>
      rollgate.isEnabled(key, fallback, userId ? { userId } : undefined),
  };
});

Practical Applications

  • Use case: B2B SaaS platforms enable features for specific ‘enterprise’ plans while excluding free-tier users via attribute targeting. Pitfall: Using environment variables for flags leads to ‘zombie flags’ that require a full redeploy to remove.
  • Use case: E-commerce sites wrap new payment providers in a kill switch to revert to legacy forms instantly if checkout errors spike. Pitfall: Instantiating a new client per request causes connection leaks and high API overhead; one client must be used per process.

References:

Continue reading

Next article

Building Multi-Agent AI Pipelines Across Google ADK and AWS Lambda

Related Content