HAProxy (High Availability Proxy) is an open-source load balancer and reverse proxy that distributes TCP and HTTP traffic across a pool of backend servers. In load balancing, it is one of the most widely deployed software solutions, valued for high throughput, low latency and fine-grained control over how each request or connection is routed.
HAProxy runs as a single event-driven process on Linux and other Unix-like systems and can operate at Layer 4 for raw TCP or at Layer 7 for HTTP, where it inspects headers, paths and cookies. It is used in front of websites, APIs, databases and message brokers, and it forms the basis of several commercial appliances and Kubernetes ingress controllers.
How HAProxy processes traffic
HAProxy accepts client connections on frontends, applies rules to each connection or request, and forwards it to a server chosen from a backend. The process uses non-blocking I/O with an event loop and multiple threads, so a single instance can handle hundreds of thousands of concurrent connections on ordinary hardware, depending on TLS usage, request size and rule complexity.
The configuration file is divided into named sections, and the table explains the role of each one.
| Section | Role | Typical settings |
|---|---|---|
| global | Process-wide behavior | Threads, logging, TLS defaults, limits |
| defaults | Values inherited by later sections | Mode, timeouts, retries |
| frontend | Listening side facing clients | Bind address, ACLs, routing rules |
| backend | Pool of servers receiving traffic | Algorithm, servers, health checks |
| listen | Frontend and backend combined | Simple services, statistics page |
Routing decisions rely on access control lists (ACLs), which test properties such as the Host header, the URL path, the source address or a TLS SNI value. A rule such as use_backend api if { path_beg /api } sends matching requests to a dedicated pool, which lets one HAProxy instance serve many applications behind a single IP address.
Load balancing algorithms in HAProxy
HAProxy selects a backend server with a configurable balance algorithm, and the right choice depends on how long requests last and whether clients must return to the same server. The following table summarizes the algorithms used most often.
| Algorithm | How it chooses | Best suited for |
|---|---|---|
| roundrobin | Rotates through servers, honoring weights | Short, uniform HTTP requests |
| static-rr | Rotation without runtime weight changes | Very large server farms |
| leastconn | Server with fewest active connections | Long sessions, databases, WebSocket |
| source | Hash of the client IP address | Basic client affinity without cookies |
| uri and hdr | Hash of the path or a header | Cache servers, sharded services |
| random | Random pick, optionally best of two | Large dynamic pools |
Hash-based algorithms can use consistent hashing through the hash-type consistent option, which limits how many keys move when a server is added or removed. For sticky sessions, HAProxy can also insert a cookie or use stick tables, in-memory tables that remember which server a client used and can be synchronized between peers.
Health checks, high availability and observability
HAProxy continuously tests backend servers with active health checks and removes a failing server from rotation until it recovers. Checks range from a plain TCP connect to an HTTP request that expects a specific status code, and parameters such as inter, fall and rise control how quickly a server is marked down or brought back.
HAProxy itself is usually made redundant by running two or more instances with a floating virtual IP managed by Keepalived and VRRP, or by placing instances behind DNS or anycast. A graceful reload starts a new process with the updated configuration while the old process finishes its existing connections, which supports connection draining without dropped requests.
For visibility, HAProxy offers a built-in statistics page, a runtime API over a Unix socket for changing weights or disabling servers without a reload, detailed per-request logs with timing fields, and a native Prometheus exporter. These logs are often the fastest way to tell whether latency comes from the client, the proxy or the backend.
HAProxy compared with other load balancers
HAProxy competes with other software proxies and with managed cloud services, and each option makes different trade-offs between control, features and operational effort. The comparison below covers the alternatives teams most often evaluate.
| Option | Main strength | Main trade-off |
|---|---|---|
| HAProxy | Deep L4 and L7 control, high performance | Configuration requires expertise |
| NGINX | Web server and proxy in one | Fewer load balancing features in free edition |
| Envoy | Dynamic API-driven configuration | More complex to operate standalone |
| Cloud load balancer | Fully managed, elastic capacity | Less tuning, provider lock-in, usage cost |
| Hardware appliance | Dedicated ASICs, vendor support | High cost, slower to change |
HAProxy is a strong choice when a team needs precise routing logic, predictable performance on its own infrastructure and transparent behavior. A managed cloud service is often simpler when traffic is elastic and the team prefers to avoid running proxy servers.
Configuration pitfalls and practical limits
Most HAProxy incidents come from configuration choices, not from software defects. The table links common symptoms to their usual causes and fixes.
| Symptom | Likely cause | Fix |
|---|---|---|
| 504 errors under load | Server timeout shorter than backend response | Align timeout server with real latency |
| Uneven server load | roundrobin with long-lived connections | Switch to leastconn |
| Backend sees proxy IP only | Client address not forwarded | Add option forwardfor or PROXY protocol |
| Dead server still receives traffic | No health check defined | Add check and an HTTP check |
| Connections refused at peaks | maxconn limit too low | Raise limits and file descriptors |
Performance also has real limits. TLS termination is CPU-intensive, so throughput with HTTPS is far lower than with plain TCP, and capacity planning should be based on measured handshakes per second. Finally, HAProxy is not a web application firewall by default; it can rate-limit and block with stick tables, but deep security inspection requires additional components.
In everyday language
HAProxy works like a traffic officer at a busy intersection. Visitors arrive at one address, and HAProxy quietly sends each of them to one of several servers that can answer, so no single server gets overwhelmed.
It also keeps checking which servers are healthy. If one breaks, HAProxy simply stops sending visitors there until it is fixed, so most people never notice a problem at all.
Seen in the real world
An online ticket seller expects a sudden rush when a popular concert goes on sale. Its operations team places two HAProxy instances with a shared virtual IP in front of eight application servers and three API servers. Requests to /api go to the API pool with the leastconn algorithm, while page requests use roundrobin, and HTTP health checks run every two seconds.
During the sale, traffic jumps to about ten times the normal level, and one application server runs out of memory. HAProxy marks it down within six seconds and spreads its share across the remaining servers. The team uses the runtime API to lower the weight of an overloaded node without a reload, and the sale completes without any visible outage for buyers.