6

Currently, I have a single client (component) having a single persistent TCP connection with the server(another component), and messages are exchanged asynchronously. I wanted to have a load balancer (HAProxy preferably) where the connection b/w client and load balancer as well as b/w load balancer and multiple servers as persistent TCP connection.

I know HAProxy can easily be set up for the TCP load balancing, but I wanted to know does it support persistent connections out of the box. Would be great help if someone points me in the right direction. Thanks.

ks2bmallik
  • 184
  • 3
  • 18
  • 1
    This question should probably be posted on [Server Fault](https://serverfault.com) rather than here, though that is a difficult distinction since HAProxy's configuration embodies many kinds of logic that are executed in a defined order, which could be interpreted as "programming." Unfortunately, you haven't shown any "code" (configuration) or an attempt to obtain the desired behavior and an explanation of what happened that was unexpected. It is also unclear what exactly you mean by "persistent." You may mean long-running (supported) or sticky (very different, but also supported). Clarify? – Michael - sqlbot May 25 '18 at 12:20

2 Answers2

6

Yes it supports persistent TCP connections right out of the box. A simple implementation in the haproxy looks like this

listen tcpProxy 0.0.0.0:7000 
    mode tcp 
    balance source
    server tcp1 ip1:port check maxconn 10000
    server tcp2 ip2:port check maxconn 10000

Hope this helps

  • 2
    You'll also want to make sure `timeout tunnel` and any other relevant timers do not have default values that are shorter than expected, since they will tear down connections with no traffic when they fire. – Michael - sqlbot May 25 '18 at 12:15
  • I'm not clear - what option in this config balances TCP packets from a single connection across servers? I tried a hello world example `cat`ting a large file over netcat to HA proxy and all the data just went to one server. – Sridhar Sarnobat Aug 15 '19 at 00:57
  • I just tried this with HA Proxy 2.0.4 and the configuration syntax doesn't work. `'listen' cannot handle unexpected argument` – Sridhar Sarnobat Aug 15 '19 at 18:58
2

This is impossible. TCP is a stateful protocol. Even we do it by some tricks, such as an iptables mirror, the target backend host will drop packets which it has no handshake or pre packets.

You would have to consider UDP instead.

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106