Welcome 🎉

logo

ReactLMS

Search
Light Mode
Contact Us

6 min to read

Contact us

No results for your search.
Sorry, an unexpected error occurred

Introduction


In practice, load balancing is a very important part of large projects, helping to route requests to different servers.

To make learning easier, you need to host 3 simple nodeJS servers to easily practice with this topic.


Installation


To implement this feature, we need the help of PM2

To install, run the following command

sudo npm install -g pm2






After installation, run the following command to run 3 nodeJS servers

pm2 start /srv/nglearns/load-balancer-demo/server-1.js
pm2 start /srv/nglearns/load-balancer-demo/server-2.js
pm2 start /srv/nglearns/load-balancer-demo/server-3.js






To check if the servers are running or not, use the following command

pm2 list






Result

# ┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
# id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
# ├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
# │ 0  │ server-1           │ fork     │ 0    │ online    │ 0%       │ 37.4mb   │
# │ 1  │ server-2           │ fork     │ 0    │ online    │ 0%       │ 37.2mb   │
# │ 2  │ server-3           │ fork     │ 0    │ online    │ 0%       │ 37.1mb   │
# └────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘






Make sure your 3 NodeJS servers are set up to run on ports 3001, 3002, 3003


Deployment method


To deploy, let's refer to the following example

events {

}

http {
  upstream backend_servers {
    server localhost:3001;
    server localhost:3002;
    server localhost:3003;
  }

  server {
    listen 80;
    server_name nglearns.test;

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






Here, by using upstream context, we will create a set of servers and configure them to become a single BE

Run the following command to see how they work.

while sleep 0.5; do curl http://nglearns.test; done






Result

# response from server - 2.
# response from server - 3.
# response from server - 1.
# response from server - 2.
# response from server - 3.
# response from server - 1.
# response from server - 2.
# response from server - 3.
# response from server - 1.
# response from server - 2.






You can use the Ctrl + C key combination to stop the process.

In the example above, you can see that nginx automatically routes user requests to each server.

To stop any server process, use the following command

pm2 stop server-1 






By doing this, you have successfully stopped server 1 in the BE system.


Read more
On This Page