Load Balancing Methods
Nginx provides us with four HTTP Load Balancing methods, and Nginx Plus brings us two additional ones.
1.) Round Robin: It is the default Load Balancing method in Nginx or Nginx Plus. The requests are evenly distributed across all the servers. The server weights have no consideration whatsoever.
upstream back {
#we don’t need to define a balancing method for Round Robin
server back1.example.com weight=3;
server back2.example.com;
server back3.example.com weight=4;
}

You can also try this code with Online Python Compiler
Run Code
In the above example, the weight of back1.example.com is three, and the weight of back3.example.com is four. Since the weight of back2.example is not mentioned in the code, it is considered 1, the default weight. This means that out of eight requests, three will be redirected to back1, four will be redirected to back4, and one will be redirected to back2.
Recommended Topic - Ibegin TCS
2.) Least Connections: As the name suggests, in this Load Balancing method, we send the request to the server having the lowest number of connections. Similar to Round Robin, the server weights have no consideration.
upstream back {
least_conn;
server back1.example.com weight=3;
server back2.example.com;
server back3.example.com weight=4;
}

You can also try this code with Online Python Compiler
Run Code
3.) IP Hash: In this method, we either use the first three octets (twenty-four digits) of the IPv4 address or the complete IPv6 address to calculate a Hash value. Using the computed Hash value, we determine the server that will proceed next. We can also temporarily remove a server from the Load Balancing rotation by using the down parameter.
upstream back {
server back1.example.com;
server back2.example.com;
server back3.example.com;
Server back4.example.com down;
}

You can also try this code with Online Python Compiler
Run Code
4.) Generic Hash: In this method, we determine the server using a user-defined key that can be a string, number, or a combination of both.
upstream back{
hash $request_uri consistent;
server back1.example.com;
server back2.example.com;
}

You can also try this code with Online Python Compiler
Run Code
5.) Least Time: This method is only provided by Nginx Plus. In this Load Balancing method, the request is sent to the server with the least latency or the least number of connections.
upstream back {
Least_time header;
server back1.example.com weight=3;
server back2.example.com;
}

You can also try this code with Online Python Compiler
Run Code
6.) Random: This type of Load Balancing is also native to Nginx Plus. Every request is passed to a randomly selected server in this method. First, Nginx randomly selects two servers considering the server weights, and then the second one is chosen based on the specified method. The user can specify three methods in Nginx Plus.
Least_conn - Server having the least number of active connections.
least_time=header - Server having the least average time to receive the response header from the server.
Least_time=last_byte - Server having the least average time to receive the complete response from the server.
upstream back{
random two least_time=header;
server back1.example.com;
server back2.example.com;
}

You can also try this code with Online Python Compiler
Run Code
Server Slow-Start
This Nginx feature is used to prevent a recently recovered server from being overwhelmed by connections. We add the slow_start parameter to the server directive.
upstream back{
server back1.example.com slow_start=10s;
server back2.example.com;
}

You can also try this code with Online Python Compiler
Run Code
You can also read about mock interview.
FAQS
-
What is Nginx?
NGINX is a web server, and it can also be used as a reverse proxy, mail proxy, HTTP cache, and load balancer.
-
What is HTTP Load Balancing?
The Nginx HTTP Load Balancer attempts to distribute the workload evenly among various instances, either stand-alone or clustered. By doing so, it increases the overall throughput of the system.
-
What is Session Persistence?
Nginx Plus provides the feature of Session Persistence. In Session Persistence, Nginx Plus identifies all the user sessions, and then it routes all the requests in a session to the same upstream server.
-
What are the benefits of using NGINX?
NGINX provides a lot of benefits like,
Standardized JSON Configuration Files
HTTP configuration API
Consistent networking layer
Reconfiguration without restarts
-
What are the four methods of Load Balancing in Nginx?
Nginx provides four methods of Load Balancing,
Round Robin
Least Connections
IP Hash
Generic Hash
Key Takeaways
This Blog covered all the necessary points about NGINX HTTP Load Balancing and all the methods for HTTP Load Balancing present in Nginx and Nginx Plus.
Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.