Skip to content

Local evaluation (server-side SDKs, beta)

By default every flag evaluation is a request to the API. Local evaluation lets a server-side process download the flag ruleset once, refresh it in the background, and answer flags in-process with no request per evaluation. It is opt-in, and it is available in the JavaScript SDK (@controlvar/js-sdk 0.3.0, evaluation: 'local') and the Python SDK (controlvar 0.3.0, evaluation="local"). Every other SDK evaluates on the server.

What stays on the server in every mode: experiments (assignment is sticky and recorded by the server, so getAssignment / get_assignment and getVariant / get_variant are always requests) and every flag the ruleset marks as server-evaluated (below).

Server-side code only

The ruleset contains every flag's targeting rules, including the values written in them (user ids, e-mail domains, plan names), and the values string, number and JSON flags serve. The key that downloads it needs the sdk:ruleset scope; keep that key on a server. The JavaScript SDK refuses evaluation: 'local' in a browser.

The promise

A local answer is the answer GET /api/v1/feature-flags/evaluate/{key} would give for the ruleset version the process holds: the same enabled and the same reason (targeting_rule, rollout or inactive). When the SDK cannot be sure of that, it does not guess: it makes the same request it makes in server mode, and the result carries source server.

That promise is checked against tests/sdk-contract/ruleset-vectors.json, a corpus of 2406 cases generated by running the server's own evaluator. Each SDK must answer the 1337 cases marked must_local locally with exactly the server's answer, and send every other case to the server.

Requirements

If you set EXPERIMENTLY_SERVER_KEY or EXPERIMENTLY_API_URL from an earlier version of these pages, set CONTROLVAR_SERVER_KEY and CONTROLVAR_API_URL instead.

  • An Controlvar server that serves GET /api/v1/sdk/ruleset and accepts POST /api/v1/tracking/evaluations (both are beta). A server without the ruleset answers 404, and the SDK then evaluates on the server (see Freshness).
  • An API key with the sdk:ruleset scope, created by a user who can change feature flags (ADMIN, DEVELOPER, a superuser, or in the full profile anyone whose custom role or direct grant gives feature_flag (update)). The scope works only while its owner can change flags: it is checked on every request.
  • Node 18 or later (the JavaScript SDK), or Python 3.9 or later (the Python SDK).

JavaScript

import { ExperimentationClient } from '@controlvar/js-sdk';

const client = new ExperimentationClient({
  apiUrl: process.env.CONTROLVAR_API_URL!,
  apiKey: process.env.CONTROLVAR_SERVER_KEY!, // a key with the sdk:ruleset scope
  evaluation: 'local',
  refreshIntervalMs: 30_000,                    // default 30 s, minimum 5 s, ±10% jitter
});

const ready = await client.ready({ timeoutMs: 5_000 }); // never rejects
if (!ready.ok) console.warn('flags are evaluated by the server for now:', ready.error.message);

const { enabled, reason, source } = await client.evaluateFlag('new_search', {
  userId: 'user-123',
  attributes: { country: 'US', plan: 'pro' },
});

// On shutdown: stop refreshing and send the evaluation counts not yet sent.
await client.close();
Option / method Meaning
evaluation: 'server' \| 'local' 'server' (default) is unchanged behaviour.
refreshIntervalMs How often the ruleset is refreshed: default 30 000, values below 5 000 are raised to 5 000, jittered by ±10%. Waits after a failure (below) are exact, not jittered.
maxStaleMs Optional. When the ruleset has not been refreshed successfully for this long, flags are evaluated by the server until a refresh succeeds. Default: no limit.
ready({ timeoutMs? }) Resolves { ok: true, rulesetVersion } once flags can be answered locally, or { ok: false, error } when the first fetch failed or timeoutMs passed. Never rejects. Evaluations never wait for it.
status() { evaluation, ready, rulesetVersion, lastRefreshAt, lastError, serverEvaluatedFlags }.
close() Stops the refresh, sends the remaining evaluation counts, and from then on evaluates on the server.
source on FlagEvaluation 'local' or 'server' (set only in local mode).
onError(err, 'refresh' \| 'flush' \| 'evaluate') A failed ruleset refresh, a failed evaluation-count report, or an unexpected failure inside local evaluation (that call then goes to the server).

