What is Haproxy in Linux

HAProxy (not Proxy HA) stands for High Availability, the Proxy is open source software that is useful to the TCP Load Balancer. Because TCP means HAProxy can be used not only for HTTP but also other protocols. In addition to HAProxy load balancer is also useful as a proxy. Load Balancing means the activity or activities to balance the load so it's not resting on 1 server only. It is easy to imagine that we have more than 1 server (say a webserver), 1 server only able to withstand a load of about 100 connections while we must bear the burden of 200 connections, in this case, we need more than 1 server. Well, to share the load, we need the role of the load balancer.

Before we study the HAProxy, it's good that we understand what the terminology in HAProxy.

ACL/Access Control List
With respect to load balancing, ACLS (s) used to test several conditions and perform actions against the condition. Select the server instance or block the incoming request. The use of ACLS (s) allow us to create a more flexible network traffic forwarding. Example:
acl <name acl> <criterion> [flags] [operator] [<value>] ...
acl url_path_xtra path_beg /xtra_path

The ACL above will comply with a request from a user with the prefix "/xtra_path". For more details check here.

The backend is some server is prepared to accept traffic/request will be forward by HAProxy. HAProxy will determine which server will be the traffic. A backend can have more than one server so that the load of incoming traffic can be divided into multiple servers that are on the backend. And it also enhances the reliability of the system because it has a lot of backend servers may resolve the problem if one server dies or cannot serve the request.

An example of the Backend.
backend web-backend
   balance roundrobin
   server web1 web1.example.com:80 check
   server web2 web2.example.com:80 check
 backend blog-backend
   balance roundrobin
   mode http
   server blog1 blog1.example.com:80 check
   server blog1 blog1.example.com:80 check

balance roundrobin - perform a balancing load with roundrobin algorithm (to be discussed in the next subsection).
mode http  – specify the layer 7 proxy will be used.
Check option at the end of the line the server will do a health check on the server. 

Frontend defines how the request will be forwarded to the Backend. Definition of frontend will consist of:
  • a set of IP address and port (e.g., *: 443 etc.)
  • ACL
  • use_backend, where we will choose which backend will be used (depending on the ACLs)
The configuration of the frontend will be discussed in the next subsection. 

Sort – sorts of Type Load Balancing
Here are the sorts of – kind of the type of load balancing can be done on HAProxy

No Load Balancing
A scheme of a web application without this kind of load balancing

In the example above, the user will be directly connected to the web server and load balancing without altogether. If the server is off (down). Then the user will not be able to access any information or who are on the server. Additionally, when a great many users who access the web server simultaneously, then the burden will make web servers are overloaded so incapable of serving the load. Users can experience the constraints that can be either a slow response or even all can not be connected.

Layer 4 Load Balancing
A load balancing method is very simple, namely doing the Division of traffic load to multiple servers on the backend using Layer 4 or Transport Layer. This Load Balancing will forward traffic from a user based on IP and port range. Example: If someone did request at "http://example.com/path1", then the traffic will be forwarded to the Backend on duty to handle the traffic on port 80 "example.com".

Users who access the load balancer (example.com), which will be forwarded to a group of Backend. Usually a server that resides on the Backend of this group (especially the web) will have in common or identical in configuration, if not, then the user will get the content don't consistently.

Layer 7 Load Balancing
This method is a bit more complicated than in layer 4. Layer 7 allowed us to redirect the request to a specific server that we want. Suppose to/blog path will be directed to a backend to handle the blog. While for others it is served by the backend web-backend. In this mode also allows us to have more than one web application with the same address and port.

In the example above, a request from the user to the domain "example.com/blog" will be redirected to the blog-the backend the backend configuration there is a list of the server that is running the application blog. Whereas the other request will be forwarded to the web-a backend may have other web applications. And both the backend connected on the same database. Example configuration Frontend on HAProxy as follows:
frontend http
bind *:80
mode http

acl url_blog path_beg /blog
use_backend blog-backend if url_blog

default_backend web-backend

On the configuration above, a "Frontend" named after http. Serving the traffic that comes to port 80.

This line "url_blog path_beg acl/blog" will match if the user requested path beginning with/blog

"use_backend blog-backend if url_blog" will use the ACL (the line above it) to do the proxy to the blog-backend

"default_backend web-backend" specifies that traffic (port 80) more will be directed to the web-backend.

Load Balancing Algorithm
Load Balancing algorithm used by HAProxy can determine which server in the Backend to be chosen to serve the request from the user. HAProxy provides several options for these algorithms. In addition to that, the servers of the algorithm can be selected with the parameters of the weight or weighting system to manipulate how often the server gets the request and respond to the user. The author does not address all of that, here, the authors discuss only a few options that are likely to be used.

Round robin is the default option and this algorithm selects servers alternately in sequence so that tends to balance to all of the servers on the Backend.

Selecting a server based on the number of connections that are the least. This is recommended for a long session. But the same Backend server will also be rotated his election as round robin.

Select the server where based on source IP (the IP address of the user) who in hash. This is to ensure that users get the same server.

Sticky Session
Some applications require users to connect to the same server on the Backend. For the purposes of this persistent required that the name Sticky Session with the appsession parameter in the Backend that need it.

Health Check
Health Check systems using HAProxy to determine or detect whether a backend server is ready or available to process a request from a user. This is to avoid removing the server from the Backend manually, if the server does not terserdia (down or unavailable). By default the Health Check done by the TCP connection is made to the server from the Load Balancer. Suppose a Backend will listen on port 80, then the servers that exist in the Backend list checked the availability of port 80 by its HAProxy.

If this check fails, then the server will not be able to serve the request, and automatically will be disable from the Backend. If all the servers on the Backend fails on the check (Health Check), then the service will not be available or unavailable to one of their success on the stage of the cheque.

Other solutions for Load Balancer:
  • Nginx – of course, Open Source software can also serve as a proxy and load balancer. Nginx is also often used in conjunction with HAProxy because its compression and caching capabilities.
  • Linux Virtual Servers – a layer 4 load balancer which is very simple in some linux distributions.

Click here for Comments