Load balance NodeJs Servers Using HAProxy
HAProxy is a free, open-source, high-availability level 7 load balancer and proxy server for TCP and HTTP-based applications. It is used to distribute incoming traffic across a number of servers or resources3 so that a single server does not become overloaded and unable to handle the traffic.
HAProxy sits between the client and the server in a typical setup and acts as a reverse proxy, forwarding traffic from the client to the appropriate server. It can also load balance traffic between multiple servers, ensuring that each server receives a balanced amount of traffic.
HAProxy can be used in various contexts, such as distributing web traffic across a number of web servers or distributing database traffic across a number of database servers. It is highly configurable and can be used to add features such as SSL termination, HTTP compression, and HTTP caching.
What Is Load Balancing?
Load balancing is distributing incoming traffic across a number of servers or resources, aiming to improve a system's performance, reliability, and scalability. It is commonly used in environments where a single server or resource may not be able to handle the incoming traffic, either due to resource constraints or because the traffic is too large.
What Is the Best Approach To Load Balance NodeJs Application?
The sole motive for Load Balancing is to maintain High Availability and maintain Redundancy. The biggest flaw with Nodejs is that is a single-thread application.
You know what it means for a decent machine eg:T3aMedium you have CPU cores sitting there doing nothing.
So let's deploy our `server.js` in production

So What's Happening here?
- We have 2 machines 4 VCPU each. SERVER_1 and SERVER_2
- As we have 4 Virtual Cores on each machine we are running 3 instances of server 2 leaving 1 core free for System processes
- Server 1 is running 2 instances of node with 1 core for HapProxy and 1 for system processes
- The Instances Running on Server 1 are backup servers and the ones on Server2 are production instances
Let's dive into Config of HAProxy
Here is an example HAProxy configuration that sets up a load balancer with three production servers and two backup servers, using the round-robin algorithm:
Copy code
global
maxconn 4096
log /dev/log local0
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
frontend http-in
bind *:80
default_backend servers
# Let's Assume
# Ip for server 2 is 10.10.0.1
backend servers
balance roundrobin
option httpchk GET /healthcheck.html
server server1 10.10.0.1:4001 check
server server2 10.10.0.1:4002 check
server server2 10.10.0.1:4003 check
server server3 127.0.0.1:4001 check backup
server server4 127.0.0.1:4002 check backup
Why are we doing all this?
Load balancers are commonly used in web server environments to improve the performance, reliability, and scalability of the system. Some specific situations where load balancers may be useful for web servers include:
-
High traffic: If your web server receives a lot of traffic, a load balancer can help distribute the traffic across multiple servers, improving the overall performance of the system.
-
Redundancy: Load balancers can be used to provide redundancy for your web servers, ensuring that the system remains available even if one of the servers goes down.
-
Scaling: If your web server needs to scale to meet the demands of an increasing number of users, a load balancer can help distribute the traffic across multiple servers, allowing you to add or remove servers as needed.
-
Geographic distribution: If your web server needs to serve users in different geographic regions, a load balancer can be used to route traffic to the nearest server, improving the performance for users in that region.
Overall, load balancers can be an important tool for improving the performance, reliability, and scalability of web server environments, and are commonly used in high-traffic websites and web-based applications.