> ## Documentation Index
> Fetch the complete documentation index at: https://backstage.spotify.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Rules

> Assign variants to users with general, override, and percentage-based rules that execute in priority order in Confidence Flags.

Use rules to assign variants.

Rules decide which variant to use for a particular client and user
at a given time. The purpose for a rule can be many: to do
a rollout, turn on a feature for employees only, or to give users in a certain
market a different experience. Importantly, rules execute in order from
top to bottom. The first rule that matches determines what variant is
assigned to the user.

If no rule matches, the client uses the default value of the flag defined in the client.

<Tip>
  Avoid rules that overlap. For example, if you have a rule that matches all
  users, and another rule that matches a subset of users, the second rule never
  matches.
</Tip>

## Types of Rules

Three types of rules are available:

* A **general rule**; the most flexible type of rule. Use when you want to assign
  a variant to a set of users based on a set of criteria.
* An **override rule**; used to force a specific variant for a set of users. Use it
  for testing and debugging.
* A **rollout rule**; used to assign a percentage of the traffic to a variant.
  Use when you want to gradually turn on a feature for a set of users.

### General Rules

General rules are the most flexible type of rule. Use when you want to assign
a variant to a set of users based on a set of criteria.
A/B tests and other types of experiments use general rules.

General rules have inclusion criteria, allocation, and randomization.
See the [audience](./audience) page for more information on how to set these.

#### Variant Assignment

The variant assignment defines how to assign variants to users.
It specifies how much, as a percentage, of the traffic each of the variants should receive.

The sum of the percentages can not be more than 100%. If the sum is less than
100%, the remaining traffic receives the default values in the clients.

### Override Rules

Use override rules to force a specific variant for a set of users. For example,
use override rules for testing and debugging.

<Tip>
  Put override rules at the top of the list of rules. This ensures they
  evaluate first and override other rules. When you create a new rule, your rule
  is at the bottom of the list.
  To move the rule to the top, drag it to the top of the list.

  Putting override rules at the top also increases their visibility, and lets
  you quickly turn them off when they are no longer needed.
</Tip>

### Rollout Rules

Use rollout rules to assign a percentage of the traffic to a variant.
Use them when you want to gradually turn on a feature for a set of users.

The users that don't match the rule, or that are not assigned to a variant by
the rule, are assigned a variant based on a rule later in the list of
rules, or the default value of the flag.

## Enable Rules

Rules are inactive when you create them. To enable a rule, click the slider
button in the top left corner of the rule card.

## Order of Rules

The order of the rules defines in what order to evaluate them. The first rule that matches a client
and a user decides the variant to use. You can reorder the rules by dragging them up and down in
the list.

Newly created rules are at the bottom of the list to not impact already
running tests and rollouts.

<Warning>
  Changing the order of rules can potentially have a big impact on the behavior
  that users experience.
</Warning>

## Create a General Rule

To create a general rule, follow these steps.

1. Go to Portal and select **Confidence flags** on the left sidebar.
2. Go to the flag you want to add a rule to.
3. Click **+ Create rule**.
4. Select **General**.
5. Build the audience according to the steps on the [audience](./audience) page.
6. Select how to split the traffic between the variants.
7. Click **Save**.
8. Enable the rule by clicking the toggle on the rule card.

Traffic is now given to your audience according to the split defined in the rule.

## Create an Override Rule

To create an override rule, follow these steps.

1. Go to Portal and select **Confidence flags** on the left sidebar.
2. Go to the flag you want to add a rule to.
3. Click **+ Create rule**.
4. Select **Override**.
5. Select the variant to assign to the users that match the rule in the **Variant** section.
6. Enter which field in the evaluation context that clients use pass the identify of the users in the **Attribute** section.
7. Enter a **list of values** to match against. If the value of the field matches one of the values in the list, the rule matches
   and the variant is assigned.
8. Click **Save**.
9. Enable the rule by clicking the toggle on the rule card.

The rule now gives the variant selected to the requests that have a match for the list of values provided in the rule.

<Frame>
  <img src="https://mintcdn.com/spotify-89f50c35/30KNAI5WzZFwyWnx/portal/guides/confidence-flags/enable-rule.png?fit=max&auto=format&n=30KNAI5WzZFwyWnx&q=85&s=27fdbf16bb52b6c8e2889a42831c6710" alt="Enable rule" width="1554" height="402" data-path="portal/guides/confidence-flags/enable-rule.png" />
</Frame>

## Create a Rollout Rule

To create a rollout rule, follow these steps.

1. Go to Portal and select **Confidence flags** on the left sidebar.
2. Go to the flag you want to add a rule to.
3. Click **+ Create rule**.
4. Select **Rollout**.
5. Pick the variant to roll out.
6. Build the audience according to the steps on the [audience](./audience) page.
7. Set the allocation to decide what percentage should receive the variant.
8. Click **Save**.
9. Enable the rule by clicking the toggle on the rule card.
