How to Balance TCP Traffic

Load balancers support TCP balancing for applications that do not speak HTTP. For example, deploying a load balancer in front of a database cluster lets you spread requests across all available machines.

To set up TCP balancing, you’ll need:

  • A TCP application, like a Galera cluster spanning several Droplets.

    If you use Galera, you’ll need to configure remote access to the database and create a remote user that has full access to that database. This ensures the load balancer will be able to connect; because load balancers are a highly available service with automatic failover, connections coming from it may not always originate from the same IP address.

  • A load balancer with those Droplets added to it.

The details of your backend configuration will depend on what software you’re using.

Add the Load Balancer TCP Forwarding Rule

On the load balancer index page with the control panel (located by selecting Networking followed by Load Balancers), click your load balancer’s name to view the detail page. On the detail page, click Settings to get to the settings page:

Load Balancer Settings page

In the Forwarding rules section, modify the existing rule by clicking the drop down menu in the first field and changing it from HTTP to TCP. Change the port in both the Load Balancer side and the Droplet side from 80 to the listening port of your application. For example, when using a Galera cluster, use the MariaDB port 3306.

Change default forwarding rule

When you’re done, click Save.

Add the TCP Health Check

Next, open the Health checks section. An HTTP health check is defined by default. Modify the health check by choosing TCP from the drop down in the first field. In the Port field, change the value from 80 to the listening port of your application.

Change health checks

Click Save to implement the change.