Load balancing
TCP
HAProxy can operate as a TCP proxy, in which TCP streams are relayed through the load balancer to a pool of backend servers. The TCP stream may carry any higher-level protocol (e.g. HTTP, FTP, SMTP). Often this mode is used when clients need to communicate with applications using a specific protocol meant only for that application, such as Redis (Redis Serialization Protocol) or MySQL database servers.
There are fewer fetch methods available to inspect raw TCP streams, but also less processing overhead.
Enable TCP mode Jump to heading
Set the mode
directive to tcp
in both the frontend
and backend
sections to load balance TCP connections.
In the following example, we load balance MySQL servers. Typically, it’s best to set the load-balancing algorithm to least connections when the servers may hold the connection for a variable amount of time. That algorithm sends the next client to the server with the fewest active connections.
haproxy
frontend mysqlmode tcpbind :3306default_backend mysql_serversbackend mysql_serversmode tcpbalance leastconnserver s1 192.168.0.10:3306server s2 192.168.0.11:3306
haproxy
frontend mysqlmode tcpbind :3306default_backend mysql_serversbackend mysql_serversmode tcpbalance leastconnserver s1 192.168.0.10:3306server s2 192.168.0.11:3306
See also Jump to heading
Do you have any suggestions on how we can improve the content of this page?