evaluateFlag, isFeatureEnabled and getAllFlags keep their signatures. A local answer uses the attributes passed to that call, so the "call clearCache() after changing attributes" caveat does not apply to it; it is still recorded for the key-less track() fan-out. That record is kept per process, so on a server pass experimentKey (experiment_key in Python) on conversions: a purchase tracked by an order service or another worker reaches no experiment without it. getAllFlags is answered locally only when every active flag can be answered for those attributes; otherwise the whole call goes to the server. The refresh and report timers do not keep a Node process alive.

Python

import os

from controlvar import ExperimentationClient

client = ExperimentationClient(
    os.environ["CONTROLVAR_API_URL"],
    os.environ["CONTROLVAR_SERVER_KEY"],  # a key with the sdk:ruleset scope
    evaluation="local",
    refresh_interval_seconds=30,            # default 30 s, minimum 5 s, ±10% jitter
    on_error=lambda error, operation: print(operation, error),
)

ready = client.ready(timeout_seconds=5)     # never raises; truthy when ready
if not ready:
    print("flags are evaluated by the server for now:", ready.error)

flag = client.get_feature_flag("new_search", "user-123", {"country": "US", "plan": "pro"})
print(flag.enabled, flag.reason, flag.source)

# On shutdown: stop refreshing and send the evaluation counts not yet sent.
client.close()                              # or: with ExperimentationClient(...) as client:
Argument / method Meaning
evaluation="server" \| "local" "server" (default) is unchanged behaviour.
refresh_interval_seconds How often the ruleset is refreshed: default 30, values below 5 are raised to 5, jittered by ±10%. Waits after a failure are exact.
max_stale_seconds Optional. When the ruleset has not been refreshed successfully for this long, flags are evaluated by the server until a refresh succeeds. Default: no limit.
on_error(error, operation) "refresh", "flush" or "evaluate", as in JavaScript. A handler that raises is logged and ignored.
ready(timeout_seconds=None) ReadyResult(ok, ruleset_version, error), truthy when flags can be answered locally. Waits for the first fetch (at most timeout_seconds). Never raises.
status() LocalEvaluationStatus(evaluation, ready, ruleset_version, last_refresh_at, last_error, server_evaluated_flags).
close(), with ... as client: Stops the refresh thread, sends the remaining evaluation counts, and from then on evaluates on the server.
source on FlagEvaluation "local" or "server" (set only in local mode).

get_feature_flag, is_feature_enabled and get_all_flags keep their signatures, with the same attribute, fan-out and get_all_flags rules as JavaScript. The attributes are evaluated as the server would receive them: the same json.dumps the request would send, read back (tuples become lists, non-string keys become strings, datetime values become ISO strings).

Threads and processes. One daemon thread per client refreshes the ruleset and sends the counts; the client is safe to share between threads. A client created before os.fork() (gunicorn with --preload, multiprocessing with the fork start method) keeps working in the child: the child gets fresh locks, starts its own refresh thread, and reports only the evaluations it made itself; the parent's counts stay with the parent. Create one client per process and share it.

What is evaluated locally

The ruleset marks each active flag "evaluation": "local" or "remote". A flag is local when every condition in its rules uses one of these operators, with a value the SDK can compare exactly as the server does:

Operator (dashboard name) Answered locally when
eq, neq (equals, not equals) the attribute and the rule value are both strings, or both numbers
in, not_in (in list, not in list) the attribute is a string and the list holds strings, or both are numbers
contains, not_contains, starts_with, ends_with the attribute and the rule value are both strings
gt, gte, lt, lte the attribute and the rule value are both numbers
is_null, is_not_null always, except on a string containing a whitespace character (the server trims strings; see below)

