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
Mastering AWS Lambda for Real-Time Pipelines: A Technical Deep Dive
Optimize AWS Lambda performance using memory-CPU scaling, VPC integration, and Kinesis stream processing with a 15-minute execution limit.
Navigating the Transition from Systems Programming to Web Development
Kelvin (Drac) outlines his technical progression from C systems programming in 2018 to full-stack web development mastery via The Odin Project in 2022.
Implementing RAG: Solving LLM Hallucinations with Retrieval Augmented Generation
RAG eliminates LLM hallucinations by grounding generation in private knowledge bases using a chunk-embed-retrieve pipeline.