Implementing Real-Time Feature Flags in Node.js for Express and Fastify
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
Understanding Blockchain Block Construction and Merkle Roots
Related Content
Tune Spam Detection Per Agent with Nylas Policy Sensitivity Dials
Nylas Agent Accounts let you tune spam detection per workspace via a policy with DNSBL, header anomaly toggles, and a sensitivity dial from 0.1 to 5.0.
RuView Open-Source Project Turns ESP32 Hardware Into a Privacy-First WiFi Radar Using 8KB AI Models
RuView is an open-source project using $9 ESP32 hardware and local AI to monitor breathing, falls, and movement via WiFi signals.
Integrating Mastra Agents with Telex via A2A Requests
A step-by-step guide to integrating Mastra AI agents with Telex using A2A (App-to-App) requests, enabling real-time JSON-RPC communication for structured message exchange.