Current Location: Home> Latest Articles> Nginx Load Balancing Fair Algorithm Principles and Practical Guide

Nginx Load Balancing Fair Algorithm Principles and Practical Guide

M66 2025-10-22

Introduction to Nginx Load Balancing and Fair Algorithm

In distributed systems, load balancing is a key factor for ensuring service stability and high performance. Nginx, as a high-performance web server and reverse proxy server, supports multiple load balancing algorithms. Among them, the Fair algorithm is a weight-based strategy for evenly distributing requests.

Principles of the Fair Algorithm

The core idea of the Fair algorithm is to allocate requests according to server weights, keeping the load on each server relatively balanced. The implementation steps are as follows:

  • Generate a server list based on each server's weight.
  • When a request arrives, select the server with the highest current weight for processing.
  • After handling the request, subtract the greatest common divisor of total weights from that server's weight.
  • Update the server list for the next request allocation.

This approach allows requests to be distributed according to server capacity, achieving effective load balancing.

Implementing Fair Algorithm in Nginx

Nginx does not provide a direct Fair algorithm by default, but it can be implemented through custom configuration. The example below shows how to configure the Fair algorithm in Nginx:

http {
    upstream backend {
        fair;
        server backend1.example.com weight=3;
        server backend2.example.com weight=2;
        server backend3.example.com weight=1;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;
        }
    }
}

In this configuration, the upstream directive defines the server list, and the weight parameter sets the weight for each server. Higher-weight servers handle more requests, making full use of server resources.

Code Example of the Fair Algorithm

To better understand the Fair algorithm, here is a Python implementation example:

import math

servers = [
    {'name': 'backend1.example.com', 'weight': 3},
    {'name': 'backend2.example.com', 'weight': 2},
    {'name': 'backend3.example.com', 'weight': 1},
]

total_weight = sum(server['weight'] for server in servers)
gcd = math.gcd(*[server['weight'] for server in servers])

for server in servers:
    server['current_weight'] = server['weight']

while True:
    best_server = None
    best_weight = 0

    for server in servers:
        server['current_weight'] += server['weight']
        if server['current_weight'] > best_weight:
            best_server = server
            best_weight = server['current_weight']

    best_server['current_weight'] -= total_weight

    print('Request sent to: %s' % best_server['name'])

In this code, the current weight of each server is calculated dynamically. The server with the highest current weight handles the incoming request, and weights are updated after each allocation, achieving balanced load distribution.

Conclusion

The Fair algorithm is a weight-based load balancing strategy that distributes requests according to server capacity, improving system stability and efficiency. Through Nginx configuration and code examples, developers can implement the Fair algorithm flexibly and adjust server weights according to real performance to achieve optimal system performance.