Different Types of Load Balancing through Nginx

Posted By : Tarun Singhal | 21-Apr-2018

There are three algo used by nginx to distribute the load:-

  • Round-Robin.
  • Least Connection.
  • Hashing

Round Robin distribute the load in sequential manner.First request goes to first server and second goes to second server and so on.
Least Connection - in this nginx distribute the load to the backend sever with the least active connection.
Hash method - This uses a key to determine how to map the request with one of the upstream servers. Generally, this is set to the client’s IP address, which allows you to map the requests to the same upstream server each time.

  Configuring The LB.

  Round Robin based load balancing

  • create an upstream
upstream backend { 
    server 127.0.0.1:8080; 
    server 127.0.0.1:8081; 
    server 127.0.0.1:8082; 
}  
  • Define a server block
server { 
    listen       80; 
    server_name  example.com; 
    access_log  /var/log/nginx/access.log  combined; 
    location / { 
        proxy_pass http://backend; 
    } 
}  

In this scenario, request to example.com will distribute between the backend server deines in upstream block.
First request to example.com will proxy pass to backend app with port 8080, likewise second request to backend app with port 8081 and third request to backend app with port 8082 and so on.
Also, it’s possible to weight the servers, meaning it will preference upstream servers with a higher weight. If your servers aren’t exactly the same, you can use weighting to preference your higher capacity systems so that they receive more request.

upstream backend { 
    server 127.0.0.1:8080 weight=2; 
    server 127.0.0.1:8081; 
    server 127.0.0.1:8082; 
}  

Because we set the first server with a weighted value of 2, it will receive twice as many requests as the others.

Least connected load balancing

The upstream block will almost same as of Round Robin but there will be an extra key word i.e least_conn;.

upstream backend { 
    least_conn; 
    server 127.0.0.1:8080; 
    server 127.0.0.1:8081; 
    server 127.0.0.1:8082; 
} 

As each new request comes in, NGINX determines which upstream server has the least amount of connections and directs requests to this server.

Hash-based load balancing

The upstream block will almost same as of Round Robin but we need to explicitly tell NGINX to use the hash method.

upstream backend { 
    hash $remote_addr consistent; 
    server 127.0.0.1:8080; 
    server 127.0.0.1:8081; 
    server 127.0.0.1:8082; 
} 

For this hash method, we used the client IP ($remote_addr) as the determining factor to build up the hash map.

The consistent parameter at the end of the hash line implements the Ketama consistent hashing method, which helps to minimize the amount or remapping (and therefore potential disruption or cache loss) if you need to add or remove servers from your upstream block directive. If your upstream servers remain constant, then you can omit this parameter.
For those who have used older versions of NGINX, the ip_hash method is still available, but with one distinct difference.

upstream backend { 
    ip_hash; 
    server 127.0.0.1:8080; 
    server 127.0.0.1:8081; 
    server 127.0.0.1:8082; 
}  

While this method still works, if you need better consistency for ip_hash mapping, then using hash $remote_addr will match the full IP address.

About Author

Author Image
Tarun Singhal

Tarun is a RedHat Certified System Administrator. He is very keen to learn new technologies. He has good command over tools like Ansible, Gitlab-CI etc.

Request for Proposal

Name is required

Comment is required

Sending message..