Proxies·7 min read·

HAProxy as a High-Throughput Layer 7 TLS Offloader and Connection Multiplexer

Handle tens of thousands of concurrent connections on minimal VPS resources with HAProxy Layer 7 TLS termination and HTTP connection pooling.

NB

Netbay Infrastructure Team

Netbay Engineering

When building applications designed to handle tens of thousands of concurrent websocket or HTTP clients, backend application servers (such as Python FastAPI, Ruby on Rails, or Node.js) quickly bog down under the computational weight of TLS handshakes and connection state tracking. HAProxy is renowned as an event-driven, single-threaded or multi-threaded Layer 7 load balancer capable of managing 100,000 concurrent sessions on modest VPS hardware.

By terminating TLS at HAProxy and enabling aggressive HTTP connection reuse (multiplexing), your backend applications only need to maintain a tiny pool of warm, persistent TCP connections to serve massive request volumes.

The technical secret behind HAProxy's exceptional efficiency is its non-blocking event loop architecture based on the Linux epoll(7) syscall. Rather than assigning an operating system thread to each client connection (which consumes 2MB to 8MB of stack memory per connection), HAProxy maintains a lightweight state machine in user space. It reads incoming network packets into memory, decrypts the TLS session using hardware AES-NI instructions, parses HTTP headers, and forwards requests over pre-existing persistent TCP channels.

Expand
HAProxy Load Balancing Architecture
HAProxy Layer 7 TLS offloading and connection pooling

Crafting a Production HAProxy Configuration

Configure /etc/haproxy/haproxy.cfg with multi-threading, strict TLS 1.3 settings, and backend connection pooling:

HAProxy Layer 7 Architecture: Connection Multiplexing & Offload 10,000+ HTTPS Clients TLS termination Keep-alive client conns HTTP/2 multiplexing HAProxy Core Event-driven epoll engine http-reuse aggressive Health check state machine Backend Services ~20 Persistent TCP conns Zero TLS CPU overhead App threads stay unblocked Resource Efficiency Win: HAProxy multiplexes 10,000 incoming user connections onto just 20 warm persistent backend sockets, reducing backend application thread count and database connection pool contention by 90%.
haproxy
global
    log /dev/log local0
    chroot /var/lib/haproxy
    user haproxy
    group haproxy
    daemon
    stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
    stats timeout 30s
    nbthread 4
    maxconn 50000

    # Modern TLS ciphers
    ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
    ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    timeout connect 5000ms
    timeout client  50000ms
    timeout server  50000ms
    retries 3

frontend https_front
    bind :443 ssl crt /etc/ssl/certs/site.pem alpn h2,http/1.1
    http-request set-header X-Forwarded-Proto https
    http-request set-header X-Real-IP %[src]
    default_backend api_servers

backend api_servers
    balance roundrobin
    # Aggressively reuse existing persistent connections to backend
    http-reuse aggressive
    option httpchk GET /health
    http-check expect status 200
    server app1 127.0.0.1:8080 check maxconn 250
    server app2 127.0.0.1:8081 check maxconn 250

The directive http-reuse aggressive is a transformative feature. In traditional reverse proxies, every incoming HTTP request initiates a new TCP three-way handshake to the backend. With http-reuse aggressive, HAProxy keeps idle TCP connections to app1 and app2 open indefinitely and multiplexes incoming requests from completely different clients over those shared sockets, shrinking backend latency and eliminating ephemeral TCP port exhaustion.

Inspecting Live Runtime Metrics via Admin Socket

HAProxy provides a Unix management socket. Using the socat utility, administrators can query metrics, drain servers for maintenance, and inspect active connection counts without downtime:

bash
# Query health status and byte counters for all backends
echo "show stat" | sudo socat stdio /run/haproxy/admin.sock | cut -d',' -f1,2,18,34,57

# Gracefully put backend app1 into maintenance mode
echo "set server api_servers/app1 state maint" | sudo socat stdio /run/haproxy/admin.sock

# Re-enable backend app1 after maintenance
echo "set server api_servers/app1 state ready" | sudo socat stdio /run/haproxy/admin.sock

Setting a server to maint mode triggers HAProxy to stop sending new client sessions to that instance while permitting active in-flight transactions to conclude gracefully. This enables zero-downtime rolling upgrades on backend services with surgical precision.

Combining HAProxy's event-driven architecture with connection pooling ensures your server delivers rock-solid throughput under severe traffic spikes.

HAProxy Architecture and Load Balancing
YouTube

You can spin up an Ubuntu 24.04 instance on Netbay in under 60 seconds with Intel Xeon Platinum compute and high-speed SSD storage in Lucknow — netbayhosts.in.

Keep reading

Follow along on a real VPS

Deploy Linux in under 60 seconds

These guides are written against Ubuntu, Debian, and RHEL-family images — the same ones on NetBay.

Deploy an instance