What is HAProxy Load Balancer and how does it work?
Traffic spikes can be a challenge on your hosting infrastructure, but also a fantastic opportunity for growth.
At first you’ll probably welcome a sudden increase in traffic. It indicates that your products and marketing efforts are succeeding in bringing new visitors to your website. Ultimately you want to gain new customers, and an increase in traffic to your website can only aid that.
These promising spikes in traffic however, can quickly become a problem. Without the correct infrastructure in place, unprecedented spikes in traffic can compromise the security of your server, the performance of your website, and even turn users away from your website.
Sudden increases in traffic doesn’t have to cause you headaches though. Thankfully there are tried-and-true solutions that are designed to handle exactly this, called load balancers.
In this blog post we’ll be going over the fundamentals of the different types of load balancers. We’ll also dive into the powerful open-source load balancing software, HAProxy, and then show you how to install it yourself.
What is a load balancer?
At its core a load balancer is a workload distributor that ensures the uptime, performance, and availability of your servers, applications, and websites during traffic spikes.
Or in more technical terms, load balancers are designed to optimise performance by distributing incoming tasks across multiple computing resources, in order to prevent any one resource from becoming overloaded.
The idea is that incoming traffic is spread evenly over multiple resources and processed on each resource, rather than being handled by the single server. These resources can be computer clusters, servers, disk drives, CPUs, or network links.
Using a load balancer is very important if you have a website or application that requires constant uptime, while also experiencing high or unpredictable levels of traffic. Load balancers not only help to keep your servers, applications, and website online during traffic spikes; they also improve your server’s performance by splitting the workload into two or more servers.
Types of Load Balancing
Load balancing can be implemented in several different ways:
- Hardware
- DNS
- Software
It’s important to get an idea how each of these methods works before looking at HAProxy, in order to understand when each method should be used.
Hardware Load Balancing
Hardware-based load balancing solutions are, as the name suggests, load balancers that utilize a dedicated hardware device to handle traffic distribution. When the right hardware and optimisations are used, this can be the most reliable method of load balancing.
Most hardware load balancers are simply servers running linux distributions, that distributes load between different servers. Sometimes these servers will be equipped with management or configuration tools, to simplify the server administration.
One of the reasons hardware-based load balancing has fallen in popularity when compared to software load balancing is the cost – as you need to purchase dedicated machines, it can prove to be too expensive for many.
DNS Load Balancing
DNS, the system that allows us to access websites over the internet, has the capability to load balance traffic over multiple servers. With DNS load balancers, the system uses a round robin algorithm to determine where to distribute traffic to. This allows for traffic distribution over multiple servers easily, although you have no control over the balancing algorithm being used.
Another major downside to DNS load balancers is that reliability can suffer considerably, if there are any network outages or errors. DNS doesn’t check for any routing issues on the destination IP address, therefore it will continue to direct traffic to that destination for some time even if it’s unavailable.
Software Load Balancing
Software-based load balancing solutions is a reliable and powerful method of distributing load between multiple servers.
With this type of load balancing, a piece of software is responsible for handling the balancing of requests, usually on a Linux platform, with an array of different algorithms used for server and resource allocation. These load balancers are usually installed directly on to a server, but can also be sold as a software-as-a-service package.
Some of the most popular software load balancers are Nginx, Neutrino, Avi Vantage Software Load Balancer, and HAProxy – which we’re going to look at now!
Layer 4 vs. Layer 7 Load Balancing
HAProxy allows for load balancing with two different network layers: Layer 4 (Transport Layer), and Layer 7 (Application Layer).
These two layers operate differently when it comes to load balancing, and are intended for different purposes. Let’s take a look at how each type works.
Layer 4 Load Balancing
Load balancing over layer 4, the transport layer, is the simplest way of balancing network traffic to multiple servers. Here the load balancer only makes limited routing decisions by examining the first few packets in the transmission control protocol (TCP) stream.
This method forwards user traffic based on the IP range and port number. For example, if a request is received for http://yourdomain.com/example, the traffic is forwarded to the backend that handles all of the requests for yourdomain.com on port 80 (the HTTP port).
With layer 4 load balancing, traffic is routed based on networking information included with the information packets like ports and protocols – without actually inspecting the contents of the request itself. As a result, traffic can be distributed quickly and efficiently, since the contents of the traffic is neither decrypted or inspected on either end.
A major downside to layer 4 load balancing is it isn’t capable of routing in a more “smart” manner. As the traffic isn’t inspected, it can’t be forwarded to certain destinations based on the content, or any other criteria beyond simple algorithms like round-robin routing.
Layer 7 Load Balancing
Load balancing over layer 7, the application layer, is a more complex method that makes routing decisions based on the actual content within the incoming message.
Rather than simply forwarding the traffic to the backend without reading it, a layer 7 load balancer terminates network traffic, performs any necessary decryption, inspects the content of the message, and then makes routing decisions based on said content. Afterwards, it creates a new TCP connection to the relevant upstream server and then writes the request to that server.
Using these application-aware networking methods, the load balancer can make more intelligent load balancing decisions and even content optimisations. Another major benefit of using layer 7 load balancing is that content caching can be used, where frequently accessed items are held in memory for quicker access. Overall layer 7 can offer more extensive functionality, although it’s not without its downsides.
As decryption is required, a slight performance penalty can be expected. This type of load balancing is also typically more expensive than the more basic layer 4 load balancing methods.
What is HAProxy?
HAProxy stands for High Availability Proxy, and is used for load balancing with HTTP and TCP-based applications. In recent years it’s become the standard for open-source load balancing, due to it’s high level of performance and reliability. HAProxy is completely free and comes bundled with most Linux distributions.
Introduced officially as HAProxy 1.0 back in 2001, it has evolved from being a basic tool used for offloading traffic from hardware load balancers, to a comprehensive load balancer that many consider to be a crucial part of their network infrastructure.
What is HAProxy used for?
Many high-profile businesses use HAProxy to ensure reliability across their websites and services, and it’s particularly suited for very high traffic websites. While it can be used for a variety of things, it’s most commonly used to split workload across multiple servers, such as web servers, application servers, and database servers.
Upon configuring HAProxy, users can expect a significant improvement to their application or website in terms of performance, reliability, and efficiency. Typically response times will greatly reduce, throughput will increase, and workload will be processed more efficiently.
Next let’s take a look at how HAProxy works, and the different algorithms it uses to distribute traffic.
The HAProxy Algorithms
A load balancing algorithm is the process that determines which backend server will be selected to send traffic to, when load balancing. HAProxy provides many different algorithm options, with different recommended uses.
As well as specifying the load balancing algorithm, servers can be assigned a weight parameter, which changes how frequently a server is selected compared to other servers.
Since HAProxy offers so many different load balancing algorithms, we’ll only be covering a few of the most commonly used ones here. To read more about all of the algorithms HAProxy has to offer, check the official HAProxy configuration manual.
Some of the most commonly used algorithms are:
Round Robin: The most popular load balancing algorithm with HAProxy, Round Robin selects servers in turns according to their assigned weight. As the default algorithm, it provides the smoothest out-of-the-box load balancing solution, as the processing time is equally distributed between servers. Being a dynamic algorithm also means that server weights can be adjusted on the go.
Static Round Robin: This is functionally the same as Round Robin, except that you cannot change server weights while the load balancer is online.
Source: In this algorithm, the load balancer selects which server to use based on a hash of the source IP address (in other words, the user’s IP address). Doing so ensures that a user will always connect to the same server, provided no servers are added or enter a down state. If the number of running servers changes, then the hash result changes and clients are then directed to a different server. This algorithm is also static by default.
Least Connections: As the name suggests, this algorithm selects the server with the least number of active connections, with the backend servers also being rotated in a dynamic round-robin fashion. This type of load balancing is more suited to very long sessions, such as with SQL or LDAP – but it’s not as suitable for shorter sessions like with HTTP.
Additional Features
Health Checks
HAProxy uses a helpful feature called health checks to determine if a backend server is available to process requests, before routing traffic to it. Doing so avoids needing to manually remove a server from the backend, if it becomes unavailable.
The default health check is to attempt to establish a TCP connection to the server. In other words, it checks if the backend server is listening on the configured IP address and port.
In the case that a server fails a health check, and is therefore unable to process requests, it’s automatically disabled on the backend and as a result, traffic won’t be forwarded to it until it passes a health check. If for any reason all of the servers in the backend fail, the service will become unavailable until at least one of those backend servers become responsive again.
Sticky Sessions
One problem with using a load balancer is that by default, traffics bounces from server to server. If the web server is using the default PHP session handling, then all of the session data is saved to a file in a temporary location on that web server. So, if a user logs into server 1, then their session data is saved to a file on server 1. But if the load balancer distributes their next request to server 2, then server 2 will be unable to read their session data, and as a result a new session is created.
This happens because server 2 has no way of accessing the session data on server 1, and the web servers aren’t synchronized. Ideally you want users to be connected to the same backend server, as sessions are an important part of your website’s usability. Especially with eCommerce sites – you don’t want your users carts to be erased!
Thankfully HAProxy provides a solution to this with sticky sessions. When sticky sessions are enabled, users are “stuck” to a certain backend web server until the cookie for the sticky session has expired. Effectively, once a user connects to a backend web server, sticky sessions allows the load balancer to ensure that the next time the user opens the website, they’ll connect to the same backend server.
In Conclusion
HAProxy stands out as one of the most reliable, high-performing, and flexible load balancers available. Its inclusion in most Linux distributions makes it an accessible and powerful option for managing traffic and ensuring optimal performance for your applications or websites. With HAProxy, you get an all-in-one solution for load balancing that can handle even the most demanding environments.
At UKHost4u, we offer a variety of cloud solutions designed to meet the needs of businesses of all sizes, whether you’re running a simple website or a complex application. Our specialized load balancing solutions ensure that your website’s performance is optimized, providing scalability and reliability as your business grows.
Explore our range of cloud hosting services and learn how our solutions can help your business thrive. If you have any questions, don’t hesitate to reach out via live chat or submit a support ticket, and our team will be happy to assist you!