Understanding Simple, Weighted, and Smooth Weighted Round‑Robin Load Balancing

This article explains three common round‑robin scheduling algorithms—simple, weighted, and smooth weighted—detailing their logic, step‑by‑step execution, PHP implementation, and the trade‑offs of each approach in distributed systems.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Understanding Simple, Weighted, and Smooth Weighted Round‑Robin Load Balancing

Simple Round‑Robin

The algorithm cycles through a list of service instances sequentially. A currentPos pointer starts at -1 and is incremented modulo the number of instances on each request.

interface RobinInterface {
    public function init(array $services);
    public function next();
}

class Robin implements RobinInterface {
    private $services = [];
    private $total;
    private $currentPos = -1;
    public function init(array $services) {
        $this->services = $services;
        $this->total = count($services);
    }
    public function next() {
        $this->currentPos = ($this->currentPos + 1) % $this->total;
        return $this->services[$this->currentPos];
    }
}

$services = ['192.168.10.1','192.168.10.2','192.168.10.3','192.168.10.4'];

All instances receive the same number of requests, which ignores performance differences.

Weighted Round‑Robin

Each instance is assigned a weight, allowing more powerful servers to handle more traffic. The algorithm maintains currentPos, currentWeight, the maximum weight max(S), and the greatest common divisor gcd(S). After a full cycle, currentWeight is decreased by gcd(S); when it reaches zero it is reset to max(S). The first instance whose weight meets or exceeds currentWeight is selected.

class WeightedRobin implements RobinInterface {
    private $services = [];
    private $total;
    private $currentPos = -1;
    private $currentWeight;
    public function init(array $services) {
        foreach ($services as $ip => $weight) {
            $this->services[] = ['ip' => $ip, 'weight' => $weight];
        }
        $this->total = count($this->services);
    }
    public function next() {
        $i = $this->currentPos;
        while (true) {
            $i = ($i + 1) % $this->total;
            if ($i === 0) {
                $this->currentWeight -= $this->getGcd();
                if ($this->currentWeight <= 0) {
                    $this->currentWeight = $this->getMaxWeight();
                }
            }
            if ($this->services[$i]['weight'] >= $this->currentWeight) {
                $this->currentPos = $i;
                return $this->services[$i]['ip'];
            }
        }
    }
    private function gcd($a, $b) {
        while ($b) { $rem = $a % $b; $a = $b; $b = $rem; }
        return $a;
    }
    private function getGcd() {
        $gcd = $this->services[0]['weight'];
        for ($i = 0; $i < $this->total; $i++) {
            $gcd = $this->gcd($gcd, $this->services[$i]['weight']);
        }
        return $gcd;
    }
    private function getMaxWeight() {
        $max = 0;
        foreach ($this->services as $node) {
            if ($node['weight'] > $max) $max = $node['weight'];
        }
        return $max;
    }
}

$services = [
    '192.168.10.1' => 1,
    '192.168.10.2' => 2,
    '192.168.10.3' => 3,
    '192.168.10.4' => 4,
];

Weighted round‑robin improves utilization but can produce bursty patterns (e.g., several consecutive requests to the highest‑weight instance).

Smooth Weighted Round‑Robin (preview)

Smooth weighted round‑robin mitigates the burstiness of weighted round‑robin by generating a more evenly distributed request sequence. Detailed implementation is omitted.

Algorithm analysis

Statelessness: All three algorithms keep only minimal state (pointers and weights), making them easy to implement in high‑performance load balancers.

Limitations: They cannot sense real‑time load; weights must be configured manually, which may not guarantee high availability under sudden load spikes.

Weighted round‑robin drawback: With weights {5,1,1}, the generated sequence can be {a,a,a,a,a,b,c}, causing five consecutive requests to instance a. This unevenness can overload a single server.

Smooth weighted round‑Robin benefit: Produces a more balanced sequence such as {a,a,b,a,c,a,a}, reducing the risk of overload.

Source code repository: https://github.com/Tinywan/load-balancing

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

algorithmPHPRound RobinWeighted Round Robin
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.