How to Balance TCP Traffic

DigitalOcean Load Balancers are a fully-managed, highly available network load balancing service. Load balancers distribute traffic to groups of Droplets, which decouples the overall health of a backend service from the health of a single server to ensure that your services stay online.

Load balancers support TCP balancing for applications that do not speak HTTP. To set up TCP balancing, you’ll need:

To configure the load balancer to balance TCP, you need to add a TCP forwarding rule and a TCP health check.

Add the TCP Forwarding Rule

From the control panel, click Networking, then click Load Balancers to go to the load balancer index page. From there, click your load balancer’s name to view its detail page.

On the detail page, click Settings.

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.

The forwarding rules for a load balancer with an example TCP to TCP rule filled in

When you’re done, click Save.

Add the TCP Health Check

On the same settings page, 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.