You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HAProxy的TCP负载均衡是否原生支持持久化TCP连接?

HAProxy Native Support for Persistent TCP Connections

Absolutely, HAProxy natively supports persistent TCP connections for both client-to-load balancer and load balancer-to-backend server links—this is a standard use case for long-lived TCP services, so you’re in good hands. Let’s walk through how to set this up properly:

Core Configuration Components

1. Client-Side Persistent Connections

To keep clients connected to HAProxy for extended periods, focus on two key settings in your frontend block:

  • option tcpka: Enables TCP keepalive packets, which prevent intermediate network devices (like firewalls) from dropping idle connections.
  • timeout client: Sets the maximum idle time before HAProxy closes a client connection. Adjust this to match your service’s needs (e.g., 8h for all-day persistent links).

2. Backend-Side Persistent Connections

HAProxy will maintain long-lived connections to your backend servers as long as you configure these settings in the backend block:

  • option tcpka: Applies keepalive logic to backend connections too, ensuring links to your servers stay alive.
  • timeout server: Defines how long an idle backend connection stays open. Use a value aligned with your client timeout (e.g., 8h).
  • Health checks: Add check to your server definitions to ensure HAProxy only maintains connections to healthy backend instances.

Full Configuration Example

Here’s a working HAProxy config that implements persistent TCP connections end-to-end:

global
    log /dev/log local0
    maxconn 10000  # Adjust based on your expected concurrent connections
    daemon

defaults
    log global
    mode tcp
    timeout connect 10s  # Timeout for initial backend connection attempts
    timeout client 8h     # Keep client connections alive for 8 hours
    timeout server 8h     # Keep backend connections alive for 8 hours

frontend tcp_service_frontend
    bind *:8000  # Your public-facing TCP port
    option tcpka  # Enable keepalive for client connections
    default_backend tcp_service_backend

backend tcp_service_backend
    option tcpka  # Enable keepalive for backend connections
    balance roundrobin  # Use your preferred balancing algorithm (e.g., source, leastconn)
    # Define backend servers with health checks
    server backend1 192.168.1.10:9000 check inter 5s rise 2 fall 3
    server backend2 192.168.1.11:9000 check inter 5s rise 2 fall 3

Optional: Session Sticky (If Needed)

If you need the same client to always use the same backend server (common for stateful services), add session sticking logic to the backend:

backend tcp_service_backend
    option tcpka
    balance roundrobin
    # Create a stick table to track client IPs for 8 hours
    stick-table type ip size 100k expire 8h
    stick on src  # Bind clients to backends based on their source IP
    server backend1 192.168.1.10:9000 check
    server backend2 192.168.1.11:9000 check

Key Notes

  • Ensure your backend servers are configured to accept and maintain long-lived TCP connections (avoid auto-closing idle connections prematurely).
  • Adjust maxconn in the global section to handle your expected number of concurrent persistent connections.
  • If you’re working with strict firewalls, tweak the keepalive interval using tcpka-int <seconds> (e.g., tcpka-int 30 to send keepalives every 30 seconds).

内容的提问来源于stack exchange,提问作者ks2bmallik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:40:07