0

I’ve got HAProxy as a reverse proxy to balance requests on to a backend of nodes, but for some reason if the client sends a request with HTTP keep-alive then HAProxy in TCP mode doesn’t rotate between the backend servers, but HAProxy in HTTP mode does. Both modes balance in round-robin for each request if HTTP keep-alive is turned off at the client.

Here’s the basic config:

listen nodes_proxy
        mode tcp
        bind :9090
        balance roundrobin
        timeout client 40s
        timeout server 40s
        retries 1
        retry-on conn-failure
        option redispatch 1
        server node1 x.x.x.x:8080
        server node2 x.x.x.x:8080
        server node2 x.x.x.x:8080

All backend nodes are active and working, but HAProxy in TCP mode doesn’t do round-robin balancing on each request from the same client with keep-alive, while HTTP mode does. For our simple use case, we prefer the TCP mode, but it doesn’t balance as expected.

How can I ensure that each request from the client (with keep-alive) is balanced in round-robin mechanism, and client-to-proxy connections as well as proxy-to-server connections are kept alive for reuse until timeout?

2

You're describing the normal and expected behavior of a connection proxied at Layer 4.

In TCP mode, HAProxy does very little or no inspection of the traffic, and certainly does not attempt to read HTTP headers. It just passes traffic. Until one end or the other disconnects, it just keeps passing traffic.

If you want such a connection balanced between servers, you must use HTTP mode.

  • Thanks for clarifying Michael. HAProxy (v2.2) docs don't clearly mention that, and tons of configs had TCP mode balancing. It's puzzling because roundrobin balancing in TCP mode works fine if keep-alive is turned-off on the client. Would you know why that works though? – Nick Sep 30 '20 at 13:13
  • Because if keep-alive is off, then the connection actually disconnects. – Michael Hampton Sep 30 '20 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.