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.

Initial Setup

To set up TCP balancing for a Galera cluster, 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.

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

Create a TCP Load Balancer

To create a Load Balancer, navigate to the Load Balancer index page with the Control Panel (located by selecting Networking followed by Load Balancers) and click Create Load Balancer.

In the Add Droplets section, choose all three of the Galera Droplets. This will also automatically select the region for your Load Balancer.

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

Next, click on the Edit Advanced Settings button within the Advanced settings section:

Edit advanced settings in DigitalOcean Load Balancer

In the expanded interface, in 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

When you are finished, click the Create Load Balancer button. Once the Load Balancer is available, you can connect to your application by connecting to the Load Balancer. You can find your Load Balancer’s public IP address by visiting the Load Balancer index page.