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 in DigitalOcean Load Balancer

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 DigitalOcean Load Balancer health check

Click Save to implement the change.