Load balancing

TCP

On this page

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 mysql
mode tcp
bind :3306
default_backend mysql_servers
backend mysql_servers
mode tcp
balance leastconn
server s1 192.168.0.10:3306
server s2 192.168.0.11:3306
haproxy
frontend mysql
mode tcp
bind :3306
default_backend mysql_servers
backend mysql_servers
mode tcp
balance leastconn
server s1 192.168.0.10:3306
server s2 192.168.0.11:3306

Do you have any suggestions on how we can improve the content of this page?