Search code examples
haproxy

How HAProxy forward traffic


I have installed a HAProxy in my server, I want Haproxy distribute all traffic from port 9092 into server worker0 and server worker1.

The following is configuration:

frontend sample-traffic
        bind *:9092
        default_backend sample-traffic
        mode tcp
        option tcplog

backend sample-traffic
        balance source
        mode tcp
        server worker0 10.16.38.210:9092 check
        server worker1 10.16.38.211:9092 check
$ netstat -nap | grep 9092
tcp        0      0 0.0.0.0:9092             0.0.0.0:*               LISTEN      8114/haproxy     
$ ps aux | grep 8114
haproxy   8114  0.7  2.9 102784 55848 ?        Ss   Jan11  94:44 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds

Okay, haproxy is running well, it is listening port 9092.

Here HAproxy is acted as a load balancer and just forward tcp traffic. My question is,

(1) Is my tcp connection terminated in HAproxy and then HAProxy re-forward my traffic to my destination server?

The reason why I ask this question is, because I saw there is a process that is listening on Port 9092, HAProxy need to receive traffic, then HAProxy need to terminate tcp for receiving.

Then, HAProxy's another side will establish another tcp connection to my destination server, and use it to forward traffic, so I think my TCP connection should be terminated in HAProxy.


                       
    +-+----+--+              +-----------------------------+            +-----------------+
    |         | tcp1         |          HAProxy            |   tcp2     |                 |
    | Clients +--------------> receiver        sender----------------->+ App Server       |
    |         |              |                             |            |                 |
    +---------+              +-----------------------------             +-----------------+

However, if the above is true, there will be two tcp socket connection, I feel it will be very lower efficient.

So I need HAProxy experts to help me understand how HAProxy process this scenario inside, is it a tcp socket connection or two socket connections inside HAProxy?


Solution

  • Is my tcp connection terminated in HAproxy and then HAProxy re-forward my traffic to my destination server?

    No, that is not how HAProxy or other proxies works, the connection will pass through HAProxy and will be terminated only when the client or the server ends the connection for some reason.

    In your example you have a frontend listening to the port 9092, this frontend has two backend servers on different machines, also using the same port.

    When your HAProxy server receives traffic in the port 9092, it will make a TCP connection with the client and it will also make another TCP connection to one of your backend servers to pass the traffic, so you will have two tcp connections, one with the client on the frontend side and another with the server in the backend side. It works like the drawing you made.