HAProxy

On this page 7

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.

SectionRoleTypical settings
globalProcess-wide behaviorThreads, logging, TLS defaults, limits
defaultsValues inherited by later sectionsMode, timeouts, retries
frontendListening side facing clientsBind address, ACLs, routing rules
backendPool of servers receiving trafficAlgorithm, servers, health checks
listenFrontend and backend combinedSimple services, statistics page
Main sections of an HAProxy configuration

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.

AlgorithmHow it choosesBest suited for
roundrobinRotates through servers, honoring weightsShort, uniform HTTP requests
static-rrRotation without runtime weight changesVery large server farms
leastconnServer with fewest active connectionsLong sessions, databases, WebSocket
sourceHash of the client IP addressBasic client affinity without cookies
uri and hdrHash of the path or a headerCache servers, sharded services
randomRandom pick, optionally best of twoLarge dynamic pools
Common HAProxy balance algorithms

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.

OptionMain strengthMain trade-off
HAProxyDeep L4 and L7 control, high performanceConfiguration requires expertise
NGINXWeb server and proxy in oneFewer load balancing features in free edition
EnvoyDynamic API-driven configurationMore complex to operate standalone
Cloud load balancerFully managed, elastic capacityLess tuning, provider lock-in, usage cost
Hardware applianceDedicated ASICs, vendor supportHigh cost, slower to change
HAProxy and alternative load balancing options

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.

SymptomLikely causeFix
504 errors under loadServer timeout shorter than backend responseAlign timeout server with real latency
Uneven server loadroundrobin with long-lived connectionsSwitch to leastconn
Backend sees proxy IP onlyClient address not forwardedAdd option forwardfor or PROXY protocol
Dead server still receives trafficNo health check definedAdd check and an HTTP check
Connections refused at peaksmaxconn limit too lowRaise limits and file descriptors
Frequent HAProxy problems and remedies

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.