Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

Navigating Web Traffic: Nginx, The Art of Load Balancing

Tuesday 16 April 2024
|
Read: Completed in minutes

Navigating Web Traffic: Nginx, The Art of Load Balancing

Introduction

Load balancers play a crucial role in ensuring the availability, scalability, and performance of web applications. Among the various load balancing solutions, Nginx stands out as a powerful and versatile option. In this essay, we will explore the significance of load balancers, the role of Nginx, and its benefits.

The Importance of Load Balancers

 Use Nginx as a Load balancer

High Availability

  • In a distributed system, multiple servers handle incoming requests. Load balancers distribute these requests across the servers, ensuring that no single server becomes overwhelmed.
  • If one server fails, the load balancer redirects traffic to healthy servers, maintaining high availability.

Scalability:

  • As web traffic grows, adding more servers becomes necessary. Load balancers allow seamless scaling by distributing requests evenly across the server pool.
  • This elasticity ensures that the application can handle increased load without compromising performance.

Improved Performance:

  • Load balancers consider factors like server health, response time, and server capacity.
  • By directing requests to the most suitable server, they optimize performance and reduce latency.

Nginx as a Load Balancer

Nginx Overview

  • Nginx is an open-source web server, reverse proxy, and load balancer
  • It efficiently handles thousands of concurrent connections and provides high performance
  • Nginx is event-driven and asynchronous, making it ideal for load balancing

Load Balancing Algorithms

  • Nginx supports various load balancing algorithms
  • Round Robin: Distributes requests sequentially to each server
  • Least Connections: Sends requests to the server with the fewest active connections
  • IP Hash: Uses the client’s IP address to consistently route requests to the same server
  • Weighted Round Robin: Assigns weights to servers based on their capacity
  • Least Time: Routes requests to the server with the lowest response time

Configuration:

  • In the Nginx configuration, define an upstream block with server entries representing backend servers.
  • Specify the load balancing algorithm using the upstream directive.
  • Example
Navigating Web Traffic: Nginx, The Art of Load Balancing

Choosing a Load Balancing Method

Round Robin - Requests are distributed evenly across the servers, with server weights taken into consideration. This method is used by default

Navigating Web Traffic: Nginx, The Art of Load Balancing

Navigating Web Traffic: Nginx, The Art of Load Balancing


Least Connections - a request is sent to the server with the lease number of active connections, again with server weights taken into consideration;

Navigating Web Traffic: Nginx, The Art of Load Balancing

Navigating Web Traffic: Nginx, The Art of Load Balancing







IP Hash - The server to which a request is sent is determined form the client IP address. In this case, either the first three octets of the IPv4 address or the whole IPv6 address ae used to calculate the hash value. The method guarantees that requests from the same address get to the same server unless it is not available.

Navigating Web Traffic: Nginx, The Art of Load Balancing

If one of servers needs to be temporarily removed from the load-balancing rotation, it can be marked with the "down" parameter in order to preserve the current hashing of client IP addresses. Request that were to be processed by this serve are automatically sent to the next server in the group.

 
Generic Hash - The server to which a request is sent is determined from a user-defined key which can be a text string, variable, or a combination. 
For example: the key may be a paired source IP address and port, or a URL as in this example:

 
The option "consistent" parameter to the hash directive enables ketama consistent-hash load balancing. Requests are evenly distributed across all upstream servers based on the user-defined hashed key value. If an upstream server is added to or removed from an upstream group, only a few keys are remapped which minimizes cache misses in the case of load-balancing cache servers or other applications that accumulate state.

Least Time - For each request, Nginx selects the server with the lowest average latency an the lowest number of active connections, where the lowest average latency is calculated based on which of the following parameters to the least_time directive is included.

- header: Time to receive the first byte from the server
- last_byte: Time to receive the full response from the server
- last_byte inflight: Time to receive the full response from the server, talking into account incomplete requests

Navigating Web Traffic: Nginx, The Art of Load Balancing

Random: each request will be passed to a randomly selected server. If the two parameter is specified, first, NGINX randomly selects two servers talking into account server weights, and then chooses one of these server using the specified method

- least_conn: The least number of active connections

- least _time=header: The least average time to receive the response header from the server

- least_time=last_byte: The least average time to receive the full response from the server

 
The Random load balancing method should be used for distributed environments where multiple load balancers are passing requests to the same set of backends. For environments where the load balancer has a full view of all requests, use other load balancing methods, such as round robin, least connections and least time

Server weights
By defaults, NGINX distributes requests among the servers in the group according to their weights using the Round Robin method. The weight parameter to the server directive sets the weight of a server, the default is 1

In the example, backend1.example.com has weight 5, the other two servers have default weight 1, but the one with IP address 192.0.0.1 is marked as a backup server and does not receive requests unless both of the other servers are unavailable.
With this configuration of weights, out of every 6 requests, 5 are sent to backend1.example.com and 1 to backend2.example.com

 
Server Slow Start
The server slow-start feature prevents a recently recovered server from being overwhelmed by connections, which may time out and cause the server to be marked as failed again.
In NGINX Plus, slow-start allows an upstream server to gradually recover its weight from 0 to its nominal value after it has been recovered or became available. This can be done with the "slow start" parameter to the server directive
Navigating Web Traffic: Nginx, The Art of Load Balancing



Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about Navigating Web Traffic: Nginx, The Art of Load Balancing , please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋  Webzone Tech Tips, all things Tech Tips for web development  - I am Zidane, See you next time soon ✋✋✋✋

ref:
https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-balancer/

🙇🏼 We Appreciate Your Comments and Suggestions - Webzone - all things Tech Tips web development 🙇🏼
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com