## Table of Contents
– [HAProxy and Load Balancing Concepts](#haproxy-and-load-balancing-concepts
– [HAProxy Terminology](#haproxy-terminology
– [Types of Load Balancing](#types-of-load-balancing
– [Load Balancing Algorithms](#load-balancing-algorithms
– [Sticky Sessions](#sticky-sessions
– [Health Check](#health-check
– [High Availability](#high-availability
In this guide, we will give you an overview of HAProxy, HAProxy terminology, types of load balancing, and how HAProxy might be used to improve the performance and reliability of a web server.
[HAProxy](http://www.haproxy.org/ is an acronym for High Availability proxy. It is free, open-source software that provides a proxy server and high availability load balancer for TCP and HTTP based web applications. It is used to improve the performance of a server by distributing the workload across multiple servers. It is particularly suitable for high-profile websites and used by very high traffic websites including Twitter, Instagram, GoDaddy, Reddit etc.
## HAProxy and Load Balancing Concepts
## HAProxy Terminology
When you are working with HAProxy, you need to understand a few terms and concepts. So, we have given the definitions of some of the key terms below.
## Access Control List (ACL
Access Control Lists (ACLs are used to test a condition and take an action based on the test result. The primary purpose of using ACL is to provide a flexible solution to perform content switching and make a decision based on the content extracted from the request. It allows flexible network traffic forwarding based on various methods like pattern matching, extracting data from a stream, and the number of connections to a backend.
acl url_services path_beg /services
The ACL is matched if the path of the request begins with /services.
A backend describes a set of servers that process the forward incoming connections. Backends are been in the backend part of the HAProxy and consist of load balancing algorithms and a database of servers and ports.
The basic syntax of a backend is:
Here is an example of a backend configuration with all the details filled in.
server appserver1 184.108.40.206:80 check
server appserver2 220.127.116.11:80 check
In the above example, appbackend is the name of the backend to which Access Control List (ACL will forward the request, roundrobin is the name of the load balancing algorithm, appserver1 and appserver2 are the server names, 18.104.22.168 and 22.214.171.124 are server IP addresses, 80 is the port number, and check is to check the health of the server.
A frontend describes a set of listening sockets that process the client requests. Frontends are been in the frontend section of the HAProxy configuration and consist of IP addresses, ports, Access Control Lists (ACLs, and use_backend rules.
The basic syntax of a frontend is:
Here is an example of a frontend configuration with all the details filled in.
acl acl_app path_sub app
use_backend appbackend if acl_app
In the above example, frontend listens on port 80 to all the interfaces. If the substring app present in the URL, the request will be forwarded to appbackend.
## Types of Load Balancing
## Load Balancing
Distributing the incoming networking traffic effectively across multiple backend servers is known as load balancing. Below are the essential basic types of load balancing.
## No Load Balancing
As the name says, it’s a web application environment with no load balance.
In the above diagram, a user directly connects to a web server which has no load balancing. If the web server breaks down, the user can not access the web server. And, if the number of users is trying to access the web server all at a time, they may experience a slow response and not access the server because the web server has no load balancing.
## Layer 4 Load Balancing
Layer 4 Load Balancing is also known as Transport Layer Load Balancing. It has a reputation for its way of balancing the network traffic load with multiple web servers. It is based on IP address and port numbers. For example, when a user requests for domain.com/blog, the traffic will be sent to the backend that manages the requests for the respective domain on port 80.
In the above diagram, a user connects to a load balancer and then the request is sent to the web servers. The selected web server responds and sends back the content to the request immediately. Both the web servers have the same database to avoid sending the inappropriate content to the user.
## Layer 7 Load Balancing
Layer 7 Load Balancing is also known as Application Layer Load Balancing. It has a reputation for a better-sophisticated way of balancing the network traffic load than Layer 4 load balancing. It is based on the content of the requests i.e., a request is sent to the web servers based on the content of the request. It is a beneficial and upgraded way of balancing the load because a user can use multiple web servers on the same domain and port.
## Load Balancing Algorithms
Load balancing algorithms play a crucial role to determine which server will take place during load balancing. There are many algorithms to load balance the requests of a user across web server pools. HAProxy offers many algorithm options. Besides a load balancing algorithm, servers are allocated with a weight parameter to manage how often a web server is chosen compared to other web servers.
Below are a few commonly used algorithms.
This is the default algorithm to select servers in turns.
This algorithm selects a web server with the least number of connections among all the web servers. It is highly recommended for more extended sessions.
This algorithm selects a web server based on a user IP address or hash of the source IP address to make sure the user is connecting to the same server.
## Sticky Sessions
Sticky sessions instruct a load balancer to process the request of a user with the same server that processed the previous request of the same user.
## Health Check
HAProxy uses health checks to determine if a backend is available to process a request or not. This avoids removing a server manually from the backend if it is unavailable. The default health check is to try to establish a TCP connection to check if a backend server is available or not.
If a web server fails a health check, it can’t process the requests and will be disabled in the backend. Considering the worst case if all the web servers fail, the service will be unavailable until at least one of the web servers becomes healthy again.
## High Availability
The load balancers we mentioned earlier, Layer 4 and Layer 5, works based on the method of directing traffic to a web server among many web servers. In both the methods, if a load balancer fails, it can’t process the requests and the end user can’t get the content at all. To overcome this issue, a high availability configuration is required to eliminate the single point of load balancer failure.
In the above diagram, it is explicitly
described the functionality of two load balancers (you can add multiple load balancers. Both the load balancers are in action, but one is in active mode and the other is in passive mode. If you have added multiple load balancers, only one will be in active mode and the remaining will be in passive mode. When a user accesses a website, the request will go the active load balancer through the external IP address. If the active load balancer fails, the failover mechanism identifies it and automatically reassigns the IP address to one of the passive load balancers.
In this article, we have given the introduction to HAProxy and load balancing concepts which can help you improve the performance and reliability of your web server environment.