Upstream Feedback#

Implements feedback-based load balancing for server groups of the HTTP Upstream module: server weights are adjusted by a metric that the servers report, for example in response headers. The module's directive is set in the upstream block.

Related guides

Configuration Example#

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

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

Each server reports its spare capacity in the X-Capacity response header field; a server that reports a higher value gets a larger share of requests.

Directives#

feedback (PRO)#

Syntax

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

Default

—

Context

upstream

Sets up a feedback-based load balancing mechanism in the upstream. It dynamically adjusts balancing decisions by multiplying the weight of each proxied server by the average feedback value, which changes over time depending on the value of the variable 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 that the server provides it in headers or otherwise.

The value is evaluated with each response from the server and is factored into the moving average according to the 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 considered 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, then 90% of the previous value and only 10% of the new value will be taken.

account

Specifies a condition variable that controls which responses are considered in the calculation. The average value is updated with the feedback value from the response only if the condition variable of that response is not equal to "" or "0".

Note

By default, responses during active checks are not included in the calculation; combining the $upstream_probe variable with account allows including these responses or even excluding everything else.

last_byte

Allows processing data from the proxied server after receiving the complete response, not just the header.

Example:

upstream backend {

    zone backend 1m;

    feedback $feedback_value factor=80 account=$condition_value;

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

map $upstream_http_custom_score $feedback_value {
    "high"                      100;
    "medium"                    75;
    "low"                       50;
    default                     10;
}

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

This configuration categorizes server responses by feedback levels based on specific scores from response header fields, and also adds a condition on $upstream_probe to consider only responses from the high_priority active check or responses to regular client requests.