# Weighted Least-Connection Scheduling

Jump to: navigation, search

## Introduction

The weighted least-connection scheduling is a superset of the least-connection scheduling, in which you can assign a performance weight to each real server. The servers with a higher weight value will receive a larger percentage of active connections at any one time. The default server weight is one, and the IPVS Administrator or monitoring program can assign any weight to real server. In the weighted least-connections scheduling, new network connection is assigned to a server which has the least ratio of the current active connection number to its weight.

## Algorithm

The weighted least-connection scheduling works as follows:

```Supposing there is a server set S = {S0, S1, ..., Sn-1},
W(Si) is the weight of server Si;
C(Si) is the current connection number of server Si;
CSUM = ΣC(Si) (i=0, 1, .. , n-1) is the sum of current connection numbers;

The new connection is assigned to the server j, in which
(C(Sm) / CSUM)/ W(Sm) = min { (C(Si) / CSUM) / W(Si)}  (i=0, 1, . , n-1),
where W(Si) isn't zero
Since the CSUM is a constant in this lookup, there is no need to divide by CSUM,
the condition can be optimized as
C(Sm) / W(Sm) = min { C(Si) / W(Si)}  (i=0, 1, . , n-1), where W(Si) isn't zero

Since division operation eats much more CPU cycles than multiply operation, and Linux
does not allow float mode inside the kernel, the condition C(Sm)/W(Sm) > C(Si)/W(Si)
can be optimized as C(Sm)*W(Si) > C(Si)*W(Sm). The scheduling should guarantee
that a server will not be scheduled when its weight is zero. Therefore, the pseudo
code of weighted least-connection scheduling algorithm is

for (m = 0; m < n; m++) {
if (W(Sm) > 0) {
for (i = m+1; i < n; i++) {
if (C(Sm)*W(Si) > C(Si)*W(Sm))
m = i;
}
return Sm;
}
}
return NULL;
```

The weighted least-connection scheduling algorithm requires additional division than the least-connection scheduling. In a hope to minimize the overhead of scheduling when servers have the same processing capacity, both the least-connection scheduling and the weighted least-connection scheduling algorithms are implemented.