Load balancing means splitting up network traffic so that you can distribute it evenly across a group of backend servers. For example, if you run two web servers, both hosting a copy of the same website, then you can balance the traffic across them, sending half to one and half to the other.
The goal of load balancing is to increase the availability of your website or web-based application by routing a portion of requests to each server. If one of your load-balanced servers fails, then the other is still there to handle requests. It doesn’t have to be web-based traffic, either. Load balancing can work for any networked application, such as FTP servers, databases, and cache servers.
The device that performs load balancing is called a load balancer. In the past, load balancers were dedicated physical servers that lived in the datacenter and used specialized components and operating systems, but today they can be software that you install onto a general-purpose server, physical or virtual. HAProxy and HAProxy Enterprise are software load balancers and can be installed onto a variety of Linux operating systems, while HAProxy ALOHA Hardware Load Balancer ships as a rack-mounted, physical server.
Types of load balancers
Two of the most popular types of load balancers are reverse proxies and DNS Round-robin.
A reverse proxy receives a request, then relays it to another server.
For example, if I make a request to a website named example.com, a load balancer would receive the request first. Then, it would choose a web server from a list and pass the request along by opening a connection on the backend to that server. Meanwhile, the load balancer would keep its connection open to me, the client. When the webserver handling the request returned its response to the load balancer, the load balancer would relay it back to me over the original connection. In essence, a load balancer sits between me and the group of web servers.
Aside from using a reverse proxy-style load balancer, you can also use DNS Round-robin to load balance traffic. With this approach, a portion of traffic is sent to different servers by returning different IP addresses for a DNS lookup. For example, when I access haproxy.com, the DNS server may return the address 184.108.40.206. With DNS Round-robin, the next time that I access the same website, I may get a different IP address back, such as 220.127.116.11. By rotating the addresses it returns for a DNS query, the DNS server sends users to different servers.
Reverse proxies can detect changes to servers more quickly than a DNS server can, such as when servers are added or removed, or when a server stops responding. DNS records are often cached by clients or intermediary DNS servers, which means that DNS Round-robin is slower to adapt to changes. However, people often use it as a second tier of load balancing to distribute traffic to multiple data centers.
The benefits of load balancing
Load balancing is important for guaranteeing the high availability of your website or networked application. High availability means that your application is there when your customers need it. Load balancing can also improve the performance of your website by spreading the work across multiple servers so that those servers can operate with optimal conditions. It also increases your capacity, or your ability to serve more customers simultaneously.
An HAProxy load balancer lets you specify different load balancing modes, depending on the type of application you’re serving. Its round-robin mode will send requests to your servers in a rotation and works well when requests take a small and predictable amount of time to handle, such as HTTP requests. However, other modes exist for specialized use cases: Load balancing long-lived connections, such as database queries and WebSocket connections, suits the least-connections mode, which sends a new connection to the server that has the fewest active connections. Caches perform better when the same request hits the same server where the result was previously cached, so URI-hash mode, which remembers which server was used the last time for the requested URL, works well.
Something else to consider is whether you need to inspect the web traffic in order to decide which server should receive the request. For example, you may need to take into account the URL path before deciding which group of servers to use. In that case, you can switch HAProxy into http mode to give it that extra layer of intelligence. Then it can extract other metadata including the HTTP headers, cookies, and method (GET or POST).
HAProxy load balancers can help you solve a number of other problems, such as security and observability. Check out our Solutions pages and watch our Introduction to HAProxy webinar to learn more about load balancing with HAProxy.