Upstream Feedback#

Implements feedback-based load balancing for server groups of the Stream Upstream module: server weights are adjusted by a performance or health metric of each server. The module's directive is set in the upstream block.

Configuration Example#

upstream backend {
    zone backend 1m;
    feedback $connect_score;

    server backend1.example.com:1935;
    server backend2.example.com:1935;
}

map $upstream_connect_time $connect_score {
    ~^0\.00    100;
    ~^0\.0     50;
    default    10;
}

Servers that accept connections faster get a higher score from the map block and a larger share of sessions.

Directives#

feedback (PRO)#

Syntax

feedback variable [inverse] [factor=number] [account=condition_variable];

Default

—

Context

upstream

Enables a feedback-based load balancing mechanism for the upstream. It dynamically adjusts load balancing decisions by multiplying each proxied server's weight by the average feedback value, which changes over time based on the variable value and is subject to an optional condition.

The following parameters can be specified:

variable

The variable from which the feedback value is taken. It should represent a performance or health metric; it is assumed to be provided by the server.

The value is evaluated with each response from the server and factored into the moving average according to inverse and factor settings.

inverse

If the parameter is set, the feedback value is interpreted inversely: lower values indicate better performance.

factor

The factor by which the feedback value is weighted when calculating the average. Valid values are integers from 0 to 99. Default is 90.

The average is calculated using the exponential smoothing formula.

The larger the factor, the less new values affect the average; if 90 is specified, 90% of the previous value will be taken and only 10% of the new value.

account

Specifies a condition variable that controls how connections are accounted for in the calculation. The average value is updated with the feedback value only if the condition variable is not equal to "" or "0".

Note

By default, traffic from probes is not included in the calculation; combining the $upstream_probe variable with account allows including them or even excluding everything else.

Example:

upstream backend {

    zone backend 1m;

    feedback $feedback_value factor=80 account=$condition_value;

    server backend1.example.com:1935  weight=1;
    server backend2.example.com:1935  weight=2;
}

map $protocol $feedback_value {
    "TCP"                      100;
    "UDP"                      75;
    default                    10;
}

map $upstream_probe $condition_value {
    "high_priority" "1";
    "low_priority"  "0";
    default         "1";
}

This configuration categorizes servers by feedback levels based on protocols used in individual sessions, and also adds a condition on $upstream_probe to account only for high_priority probes or regular client sessions.