different types of load balancing in nginx3 min read

Prabhin Prabharkaran Administrator
DevOps Engineer

He is a Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!

follow me

Hi All, this document is about different types of load balancing in nginx.
Nginx is one of the best and commonly used webserver in the world. It famous more for its capability of handling the requests.

It supports various modules that can be used for real-time examples. Nginx can be used as a reverse proxy, mail proxy, HTTP cache, and even a load balancer.
Today I’m going to brief you about the load balancing techniques which are supported by Nginx.
Nginx supports various support of load balancer types such as

  •  round-robin
  •  least connections
  •  IP hash
  •  weighted

Let’s look into each technique separately

Round Robin
Round Robin is the default load-balancing method, this doesn’t require any kind of configuration.

 upstream backend {
    server bk1;
    server bk2;
    server bk3;
    server bk4;
}

server {
    server_name www.techrunnr.com;

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

Above is the sample configuration which works with round-robin. Round Robin works as an equal number of requests are given to the number of servers listed the upstream. It will better always to have the same configuration of servers. If any of the servers/services are down on the listed upstream nginx continues to send the request to those servers. This results in a 502 gateway error for the users.
In such cases, the configuration can be tweaked with max_fail and fail_timeout.
fail_timeout – This sets the time duration of fails mush to happen for the server to be marked as the server is unavailable to handle requests.
max_fail – is the number of fail attempts that happened during the fail_timeout to mark server is unavailable.

 upstream backend {
    server bk1;
    server bk2;
    server bk3;
    server bk4 max_fails=4 fail_timeouts=15s;
}

Least connection.
In this method of load balancing, the load balancer sends requests to the server which have less active connections. In this method, not all the servers loaded with more connection. Nginx will handle it internally.
This configuration is made by adding the least_conn derivative in the upstream section.

 upstream backend {
 	least_conn;
    server bk1;
    server bk2;
    server bk3;
    server bk4;
}

server {
    server_name www.techrunnr.com;

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

IP Hash
IP Hash works for HTTP only and the has is based on the client IP address. This is enabled by adding ip_hash derivative in the upstream section. In IPv4 the hash is based on the first 3 octets of the address. So whenever the IP address changes dynamically router reboot or reconnection nginx checks only the first 3 octets and the request still goes to the same connected server as before.
This method is not good when the customer is expecting a huge request coming from a specific series of IP addresses. This will lead to server unavailability.
Still to overcome these scenarios use the max_fail concept for the upstream servers.

Weighted Routing
This is another method used in load balancer which you can choose when you want to split the traffic across the server based on some values.
Lets you want to split requests on one server with 75% and other with 25%, below is the configuration for that.

 upstream backend {
    server bk1 weight=75;
    server bk2 weight=25;

}

server {
    server_name www.techrunnr.com;

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

this type of configuration helps when the equal specification is not used in the load balancer.

Hope you understood nginx load balancing methods.

© 2020, Techrunnr. All rights reserved.

#1
#2
#3
Questions Answered
Articles Written
Overall Points

Related posts

Leave a Reply