Internative Logo

What Is HAProxy? Load Balancing and Reverse Proxy Explained

What Is HAProxy? Load Balancing and Reverse Proxy Explained

A single application server is a single point of failure. When it slows down, every user waits; when it goes down for an update or a crash, the service is gone. The usual answer is to run several servers and put something in front of them that spreads requests, notices when a server is unhealthy and quietly stops sending it traffic. HAProxy is one of the most widely used pieces of software for that job, and it has been doing it for more than two decades.

This article explains what HAProxy is, how it routes traffic, where it is used, when it is the right tool and how it compares with NGINX and other load balancers.

What is HAProxy?

HAProxy, short for High Availability Proxy, is a free, open-source load balancer and reverse proxy for TCP and HTTP-based applications. It sits between clients and your servers, receives every connection and decides which backend server should handle it. It is written in C, runs mainly on Linux, and is released under the GNU General Public License version 2.

HAProxy was written by Willy Tarreau, who is also known for his work on the Linux kernel. According to the project's own changelog, the first lines of code were written in November 2000 and version 1.0.0 was released on 16 December 2001. Its first serious production use was offloading traffic from hardware load balancers that could not keep up. HAProxy Technologies, the company behind the project, was founded in 2013 and sells HAProxy Enterprise and the ALOHA appliance, while the community edition stays open source.

Over the years the project added native SSL/TLS, HTTP/2 on both the client and server sides, multithreading and, from version 2.6, HTTP/3 over QUIC. Its reputation rests less on features than on behaviour under load: it handles very large numbers of concurrent connections with predictable memory use and latency.

How HAProxy works

Frontends and backends. A HAProxy configuration has two main building blocks. A frontend defines where traffic arrives: which IP addresses and ports to listen on, and which certificates to use. A backend defines a pool of servers that can handle it. Rules connect the two.

Layer 4 and Layer 7. In TCP mode, HAProxy works at Layer 4: it forwards connections based on IP address and port without reading their content, which suits databases, message brokers and any non-HTTP protocol. In HTTP mode, it works at Layer 7: it reads the request and can route on the host name, path, headers or cookies, rewrite requests and responses, and add or remove headers.

Load-balancing algorithms. Each backend chooses how to spread traffic: roundrobin takes servers in turn, leastconn sends new connections to the server with the fewest active ones, and hash-based methods keep the same client or URL on the same server.

Health checks. HAProxy checks backend servers continuously, with a TCP connect or an HTTP request to a health endpoint. A server that fails is taken out of rotation and returns automatically when it recovers.

ACLs and stick tables. Access control lists express conditions, such as "the path starts with /api" or "this IP is on a deny list". Stick tables keep counters per client in memory, which is how HAProxy enforces rate limits and blocks abusive traffic without an external store.

Key features

  • SSL/TLS termination, so certificates and encryption are handled in one place and backends receive plain traffic, or re-encrypted traffic when required.
  • HTTP/1.1, HTTP/2 and HTTP/3 support, plus WebSocket and gRPC proxying.
  • Session persistence with cookies or source hashing for applications that keep state on the server.
  • Observability: a built-in statistics page, detailed logs and a native Prometheus exporter for Prometheus and Grafana dashboards.
  • Runtime API to drain, disable or add servers and change weights without a reload.
  • Hitless reloads, so configuration changes do not drop existing connections.
  • Kubernetes ingress: HAProxy Technologies maintains an ingress controller for Kubernetes clusters.

Where HAProxy is used

  • In front of web applications and APIs, spreading traffic across several application servers and taking failed ones out of rotation.
  • TLS edge: terminating HTTPS for many domains and routing each to the right service.
  • Database and queue traffic in TCP mode, for example sending writes to a primary and reads to replicas.
  • Microservices, routing by path or host name to different backend services.
  • Highly available pairs: two HAProxy nodes sharing a virtual IP through Keepalived, so the load balancer itself is not a single point of failure.
  • Blue-green and canary releases, by shifting weights between an old and a new pool of servers.

When to choose HAProxy, and when not to

Choose HAProxy when:

  • You need a dedicated, high-performance load balancer for HTTP or TCP traffic in your own data centre or on virtual machines.
  • Health checks, failover and detailed traffic control matter more than serving files.
  • You want advanced routing, rate limiting and connection handling in a single process with a readable configuration.
  • You run hybrid or on-premise infrastructure where a cloud provider's managed load balancer is not available.

Think twice when:

  • You also need a web server for static files or a simple application server. NGINX does both in one component.
  • You run entirely on one cloud and its managed load balancer covers your needs; operating your own adds work without adding value.
  • You need a service mesh with dynamic, API-driven configuration across many services. Envoy-based tools are designed for that.
  • Nobody on the team can own the configuration. HAProxy is reliable, but its configuration language takes time to learn well.

HAProxy and its alternatives

OptionTypeStrengthBest fit
HAProxyLoad balancer and reverse proxyPerformance, health checks, TCP and HTTP controlDedicated load balancing, on-premise and hybrid
NGINXWeb server and reverse proxyStatic files plus proxying in one processWeb serving with load balancing on the side
EnvoyProxy for service meshesDynamic configuration through APIsLarge microservice platforms
TraefikReverse proxy with auto-discoveryReads routes from Docker and Kubernetes labelsSmall container setups that change often
Cloud load balancersManaged serviceNo servers to runWorkloads on a single cloud

The most common comparison is HAProxy vs NGINX. Both are fast, open-source reverse proxies and both balance load. NGINX started as a web server and is often chosen when one component must serve files and proxy requests. HAProxy was built only for proxying and load balancing, and it offers more detailed health checks, connection handling and traffic statistics. Many architectures use both: HAProxy at the edge, NGINX or Apache behind it serving the application.

How we use HAProxy at Internative

We use HAProxy to design resilient, highly available architectures. Its performance, flexibility and stability make it a good fit for load balancing APIs, web applications and distributed backend systems.

If you are planning infrastructure that has to stay up when a server fails, or moving an application to a hybrid setup, our DevOps services and hybrid infrastructure setup teams can help you design it.

Frequently asked questions

What is HAProxy used for?

HAProxy spreads incoming traffic across several servers so that no single server is overloaded and a failed server does not take the service down. It is also used to terminate SSL/TLS, route requests by host name or path, and limit abusive traffic.

Is HAProxy a load balancer?

Yes. HAProxy is a software load balancer and reverse proxy. It can balance traffic at Layer 4 (TCP) for any protocol and at Layer 7 (HTTP), where it can make routing decisions based on the content of each request.

What is the difference between HAProxy and NGINX?

NGINX is a web server that also works as a reverse proxy and load balancer. HAProxy is dedicated to proxying and load balancing. NGINX is convenient when one component must serve static content and proxy requests; HAProxy offers finer control over health checks, connections and traffic statistics.

Does HAProxy terminate SSL?

Yes. HAProxy can terminate SSL/TLS, so clients connect over HTTPS and backends receive unencrypted traffic, which reduces the work on application servers. It can also re-encrypt traffic to the backends or pass encrypted TCP traffic through untouched.

Related services

Where this topic becomes an engagement.

Keep reading

The articles that follow this one.