When you run multiple servers to handle a huge load of incoming requests, you will benefit from using a load balancer....
Vendors provide load balancers as a hardware appliance, but they are also available for free as Linux load balancer software.
A load balancer increases the response time of high-traffic, back-end servers. Typically, organizations use a load balancer as the front end for an application or workload on multiple servers running as the back end. In theory, load balancing could occur for any type of service, but in practice, it typically benefits operations on HTTP and HTTPS servers.
Load balancers introduce a single point of failure, but using them in a high availability (HA) configuration ensures that, if the load balancer goes down, it will start again. Load balancer HA can be organized by an external HA product, such as the generic Linux-based Pacemaker HA. Pacemaker will monitor the load balancer by polling its availability on a regular basis. If it goes down, it will start it again.
Linux load balancer options
Three common Linux load balancer options are Nginx, HAProxy and Linux Virtual Server. Each provide load balancing services and run on top of Linux. The differences between them can be explained with the Open Systems Interconnection (OSI) model in mind.
The OSI model defines network communication in different layers -- the higher the layer, the more specific it is about the type of protocol used. A load balancer that works at a lower OSI layer is more generic because it is protocol-agnostic, and is faster, due to smaller packet overhead. A load balancer that works on a higher layer of OSI can be much more specific, thus offering more features. But because of that, it will also be slower.
The Linux Virtual Server load balancer works at OSI layer 4, so it looks at connections in a generic way and deals with different types of traffic. HAProxy and Nginx both provide HTTP-based load balancing services on OSI layer 7. That means they are excellent options to improve HTTP response times, but you'll have a difficult time load balancing traffic toward other protocols, such as Simple Mail Transfer Protocol, File Transfer Protocol or Secure Shell. However, HAProxy supports load balancing features for MySQL/MariaDB databases, and Nginx also offers some support for email.
That doesn't mean you'll always be better off using HAProxy or Nginx if you need to load balance HTTP packets. When the load balancer operates at a higher level of the OSI model, it also needs to do more work and inspect packets all the way up to the HTTP packet header.
Another difference is that the layer 7 load balancers operate in user space, which makes it more customizable. However, the Linux Virtual Server load balancer works in kernel space, which makes it faster.
HAProxy or Nginx?
As a web server, Nginx is ahead of Apache when it comes to handling high traffic environments; the same is true for Nginx as a Linux load balancer. So if you're expecting a high workload on HTTP, use Nginx instead of HAProxy.
But, Nginx isn't always the best option. The problem with Nginx is that it hasn't been developed to be a load balancer. Nginx is a fast web server, and also a proxy and load balancer. With some items, HAProxy has more developed features to accommodate its operation as a load balancer. For example, an algorithm is used to decide which server receives the packets that are sent. HAProxy has at least eight different algorithms, including common algorithms such as round-robin, static round-robin and least connection. Nginx offers just three different algorithms and for that reason, is not as flexible.
If you want a multi-protocol Linux load balancer that works quickly and is implemented in the Linux kernel, you'll be interested in Linux Virtual Server. If you need a load balancer that is focused on HTTP, and a limited set of other services, you'll need a layer 7 load balancer.
Of these layer 7 load balancers, Nginx performs well under high workloads. HAProxy offers more features, including more options to select the underlying algorithm, making HAProxy the recommended protocol-specific load balancer.
Enterprise load balancing: hardware or software?
Load balancers in active-active clustering
Learn how Linux containers change IT networks