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

Files touched

--- 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;
   }
 }

Review this PR

Node practice