What is load balancer? Create your own using ngnix.

Mohasin Hossain
3 min readAug 19, 2022

--

In this tutorial you are going know what is load balancer, how it works & how you can create your won load balancer using nginx.

What is load balancer

Load balancer is a way of processing incoming multiple request at a time in your server. Load balancer efficiently distributing incoming request across a group of backend servers.

Why load balancer needed

Think you have thousands or millions request in your server at a time and your server failed to handle all request at a time, in this point you need load balancer to handle large amount of request without overloading or down your server.

How load balancer works

Load balancer is kind of sitting in front of your servers and routing client requests across all servers. like you have your main domain or server. your main domain server works as a load balancer. all load balancing login handle by your main domain server. and you have some proxy server. say you have 3 proxy server. so user one request to your main server and main server pass the request to proxy server one. in same time user two request your main server and main server pass the request proxy server two. passing request to proxy server depends on how you handle it or write logic in your load balancer, it can be handle some way like:

  • Round Robin — Requests are distributed across the group of servers sequentially.
  • Least Connections — A new request is sent to the server with the fewest current connections to clients. The relative computing capacity of each server is factored into determining which one has the least connections.
  • Least Time — Sends requests to the server selected by a formula that combines the fastest response time and fewest active connections.
  • Hash — Distributes requests based on a key you define, such as the client IP address or the request URL.
  • IP Hash — The IP address of the client is used to determine which server receives the request.
  • Random with Two Choices — Picks two servers at random and sends the request to the
    one that is selected by then applying the Least Connections algorithm.
  • There are some other option like server weight or server health.

Let’s create a load balancer using ngnix

For this we have a main domain server and two proxy server. First configure main server for load balancer and two proxy server for our project. and all proxy server hold the same project. so our main server / load balancer configuration will be like this.

Benefits of load balancing

  • Reduced downtime
  • Scalable
  • Redundancy
  • Flexibility
  • Efficiency

Please feel free to leave a comment if you have any feedback, questions or want me to write about another PHP/Laravel related topic.

--

--

Mohasin Hossain
Mohasin Hossain

Written by Mohasin Hossain

Senior Software Engineer | Mentor @ADPList | Backend focused | PHP, JavaScript, Laravel, Vue.js, Nuxt.js, MySQL, TDD, CI/CD, Docker, Linux

Responses (1)