Local flag evaluation in the Node SDK

Evaluate feature flags inside your Node server from downloaded definitions: the key it needs, how definitions refresh, what happens when a fetch fails, and how exposures are recorded.

Embed
Preview of the card
<a href="https://lucidace.com/help/node-local-evaluation"><img src="https://lucidace.com/help/_api/public/orgs/af34862a-c837-4478-8335-0505efe40459/spaces/2f4b888b-c4e6-488b-81bd-6e838fb12d2e/pages/3f7c064a-5465-4686-81a6-c568b728b180/social-card.svg?v=3e7ae33eec8b" alt="Local flag evaluation in the Node SDK" width="600" height="315"></a>

By default every flag read from a server is one POST /v1/flags call. With local evaluation the Node SDK downloads your flag definitions once, keeps them in memory and decides each assignment in-process, so a read costs no network call. It needs @lucida-analytics/node 0.1.1 or later.

Turn it on

Pass a secret key (lc_sk_...) next to the project key. Without one the SDK has no access to definitions and every read is remote.

import { Lucida } from "@lucida-analytics/node";

const lucida = new Lucida("lc_pk_...", {
  host: "https://capture.lucidace.com",
  secretKey: process.env.LUCIDA_SECRET_KEY, // lc_sk_..., server only
});
await lucida.ready; // resolves after the first definitions fetch

const variant = await lucida.getFeatureFlag("onboarding-post-login", "user_123");

personalApiKey is an alias of secretKey. Keep the key on the server; the definitions include targeting rules.

How definitions refresh

  • The SDK fetches GET /v1/flags/definitions with the secret key, sending the last ETag as If-None-Match, so an unchanged set costs a 304.

  • It also opens GET /v1/flags/stream (server-sent events) and reloads on every change, so an edit or a kill switch reaches your servers in seconds. Turn the stream off with streamFlags: false.

  • It polls every featureFlagsPollingIntervalMs (default 30 000 ms), but only while the stream is down.

  • lucida.reloadFeatureFlags() fetches now.

  • Several workers can share one fetch with flagDefinitionCacheProvider (an in-memory, Redis or other key-value store).

What happens on failure

  • A failed refresh keeps the definitions you already have. It is logged only when debug: true.

  • Before the first successful fetch there are no definitions, so reads go to POST /v1/flags.

  • Flags that cannot be decided locally (a cohort filter) also go to POST /v1/flags, unless you pass onlyEvaluateLocally: true on the call.

  • If that call fails too, the SDK serves fallbacks[key] when you set one (reason "fallback", no exposure recorded), else the call's defaultValue, else undefined.

Exposures

getFeatureFlag, isFeatureEnabled and getFeatureFlagDetails record $feature_flag_called and $exposure with locally_evaluated: true, once per distinct id, flag and value in that process (the last 10 000 combinations are remembered, so a restart records them again). Experiment results read these events. Pass sendExposure: false on a call to skip it. getFeatureFlagPayload, getAllFlags, getAllFlagsAndPayloads and evaluateFlags never record one; attach a snapshot to an event with capture({ flags }) instead. A served fallback is never an exposure.

Tests

new Lucida("lc_pk_test", { testMode: { fixture: "flags.json" } }) runs offline against a file written by lucida flags fixture --out flags.json. Events land in lucida.captured.

Updated