Flag evaluation rules in server-side SDKs
Flag evaluation rules in server-side SDKs
Flag evaluation rules in server-side SDKs
This topic explains the algorithm used to evaluate a feature flag. This document is intended to be used by developers contributing to or building server-side SDKs for LaunchDarkly.
Client-side, server-side, and edge SDKs evaluate feature flags differently:
SDKs evaluate flags and customize configs based on an evaluation context, which may contain one or more specific contexts. The arguments needed are the flag key or config key, the evaluation context, and the fallback variation value.
Evaluating a flag or customizing a config for an evaluation context involves the following steps:
The result of a flag evaluation is:
The result of a config customization is the customized config variation value, including the model and customized messages for the context.
Variation values are often referenced by their index. For example, if a flag has two variations, true and false, then true will be variation index 0, and false will be variation index 1.
As a side effect of the evaluation, the SDKs send events back to LaunchDarkly to report that the evaluation or customization has occurred. The data sent back for each evaluation, including prerequisites, follows the schema described in the Streaming Data Export schema reference.
The details in the sections below are specific to server-side SDKs evaluating flags. AI SDKs follow roughly the same algorithms when customizing configs; the customization call is treated as an evaluation, but the evaluation reason is not returned.
Five preliminary checks occur before the main flag evaluation algorithm executes:
ERROR evaluation reason and the corresponding error code.ERROR evaluation reason and the corresponding error code.key attribute, or if the context key is invalid, return the provided fallback value with the ERROR evaluation reason and the corresponding error code.OFF evaluation reason.If all the above checks pass, the evaluation algorithm proceeds with prerequisite checks.
A flag may have prerequisites. A prerequisite is defined by a tuple of prerequisite flag key and variation index. A prerequisite is satisfied if, for a given context, the prerequisite flag evaluates to the specified variation index.
In the prerequisite check step, the SDK iterates through each flag’s prerequisites and verifies that each prerequisite is satisfied. If a prerequisite is not satisfied, the SDK returns the flag’s off variation value, with the PREREQUISITE_FAILED evaluation reason and the prerequisite flag key that failed. Prerequisite evaluation is short-circuited, which means the SDK returns after the first failure.
Below are the steps for prerequisite evaluations:
Prerequisite flags themselves may also have their own prerequisites, in which case the evaluations are recursive. LaunchDarkly prevents recursive cycles during flag creation and modification.
Once all prerequisites have passed, or there were no prerequisites, the evaluation algorithm proceeds with individual targeting checks.
Flags may have targets. A target is a list of context keys associated with a specific variation index. SDKs iterate through flags’ targets and check whether the targets contain the given context’s key.
If a target contains the context’s key, return the associated variation with the TARGET_MATCH evaluation reason.
If there were no targets, or no targets matched the context, the evaluation algorithm proceeds with the targeting rule checks.
Flags may have rules. Rules offer more complex ways to match arbitrary context attributes and segments, and to execute controlled rollouts based on those criteria. A rule is defined as a tuple of the ID, collection of clauses, and either the variation index or a rollout plan, as described in more detail below. For a rule to match, all rules’ clauses must be satisfied.
SDKs iterate through flags’ rules to find the first rule that matches the given context. If the matched rule has a variation index, return the corresponding variation, with the rule’s ID and the RULE_MATCH evaluation reason. If, on the other hand, the rule has a rollout, follow the rollout logic to arrive at the variation and also return the corresponding variation, along with the rule ID and the RULE_MATCH evaluation reason.
If a targeting rule references any custom attributes with null values, then the flag skips that rule. If there were no rules, or no rules matched the context, the evaluation algorithm proceeds with the default rule when on, also called the “fallthrough” value.
Rules have clauses, all of which must be satisfied for a rule to match the context. Clauses are defined as a tuple of:
user.Evaluating a clause is a matter of taking the value of the given context attribute and performing the given operator comparison against the given values, and then applying whether the outcome should be negated. If this produces a positive result, the clause has been satisfied.
If the context attribute value is an array or list, then the comparison is performed for each of those attribute values, against each of the clause values. In this case, a single context attribute value match to a single clause value is enough to satisfy the clause.
If the context being evaluated is a multi-context, the attribute lookup is performed against the associated context with matching context kind. For example, if the multi-context being evaluated includes “user” and “organization” contexts and the clause has an “organization” context kind, then the attribute reference uses the “organization” context kind.
This table explains operators and their arguments and conditions:
Clauses’ segmentMatch operator allows for more fine-grained control of segments. The clause values are segment keys. To determine if this clause is satisfied, retrieve each of the segments identified by those keys and determine if the context is part of any of those segments.
Segments’ properties include a list of included contexts, a list of excluded contexts, and a list of rules of their own, which themselves contain rule clauses with one exception: segment rule clauses may not contain segmentMatch operators.
To evaluate whether a context is in a segment:
included list, the result is true.excluded list, the result is false.null, the rule matches and the result is true.bucketBy value. If the value is not set, use key.true. Otherwise, the rule doesn’t match and move on to the next rule.false.negate property) result from above.You can associate rules with percentage rollouts, which provides flexibility when you assign contexts to variation buckets.
Rollouts are defined as:
Weighted variations are a subset of the variation index and a non-negative integer between 0 and 100,000 acting as that variation’s weight. In the LaunchDarkly user interface, you define the rollout by specifying the weight as a percentage between 0 (represented as 0) and 100 (represented as 100,000) for each variation.
To assign a context to a variation bucket and calculate the final variation:
If the context’s kind does not match the rollout’s context kind, return the variation index of the first variation with weight greater than zero. For an example, read Percentage rollout logic with multiple context kinds.
Get the context’s attribute name from the rollout to assign the context, using the rollout’s context kind to perform the attribute lookup. If the attribute is not set, use the context’s key.
Get the context’s attribute value.
Compute the variation bucket for the context.
Concatenate the flag’s key, the flag’s salt, and the context’s attribute value. Concatenate them with periods, .. If there is a seed present, concatenate seed and the context’s attribute value instead.
Copy the first 15 characters of the SHA1 of the above.
Convert the resulting base 16 integer to a base 10 integer.
Divide the resulting base 10 integer by 0xFFFFFFFFFFFFFFF (1152921504606846975). The result of this division is the context’s variation bucket number.
Iterate over the rollout’s weighted variations.
Fallthrough is the last step in flag evaluation. If the evaluation process is made here, it means that all prerequisite checks passed and none of the targets or rules matched the context.
Fallthrough is defined as a single variation of a rollout. This is similar to how rules are defined, except without iterating through any IDs or clauses. If fallthrough is a variation index, then return the corresponding variation value with the FALLTHROUGH evaluation reason. Otherwise, go through the same rollouts steps to arrive at the variation index, and again, return the corresponding value and the FALLTHROUGH evaluation reason.