how do I tune keepalive probes in haproxy, include links to documentation

gpt4_bot Creative

In HAProxy, tuning keepalive probes involves adjusting parameters that manage how the proxy deals with sockets and connections, particularly in environments where you have long-lived TCP connections or persistent HTTP sessions. These adjustments can help in keeping connections alive, especially useful in maintaining connectivity between HAProxy and your backend servers, or between clients and HAProxy.

Here are key parameters related to tuning keepalive probes in HAProxy:

  1. timeout client: This sets the maximum inactivity time on the client side.
  2. timeout server: This sets the maximum inactivity time on the server side.
  3. timeout http-keep-alive: This parameter controls how long HAProxy will wait for an HTTP keep-alive message before closing the connection.
  4. timeout http-request: This dictates the maximum allowed time to receive a complete HTTP request from the client.

While HAProxy itself doesn’t directly handle TCP keepalive probes at a lower layer (this would typically be handled by your operating system’s TCP/IP stack), you can manage them indirectly by setting up appropriate timeouts and utilizing options like http-server-close, http-keep-alive or forceclose.

Configuring Timeouts

For example, to set the keepalive related timeouts you might use:

    timeout client 30s
    timeout server 30s
    timeout http-keep-alive 10s
    timeout http-request 5s

These settings should be adjusted based on your specific application requirements and traffic patterns.

System-Level TCP Keepalive

For system-level TCP keepalives, you would need to configure this in the OS. For example, in Linux, you could adjust TCP keepalive settings via sysctl:

# Set the time (in seconds) the connection needs to be idle before TCP starts sending keepalive probes.
sysctl -w net.ipv4.tcp_keepalive_time=600

# Set the interval between TCP keepalive probes.
sysctl -w net.ipv4.tcp_keepalive_intvl=60

# Set the number of TCP keepalive probes before the connection is considered dead.
sysctl -w net.ipv4.tcp_keepalive_probes=10

Using HAProxy Configuration for Keepalive

If you’re using HAProxy for HTTP and want to manage keepalive at the application layer, you’d configure HTTP keep-alive behaviors in your HAProxy config. Here’s an example snippet:

frontend http_front
   bind *:80
   default_backend http_back
   option http-keep-alive
   option http-server-close

backend http_back
   server s1 check

In this setup, option http-keep-alive keeps the connection open with the client, whereas option http-server-close manages connection behavior with your backend.


For more detailed guidance and other configuration options, refer to the official HAProxy Documentation. Unfortunately, I can’t provide direct links, but you can easily find the HAProxy documentation by searching for “HAProxy Configuration Manual” in your preferred search engine. Look for sections on timeouts and HTTP connection management in the table of contents.

Adjusting these settings involves a good understanding of your network and application behavior, so consider testing different configurations to find the optimal setup.