Anything else is evaluated by the server, for the flag or for that one evaluation:

  • flags using any other operator (semantic version, contains_all / contains_any, date and time windows, geo distance, array length, JSON path, percentage bucket, and pattern matching, which is server-only because Python and JavaScript regular expressions are different dialects), and flags stored in the legacy list shape: status() lists them as server-evaluated flags;
  • flags whose rules use a segment (in_segment, not_in_segment). Membership lives on the server (an ID list can hold a million IDs), so the ruleset marks these flags "remote" and carries neither their rules nor the segment, status() lists them as server-evaluated flags, and each evaluation is a request, counted against the SDK rate limit;
  • a flag whose stored rules the server cannot read (only a direct database write leaves one): the ruleset marks it "remote", and the server answers it enabled: false, reason: "error". While it is active, every local getAllFlags / get_all_flags call goes to the server, because a bulk call is answered locally only when every active flag is;
  • a string, number or JSON flag with a split (several values, each with a weight) or a targeting rule that has a value of its own. While such a flag is on, the ruleset marks it "remote" and carries neither its values nor its rules, and bulk calls go to the server, as above;
  • an attribute whose type does not match the rule (a string against a number, a boolean, a numeric string such as "10"); the server's type coercions are not reproduced;
  • a number larger than 2^53 − 1 in magnitude, and a non-finite one;
  • a string, attribute name or user id containing an unpaired UTF-16 surrogate;
  • is_null / is_not_null on a string that contains any whitespace character, even an ordinary space;
  • a flag key the ruleset does not have yet (a flag created since the last refresh).

A flag with a split or a rule's own value (Multi-way flags). Every SDK asks the server about such a flag: no release of either SDK evaluates it locally. Its answer from the server has the fields any flag's has, and a reason of targeting_rule or rollout for a user it is on for.

  • Turning one off (the switch, or a safety rollback to 0%) reaches every release within one refresh: an inactive flag is an entry the SDK answers locally, before any answer it has cached.
  • Any other change (its weights, the rollout percentage, a safety rollback to 1–99%, its rules) reaches a user an SDK has already cached only after the cache lifetime, 300 s by default (cacheTtlMs / cache_ttl_seconds), in every release.
  • Each user's first evaluation of such a flag in a cache lifetime is a request.
  • A single evaluation is counted once per IP address and flag key, because the user ID is in the query and not in the path, so above about 100 new users a second for one multi-way flag from one IP address the server answers 429; after one retry the getters return the caller's default.
  • While any such flag is active, a local getAllFlags / get_all_flags call goes from 0 requests to 1. The bulk route of each user ID is a path of its own under the SDK rate limit, 6000 requests a minute per IP address and path by default (SDK_RATE_LIMIT_PER_MINUTE), so above about 100 bulk calls a second for one user from one IP address the server answers 429.

Rollout percentages bucket users exactly as the server does (md5-mod100-v1: the whole MD5 digest of "{userId}:{flagKey}" read as a big-endian integer, mod 100). This is not the consistentHash / consistent_hash utility the SDKs export.

Freshness

A change made in the dashboard (turning a flag off, a rollout step, a safety rollback) reaches a process evaluating locally at its next successful refresh: by default within 33 seconds (the 30-second interval plus up to 10%) while the API is reachable. For a flag with a split or a rule's own value, which the SDK asks the server about, a change other than turning it off follows the cache lifetime instead (above). How long each client takes, in local evaluation and out of it: Turning a Flag Off.

While the API is unreachable (errors, timeouts), the SDK keeps serving the last ruleset it received, so a flag turned off during an outage stays on in that process until the API is reachable again. Set maxStaleMs / max_stale_seconds if you prefer evaluations to go to the server once the ruleset is that old. A process that starts while the API is unreachable has no ruleset: its evaluations go to the server, as in server mode.

