NickRamirez
NickRamirez

Reputation: 340

HAProxy Keep-Alive not working as expected

Is my reasoning about HTTP Keep-Alive correct? Here are my assumptions:

The reason I ask is because when I set a long keep-alive timeout (via timeout http-keep-alive), typing F5 rapidly loads a different backend server just as fast, rotating through all three without any effect from the keep-alive. I would have thought that if I hit F5 during the keep-alive timeout period of five seconds, I'd still get the same backend server.

Am I thinking about this wrong?

Here is my HAproxy config where I've set the keep-alive timeout to 5 seconds:

defaults
  timeout connect 5s
  timeout client 120s
  timeout server 120s
  timeout http-request 5s
  timeout http-keep-alive 5s
  option http-keep-alive

frontend http
  mode http
  bind *:80
  default_backend servers1

backend servers1
  mode http
  balance roundrobin
  server web1 web1:80 check
  server web2 web2:80 check
  server web3 web3:80 check

UPDATE

Using Wireshark, it looks like, from the client perspective, the client is reusing the same socket connection to HAProxy's frontend until the timeout expires. So the keep-alive appears to be working between client and frontend. However, I added an image to the Web page to see which backend server would serve it, and if it would be a different server than the one that served the HTML...and it was different. In roundrobin mode, two different backend servers served up content to the same client, even though I had keep-alive enabled.

This all seems to me like keep-alive is not working between frontend and backend. This is the behavior I would expect if I had set option http-server-close, but not when using option http-keep-alive which should keep the connection to the server open.

Upvotes: 2

Views: 7715

Answers (1)

Baptiste
Baptiste

Reputation: 1759

  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.

Upvotes: 2

Related Questions