http_upstream_feedback module

http_upstream_feedback module#

The module implements a feedback-based balancing mechanism for http_upstream, using a designated variable to dynamically recalculate peer weights with average feedback values received with previous requests. It also allows to conditionally control which requests are accounted for in the calculation.

Example Configuration#

upstream backend {

    zone backend 1m;

    feedback $request_time inverse factor=80;

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

Directives#

feedback (PRO)#

Added in version 1.6.0: PRO

Syntax:

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

Default:

Context:

upstream

Enables a feedback-based load balancing mechanism for the upstream context. It adjusts the load balancing decisions dynamically, multiplying each peer’s weight by its average feedback value that is affected by the value of the variable over time and is subject to an optional condition.

The following parameters are accepted:

variable

The variable from which the feedback value is taken. It should represent a performance or health metric, and is intended to be supplied by the peer in header fields or otherwise.

The value is assessed at each response from the peer and factored into the rolling average according to inverse and factor settings.

inverse

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

factor

The factor by which the feedback value is weighted when calculating the average. Valid values are integers between 0 and 100. By default — 90.

The average feedback is calculated using the exponential moving average formula.

The larger is the factor, the less is the average affected by new values; if the factor is set to 90, the result has 90% of the previous value and only 10% of the new value.

account

Specifies a condition variable that controls which responses should be included in the calculation. The average is updated with the feedback value only if the condition variable for the response isn’t "" or "0".

Note

By default, responses from probes aren’t included in the calculation; combining the $upstream_probe variable with account allows to include these responses or even exclude everything else.

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 categorizes server responses into different feedback levels based on specific scores obtained from response header fields, and also adds a condition mapped from $upstream_probe to account only for the responses from the high_priority probe or responses to regular client requests.