Refresh answer No ruleset yet Ruleset loaded
200, or 304 (unchanged) load it replace it, or keep it
5xx, network error, timeout, or a 200 that is not a valid ruleset evaluate on the server; retry with backoff up to 5 minutes keep serving it; report refresh; retry with backoff
429 wait Retry-After (never less, and never less than the refresh interval) keep serving it; the same wait
401 / 403 (key revoked, scope removed, owner no longer allowed) evaluate on the server discard it: every flag is evaluated by the server
404 (a server without the ruleset), or a ruleset format this SDK does not know evaluate on the server discard it

401, 403, 404 and an unknown format are reported once and retried every 10 minutes, so a scope granted later is picked up without a restart. The backoff doubles from the refresh interval up to exactly 5 minutes. Only the regular refresh interval is jittered: backoff steps, Retry-After and the 10-minute retry are not.

With a JavaScript or Python SDK release published before November 2026, set cacheTtlMs / cache_ttl_seconds (5 minutes by default) lower if these limits must hold: once such a release stops evaluating a flag locally (maxStaleMs / max_stale_seconds passed, the ruleset dropped, close()), a user it answered locally keeps that answer from their cache for up to that lifetime.

Safety monitoring and the dashboard

The server cannot see local evaluations, so the SDK reports how many it made: per flag, every 60 seconds and on close(), to POST /api/v1/tracking/evaluations. Safety monitoring divides the errors your server reports with reportError (JavaScript) or report_error (Python), which send POST /api/v1/tracking/errors with the client's key (here the same sdk:ruleset key), and the evaluations that fail on the server, by these counts, so automatic rollback keeps working for flags evaluated locally. Reports sent with a key without the scope are stored but not counted (see Which errors count). Evaluations sent to the server are not counted twice.

reportError and report_error return true / True when the server stored the report (201), and false / False on any other answer or a network failure; they never throw. Stored is not counted: a report counts only when the key has the sdk:ruleset scope and was created by a user who can change feature flags, and the flag's safety check shows the production reports it stored but did not count as uncounted_error_count. With no evaluation counts in the check's 15-minute window (a process that stopped without close() loses up to 60 seconds of them), the error rate reads 0 and an error-rate threshold cannot fire.

Two dashboard figures change as local evaluation is adopted, because a count carries no user and no rule:

  • the rule-match rate of a flag falls: locally answered evaluations are counted, but not attributed to a rule;
  • unique users are under-counted: only evaluations made on the server carry a user id.

The server side, step by step

What the SDKs do, with curl, against a running stack. Run these in one terminal, in order; each step uses the shell variables set by the ones before it.

Log in as the demo administrator:

TOKEN=$(curl -s -X POST localhost:8000/api/v1/auth/login \
  -H 'content-type: application/json' \
  -d '{"email":"admin@demo.com","password":"Demo1234!"}' | jq -r .access_token)

curl -s localhost:8000/api/v1/auth/me -H "Authorization: Bearer $TOKEN" | jq .role

It prints "ADMIN".

Create a flag, turned on ("is_active": true; a new flag is off otherwise) and serving no one except users in the US or Canada:

curl -s -X POST localhost:8000/api/v1/feature-flags/ \
  -H "Authorization: Bearer $TOKEN" \
  -H 'content-type: application/json' \
  -d '{
    "key": "local-eval-demo",
    "name": "Local evaluation demo",
    "is_active": true,
    "rollout_percentage": 0,
    "targeting_rules": {
      "logical_operator": "AND",
      "groups": [
        {
          "logical_operator": "AND",
          "conditions": [
            {"attribute": "user.country", "operator": "in", "value": ["US", "CA"]}
          ]
        }
      ]
    }
  }' | jq -c '{key, status}'

It prints {"key":"local-eval-demo","status":"active"}.

