How to Enable Keep-Alive in Haproxy

How to enable keep-alive in haproxy?

HTTP 1.1 operates in keep-alive mode by default, see RFC7230. So you don't need to explicitely set the Connection header in order to use persistent connections (keep-alive mode).

The connection header is only needed when using HTTP 1.0 because it was designed to close the connection after every request.

Thus, to verify that HAProxy is operating in Keep-alive mode, you need to send multiple HTTP requests (and not just one) with curl, and check if there was only one connection established and used.

This serverfault post shows how to do that. (Notice the "Connecting to .." and "Closing connection" lines)

HAProxy Keep-Alive not working as expected

  1. you should ensure your server does not close the connection.


    1. you should enable the "option prefer-last-server", in your HAProxy's defaults section.
    2. you should re-read the "option http-keep-alive" documentation of HAProxy: http://cbonte.github.io/haproxy-dconv/configuration-1.6.html#option%20http-keep-alive.
      It clearly states that:

If the client request has to go to another backend or another server due to content switching or the load balancing algorithm, the idle connection will immediately be closed and a new one re-opened. Option "prefer-last-server" is available to try optimize server selection so that if the server currently attached to an idle connection is usable, it will be used.

(hence point 2)

So your observations looks normal.

HAProxy closes long living TCP connections ignoring TCP keepalive

TCP keep alive is at the transport layer and is only used to do some traffic on the connection so intermediate systems like packet filters don't loose any states and that the end systems can notice if the connection to the other side broke (maybe because something crashed or a network cable broke).

TCP keep alive has nothing to do with the application level idle timeout which you have set explicitly to 200s:

timeout client 200000ms
timeout server 200000ms

This timeouts gets triggered if the connection is idle, that is if no data get transferred. TCP keep alive does not transport any data, the payload of these packets is empty.



Related Topics



Leave a reply



Submit