DigitalOcean fully manages Regional Load Balancers and Global Load Balancers, ensuring they are highly available load balancing services. Load balancers distribute traffic to groups of Droplets in specific regions or across different regions, which prevents the health of a backend service from depending on the health of a single server or a single region.
After creating a global load balancer, you can add additional Droplets and domains, and modify its settings to fit your needs.
To add Droplets to a global load balancer, click Networking in the main navigation, click the Load Balancers tab, and then click the global load balancer that you want to add Droplets to.
On the load balancer’s overview page, click the Add Droplets button. In the Add Droplets modal, enter the name of the Droplet, or the tag associated with a group of Droplets, you want to add to the load balancer. The field populates with a list of Droplets and tags that match your search criteria. Click the Droplet or tag that you want to add and then click the Add Droplets button. The Droplets are added to the load balancer’s backend node pool.
To remove Droplets, click the More button beside the Droplet you want to remove and then click Remove.
To edit your global load balancer’s settings, click Networking in the main navigation, and then click Load Balancers to go to the load balancer’s overview page. From the overview page, click the Settings tab.
You can edit the following settings in the Settings tab:
In the Domains section, you can add or remove domains that route traffic to the load balancer, and subsequently, its backend Droplets. Follow the instructions in the Connect a Domain section of the setup guide to add a domain to your load balancer.
Enabling CDN caching allows DigitalOcean’s CDN to cache your server’s responses. This places your most requested content closer to your users, and reduces the load on your origin server.
If you enable CDN caching, your server responses must include the Cache-Control
HTTP header. This tells the CDN how long to cache the response for before requesting a new response from the origin server. For example, the following header, Cache-Control: public, max-age=3600
, tells the CDN to cache the response for one hour.
The following example shows how to set up your server responses for caching using an Express JS server. The server response is set to cache for one hour.
const express = require('express');
const app = express();
// Middleware to set caching headers
app.use((req, res, next) => {
res.set('Cache-Control', 'public, max-age=3600'); // Cache for 1 hour
next();
});
app.get('/', (req, res) => {
res.send('Hello World');
});
app.listen(3000, () => {
console.log('Server running at http://127.0.0.1:3000/');
});
Unlike regional load balancers, global load balancers can only have one forwarding rule, and the rule can only use HTTP protocols (HTTP
, HTTPS
, and HTTP2
) to forward traffic to the backend Droplets. The rule can only forward traffic to either ports 80
and 443
.
You can update the forwarding rule’s protocol and port in the Forwarding Rules section as needed.
In the Target section, you choose the Protocol (HTTP, HTTPS, or TCP), Port (80 by default), and Path (/
by default) that nodes should respond on.
In the Additional Settings section, you choose:
The success criteria for HTTP and HTTPS health checks is a status code response in the range 200 - 399. The success criteria for TCP health checks is completing a TCP handshake to connect.
In the Target section, you choose the Protocol (HTTP, HTTPS, or TCP), Port (80 by default), and Path (/
by default) that nodes should respond on.
In the Additional Settings section, you choose:
The success criteria for HTTP and HTTPS health checks is a status code response in the range 200 - 399. The success criteria for TCP health checks is completing a TCP handshake to connect.
By default, global load balancers route traffic to your Droplets in the nearest available region. However, if a region becomes unhealthy, traffic is routed to the nearest healthy region. You can change this to prioritize routing traffic to specific regions in the event of failover.
Sticky sessions send subsequent requests from the same client to the same Droplet by setting a cookie with a configurable name and TTL (Time-To-Live) duration. The TTL parameter defines the duration the cookie remains valid in the client’s browser. This option is useful for application sessions that rely on connecting to the same Droplet for each request.
The SSL option redirects HTTP requests on port 80 to HTTPS on port 443. When you enable this option, HTTP URLs are forwarded to HTTPS with a 307 redirect. To redirect traffic, you need to set up at least one HTTP forwarding rule and one HTTPS forwarding rule.
By default, load balancer connections time out after being idle for 60 seconds. You can increase or decrease this amount of time to fit your application’s needs. You can set it for a minimum of 30 seconds and a maximum 600 seconds (10 minutes).