
Table of Contents
By Khimananda Oli | Last reviewed: August 2026
When your application traffic outgrows a single server, you need a reliable layer to distribute requests efficiently and handle failures gracefully. This HAProxy Load Balancing Guide provides the exact configuration patterns I use in production to achieve high availability, from basic Layer 4 TCP forwarding to advanced Layer 7 HTTP routing with TLS termination. Whether you are building infrastructure for a Nepal-based e-commerce platform or a global SaaS, mastering these fundamentals ensures your system remains responsive under load.
How do you configure basic HAProxy load balancing for production?
Before diving into complex routing rules, you must establish a solid baseline configuration. A common mistake in tutorials is omitting connection limits and timeouts, which leads to resource exhaustion during traffic spikes. In practice, every production haproxy.cfg should explicitly define global tuning parameters, default safety valves, and backend server definitions.
The following configuration establishes a secure, performant foundation. Note the explicit timeout values; never rely on defaults for production workloads.
global
log /dev/log local0 info
maxconn 4096
tune.ssl.default-dh-param 2048
ssl-default-bind-options no-sslv3 no-tlsv10 no-tlsv11
ssl-default-bind-ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384
defaults
mode http
log global
option httplog
option dontlognull
timeout connect 5s
timeout client 30s
timeout server 30s
retries 3
frontend https_front
bind *:443 ssl crt /etc/haproxy/certs/site.pem
default_backend app_servers
backend app_servers
balance roundrobin
option httpchk GET /healthz HTTP/1.1\r\nHost:\ localhost
server app1 10.0.1.10:8080 check inter 3s fall 3 rise 2
server app2 10.0.1.11:8080 check inter 3s fall 3 rise 2
server app3 10.0.1.12:8080 check inter 3s fall 3 rise 2 backup This configuration enforces modern TLS standards, sets aggressive but safe timeouts to prevent zombie connections, and defines health checks that verify application responsiveness rather than just TCP port availability. For teams managing database backends alongside web servers, understanding MySQL performance tuning complements this setup by ensuring your balanced traffic doesn't hit an unoptimized data layer.
Which HAProxy load balancing algorithm should you choose?
Selecting the right algorithm is not academic; it directly impacts latency distribution and backend utilization. HAProxy offers several strategies, but three dominate production use cases. Understanding when to apply each prevents the common pitfall of using round-robin for heterogeneous workloads.
- roundrobin: Distributes requests sequentially across servers. Best for stateless applications with uniform request costs and identical hardware specifications. It has near-zero overhead and works well for static content or microservices with predictable latency.
- leastconn: Routes new connections to the server with the fewest active connections. Essential for long-lived requests like WebSocket connections, file uploads, or API endpoints with variable processing times. This prevents slow requests from piling up on a single node while others sit idle.
- source: Hashes the client IP to select a backend server, providing session persistence without cookies. Useful for legacy applications that store session state locally, though sticky sessions via cookies are generally preferred for modern apps. Be cautious with this behind NATs or CDNs where source IPs represent many users.
| Algorithm | Best Use Case | Persistence | Overhead | Caveat |
|---|---|---|---|---|
| roundrobin | Stateless APIs, static assets | None | Minimal | Ignores server load variance |
| leastconn | WebSockets, variable-latency APIs | None | Low | Slightly higher CPU for tracking |
| source | Legacy session-state apps | IP-based | Moderate | Breaks behind NAT/proxies |
| hdr | Tenant-based routing | Header-based | Moderate | Requires consistent header presence |
In my experience deploying services across Nepal and international regions, leastconn is the safest default for most web applications because it naturally adapts to varying request complexity. Only switch to roundrobin after profiling confirms uniform request costs.
How do you implement reliable health checks and failover?
A load balancer that routes traffic to dead servers is worse than useless—it creates intermittent failures that are notoriously difficult to debug. Active health checks are non-negotiable in production. Passive observation alone cannot distinguish between a server that accepts TCP connections but returns 500 errors and one that is genuinely healthy.
The key parameters in health check configuration deserve careful tuning:
- inter: The interval between checks. Three seconds balances detection speed against backend overhead. For databases or heavy services, increase to 5–10 seconds.
- fall: Consecutive failures required to mark a server DOWN. Setting this to 3 prevents flapping from transient network blips.
- rise: Consecutive successes needed to mark a recovered server UP again. A value of 2 ensures the service is stable before accepting traffic.
Always implement a dedicated health endpoint in your application that verifies critical dependencies. A /healthz that returns 200 while the database is unreachable provides false confidence. For teams running PostgreSQL backends, integrating checks with PostgreSQL administration essentials ensures your health endpoint actually validates database connectivity and replication lag.
How do you set up TLS termination and HTTP routing in HAProxy?
TLS termination at the load balancer simplifies certificate management and offloads cryptographic overhead from application servers. In 2026, there is no excuse for supporting anything below TLS 1.2, and TLS 1.3 should be your primary target. HAProxy handles this efficiently with minimal configuration.
frontend https_front
bind *:443 ssl crt /etc/haproxy/certs/ alpn h2,http/1.1
http-request set-header X-Forwarded-Proto https
http-request set-header X-Real-IP %[src]
# Route API traffic to dedicated backend
acl is_api path_beg /api/v1 /api/v2
use_backend api_servers if is_api
# Route static assets to CDN-origin backend
acl is_static path_end .css .js .png .jpg .woff2
use_backend static_servers if is_static
# Default catch-all
default_backend web_servers
backend api_servers
balance leastconn
option httpchk GET /api/health HTTP/1.1\r\nHost:\ api.internal
http-check expect status 200
server api1 10.0.2.10:8080 check
server api2 10.0.2.11:8080 check Several details here matter operationally. The alpn h2,http/1.1 directive enables HTTP/2 negotiation, which significantly improves page load performance for browser clients. The X-Forwarded-Proto and X-Real-IP headers preserve client context that would otherwise be lost after termination; ensure your application framework trusts these headers only from known HAProxy IPs to prevent spoofing.
For certificate management, concatenate your certificate chain and private key into a single PEM file. Automate renewal with certbot or acme.sh, and configure HAProxy to reload without dropping connections using the -sf flag or systemd socket activation. If you're comparing this approach with Nginx for your stack, the Nginx vs Apache comparison covers trade-offs relevant to reverse proxy selection.
How do you monitor HAProxy performance and troubleshoot issues?
You cannot manage what you cannot observe. HAProxy's built-in stats page provides real-time visibility into connection rates, queue depths, response times, and error counts. Enable it with authentication and restrict access to internal networks or VPN CIDRs.
listen stats
bind *:8404
mode http
stats enable
stats uri /stats
stats realm HAProxy\ Statistics
stats auth admin:SecurePasswordHere
stats refresh 10s
stats show-legends
stats admin if TRUE Beyond the stats page, integrate HAProxy logs with your observability stack. Structured logging in JSON format enables parsing by tools like Loki or Elasticsearch. Monitor these four golden signals religiously: request rate, error rate, latency percentiles (p50, p95, p99), and saturation (connection queue depth). When alerts fire, the stats page is your first diagnostic stop—check for servers marked DOWN, rising queue lengths, or abnormal retry counts.
For comprehensive observability integration, pair HAProxy metrics with the guidance in the four golden signals of monitoring to build dashboards that surface actionable incidents rather than noise.
Implementing Your HAProxy Load Balancing Guide Checklist
Deploying HAProxy in production requires methodical validation. Before promoting any configuration change, verify TLS cipher suites with testssl.sh, confirm health checks trigger correctly by simulating failures, and load test with realistic traffic patterns using k6 or wrk. Document your runbooks: how to add/remove backends, rotate certificates, interpret stats anomalies, and execute emergency rollbacks.
This HAProxy Load Balancing Guide gives you the foundation, but operational excellence comes from disciplined iteration. Start with the baseline config, measure real traffic behavior, then tune algorithms and timeouts based on evidence—not assumptions. If you need hands-on assistance architecting or auditing your load balancing layer, reach out to discuss your infrastructure needs.