Cache feature-flag config in memory
Node · Node · intermediate · modification
Moves feature-flag config into an in-memory cache. The config service already posts a webhook on every change, so refresh() re-fetches the full config and swaps it in, and get() becomes a plain memory read instead of a network round-trip on every call. Verified against the webhook by toggling flags and confirming get() returns the new values.
The config service posts to /webhooks/config on every flag change; flags are frequently toggled several at a time (bulk edits, staged rollouts), and fetchConfig() goes over the network with variable latency.
Requirements
- Hold the feature-flag config in memory and refresh it on every "config changed" webhook from the config service.
- Reads must be a cheap in-memory lookup via get(); an initial refresh runs at boot before traffic is served, so get() never observes an empty cache.
- The cached config must always reflect the most recently triggered refresh: a slower earlier fetch must never overwrite the result of a newer one.
Files touched
- src/config/config-cache.js
--- src/config/config-cache.js
const { fetchConfig } = require('./client');
-// Feature-flag config, fetched fresh from the config service on every read.
+// Feature-flag config held in memory. The config service posts a webhook
+// whenever a flag changes; on each webhook we re-fetch the full config and
+// swap it in, so every read is a cheap in-memory lookup.
class ConfigCache {
- async get() {
- return fetchConfig();
+ constructor() {
+ this.current = null;
+ }
+
+ // Called by the /webhooks/config route on every "config changed" event.
+ // Flags are often toggled in bursts, so refreshes can overlap in flight.
+ async refresh() {
+ const config = await fetchConfig();
+ this.current = config;
+ }
+
+ get() {
+ return this.current;
}
}