Create a server-side key with the sdk:ruleset scope, and a plain key without it. The plaintext key is in the create response only:

SERVER_KEY=$(curl -s -X POST localhost:8000/api/v1/api-keys \
  -H "Authorization: Bearer $TOKEN" \
  -H 'content-type: application/json' \
  -d '{"name": "local-eval-demo-server", "scopes": ["sdk:ruleset"]}' | jq -r .key)
PLAIN_KEY=$(curl -s -X POST localhost:8000/api/v1/api-keys \
  -H "Authorization: Bearer $TOKEN" \
  -H 'content-type: application/json' \
  -d '{"name": "local-eval-demo-plain"}' | jq -r .key)

curl -s -o /dev/null -w '%{http_code}\n' localhost:8000/api/v1/sdk/ruleset -H "X-API-Key: $PLAIN_KEY"
curl -s -o /dev/null -w '%{http_code}\n' localhost:8000/api/v1/sdk/ruleset -H "X-API-Key: $SERVER_KEY"

It prints 403 for the plain key, then 200 for the server-side key.

The flag's entry in the ruleset: evaluated locally, with its conditions in the engine's operator names (the dashboard's groups become nested groups):

curl -s localhost:8000/api/v1/sdk/ruleset -H "X-API-Key: $SERVER_KEY" \
  | jq -c '.flags[] | select(.key == "local-eval-demo") | {evaluation, conditions: [.. | objects | select(has("operator"))]}'

It prints {"evaluation":"local","conditions":[{"attribute":"user.country","operator":"in","value":["US","CA"]}]}.

A refresh sends the ETag back, and an unchanged ruleset answers 304 with no body:

ETAG=$(curl -s -D - -o /dev/null localhost:8000/api/v1/sdk/ruleset -H "X-API-Key: $SERVER_KEY" \
  | tr -d '\r' | awk 'tolower($1) == "etag:" {print $2}')
curl -s -o /dev/null -w '%{http_code}\n' localhost:8000/api/v1/sdk/ruleset \
  -H "X-API-Key: $SERVER_KEY" -H "If-None-Match: $ETAG"

It prints 304.

The server's own answer for a US user, which an SDK evaluating locally gives without a request:

curl -s "localhost:8000/api/v1/feature-flags/evaluate/local-eval-demo?user_id=user-1&context=%7B%22country%22%3A%22US%22%7D" \
  -H "X-API-Key: $SERVER_KEY" | jq -c '{enabled, reason}'

It prints {"enabled":true,"reason":"targeting_rule"}: country in the context answers the rule on user.country.

Troubleshooting

What you see Cause Fix
Constructor throws "evaluation: 'local' downloads every flag's targeting rules, …" evaluation: 'local' in a browser (JavaScript) Leave the browser client in server mode.
refresh error: "The API key was refused (403) fetching the flag ruleset: This API key does not have the 'sdk:ruleset' scope …" The key lacks the scope Create a key with the sdk:ruleset scope.
The same, with "This key's owner can no longer change feature flags …" The key's owner can no longer change feature flags: an ANALYST or VIEWER with no custom role or direct grant giving feature_flag (update) Use a key created by a user who can change feature flags.
refresh error: "The API key was refused (401) fetching the flag ruleset: Invalid API Key …" The key was deleted, deactivated or has expired, or its owner's account is inactive Create a key as a user whose account is active and who can change feature flags.
"The server has no /api/v1/sdk/ruleset (404). Local evaluation needs Controlvar 0.11.0 or later …" The server predates the ruleset Upgrade the server, or leave evaluation at server.
"… a format this SDK does not understand …" The server is newer than the SDK Upgrade the SDK.
status() lists server-evaluated flags Those flags use rules that are evaluated on the server Nothing to fix; they keep working with a request each.
evaluate error: "Local evaluation failed; asking the server instead: …" A defect in the SDK's evaluator The call was answered by the server; please report it.