Official Power Up Hosting Blog

Everything about Linux, Windows, and hosting ;)

Selvakumar
Author

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Share


Our Newsletter


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Tags


Twitter


Official Power Up Hosting Blog

(Guide) Haproxy Load Balancing Algorithms and Methods

SelvakumarSelvakumar

You might all have a question what is HAproxy, for you here I am going to explain what is HAproxy and the HAproxy load balancing algorithm used in it.

If you don't know what is meant by load balancing, refer here.

haproxy load balancing alogrithms

HAProxy is an open source software used for TCP/HTTP load balancing.

It runs on Linux, FreeBSD, and Solaris. It is famous and widely used for its ability to keeps servers up by distributing the load across multiple servers.

The biggest internet companies Airbnb, Alibaba, GITHUB, Imgur, Instagram, Reddit, StackOverflow, Tumblr, Twitter, and Vimeo, have been using HAProxy.

A lot of terminologies used in the load balancing.

Here, Let us look at the following three

  • ACL
  • Front End
  • Back End

Access Control List

ACL contains a set of rules which has to be checked so that it can carry out some action.

The action can be blocking some requests and selecting the server to forward the request based on the conditions.

The best thing here is, the ACL applies to all traffic. Here is the ACL’s basic part

entry <ACL_rule_name>
{

If {
<match-conditions>:
}
then
{
<action>;
<action modifiers>;
}
}

The ACL increases the flexibility of the traffic forwarding based on various factors such as pattern matching and some connections to the back end.

The ACL is the keyword which is always used to define the rule.

The pattern will be like

acl <aclname> <criterion> [flags] [operator] <value>

Back End

Back end is the group of servers that we have established for load balancers. Backend setup is defined in the back end section of HAproxy.

The back end contains the IP address and server. The back end has to choose the load balancing algorithm for effective processing of the requests.

You can add more servers to the back end so that the load processing ability will increase. It is better to have more than a single server so that the load can be distributed across multiple servers rather than pushing one server to respond beyond its capability.

Front End

The front end in the HAproxy configuration how the requests have to be forwarded to the back-end servers with ACL. The Front end is defined in the front end configuration of HAproxy.

The definition contains the set of IP address and ports, ACLs and the back end. The link here will show you how to configure HAproxy.

Load Balancing methods and techniques

There are two types in load balancing. They are Layer 4 and Layer 7 load balancing. But here I have to mention one more thing that is No load balancing.

It cannot be categorized into load balancing types. However, I like to mention it here.

1. No Load Balancing

In No load balancing status, you won't have any load balancer between the internet and web server.

Let us you have website mydomain.com and it hosted on a web server.

Usually, for the small site which serves few user base won't have any load balancer. In that case, it would be expensive.

But, if the site started to get more traffic, the web server needs to serve the huge user base.

Since it does not have any load balancer, the server will receive a high number of requests.

Due to the load, it will become unable to serve the more user.

You can’t access the website easily at this stage, and the user who is all trying to access the site will face slow loading or no response.

2. Layer 4 load balancing

It is the simplest form of load balancing which is also called as transport-layer load balancing (Transport layer is the 4th Layer in the OSI model).

It is popular due to its ability to distribute the load across multiple servers.

The Load balancing here is depending on two things. One is IP range/domain and the port.

If a user accesses the mydomain.com/blog, the load balancer will send the request to the back-end server which processes the requests for the mydomain.com on port 80.

3. Layer 7 Load Balancing

Layer 7 load balancing is the advanced technique which is more effective than the layer 4 load balancing.

The specialty of this load balancing is it forwards the requests based on the content which the user requested.

Let us say; there are two requests. One is for www.mydomain.com, and the other is for mydomain.com/blog.

Here the Load balancer will send the requests to the appropriate web servers which handle the specified requests.

Now all the server has access to same content so that any server can respond to the user request.

Here the user can run multiple web servers on the same domain and port.

Example code for the front end of HAproxy configuration. HAproxy GUI is easy to use.

frontend http
bind *:80
mode http acl url_blog path_beg /blog
use_backend blog-backend
if url_blog default_backend web-backend

HAproxy Sticky sessions

Sticky sessions are used in the application load balancing method. Sometimes a user has to be continuously connected to the same backend server.

This can be done through the remembering the server which is connected to that particular client. The load balancer has to remember the server for the specific period.

So, the connection will continue until the client stops sending the requests.

Health Checkup

HAproxy health check is checking whether a back-end server is responding or not.

The test is carried out by simply sending a TCP request and find whether the server listens to the specific port and IP.

If there is no response from the server, the load balancer forwards the requests to another server which is healthy.

The back-end server does not receive any requests until it becomes healthy.

If no servers are healthy, the incoming requests to the load balancers won't be processed. At Least, one server should be healthy to process the request.

HAproxy Load Balancing Algorithms

HAproxy allows you to use various load balancing algorithms in it.

The algorithm selection always takes place major role which decides the selection of services.

You have to choose the algorithms according to your requirement.

Here we discuss the most used algorithms.

I will discuss only a few if you want to know the very detailed list of load balancing algorithms read here.

Round Robin

The algorithm chooses the server sequentially in the list. Once it reaches the end of the server, the algorithm forwards the new request to the first server in the list. There are two types in the round robin algorithm. They are

Weighted round robin

The weighted round robin uses the weight allocation to the server to forward the request.

Dynamic round robin algorithm

This algorithm uses the real time updated weight list of the server to forward the request.

Least connection algorithm

This algorithm selects the server with few active transactions and then forwards the user request to the back end.

Source

This algorithm selects the server based on the source IP address using the hash to connect it to the matching server.

High Availability

To overcome the problem of the server overloading, we are using the many backend servers to balance the load.

But what if the load balancer is getting crowded with a lot of requests. They are also device like servers.

When load balancers receive a lot of requests, it becomes slow.

The result, the end user, will receive the response with the delay in time.

To solve this issue, we will use another load balancer which we can call as the secondary load balancer.

If the primary load balancer is getting crowded, the second load balancer will come in rescue.

The health check also performed between the load balancers. They check the health of the each other.

The primary load balancer will be in active mode, and the secondary one will be in passive mode.

You can add more load balancers in the passive mode. There, the failover mechanism switches the load balancers and forwards the request to the correct server.

This is how the high availability of the load balancers are maintained.

Conclusion

I hope this article helps you to know more about the HAproxy and the load balancing algorithms.

For more details read the HAproxy documentation.HAproxy download link is available on their official site.

Now you should have to know about the load balancing techniques.

This article will help you in configuring load balancers for your server.

If you have any questions then kindly leave your comment below, and if you enjoyed reading the article then please do share on your preferred social media network..

Selvakumar
Author

Selvakumar

I am an Online Marketer and technology lover. I like to learn new things and share that with people.

Comments