Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 35 ยท Load Balancers and Security

DigitalOcean Load Balancers

Spread traffic across Droplets with a DigitalOcean Load Balancer: forwarding rules, round robin and least connections, health checks, sticky sessions and TLS.

A single Droplet is a single point of failure and a single ceiling for traffic. A Load Balancer sits in front of several Droplets, accepts connections on one public IP, and forwards each request to a healthy backend. You get more capacity, zero-downtime deploys (take one Droplet out, update, put it back) and automatic removal of broken servers.

DigitalOcean cannot run in this course environment, so the console steps and commands are described as text, and the runnable Python blocks are stand-ins that simulate the algorithms and health checks.

Parts of a Load Balancer

  • Region and VPC. A regional Load Balancer lives in one region and reaches backends over the private VPC network, so backends need not expose public ports.
  • Backends. Add Droplets by name, or (better) by tag such as web. Any Droplet that gets the tag joins automatically, which suits autoscaling by script or Terraform.
  • Forwarding rules. Each rule maps an entry protocol and port to a target protocol and port, for example HTTPS 443 to HTTP 8080.
  • Health checks. The balancer probes each backend and sends traffic only to those that pass.
  • Size. Capacity is sold in nodes; you can add nodes to handle more connections and requests per second. Check the pricing page for current limits.
flowchart LR
  U["Visitors"] --> LB["Load Balancer<br/>203.0.113.10"]
  LB -->|"healthy"| A["web-1"]
  LB -->|"healthy"| B["web-2"]
  LB -.->|"failed check, skipped"| C["web-3"]

The third backend failed its health check, so it receives no traffic until it recovers.

Creating one with doctl

Terminal
doctl compute load-balancer create \
  --name web-lb \
  --region nyc1 \
  --vpc-uuid YOUR_VPC_ID \
  --tag-name web \
  --forwarding-rules "entry_protocol:http,entry_port:80,target_protocol:http,target_port:8080" \
  --health-check "protocol:http,port:8080,path:/healthz,check_interval_seconds:10,response_timeout_seconds:5,healthy_threshold:3,unhealthy_threshold:3"

Provisioning takes a minute or two. Then point your DNS A record at the balancer's IP. List balancers with doctl compute load-balancer list.

Choosing an algorithm

  • Round robin sends request 1 to backend A, 2 to B, 3 to C and repeats. It is simple and fair when requests cost about the same.
  • Least connections sends the next request to the backend with the fewest active connections. It copes better with slow or long-lived requests such as downloads and WebSockets.
Python Stand-in: round robin vs least connections

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first request takes nine seconds. Round robin keeps giving the busy backend its share of the following requests, whereas least connections routes around it.

Sticky sessions

If your app keeps sessions in a Droplet's memory, a visitor must hit the same backend every time. The balancer can set a cookie (DO-LB) or hash the client to a backend. Prefer stateless apps instead: store sessions in a managed Redis (see managed databases) so any backend can serve any user. Sticky sessions also unbalance load and break when a backend fails.

Health checks and thresholds

A backend is marked down after unhealthy_threshold consecutive failures and up again after healthy_threshold consecutive successes. Thresholds prevent flapping from a single dropped probe. The check path should be cheap and test what matters (for example it queries the database), but returning 200 from a static file proves only that Nginx runs.

Python Stand-in: health check state machine

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the isolated failures at probes 2 and 4 do nothing, three in a row take the server out, and a single pass does not bring it back.

TLS at the balancer

You have two choices. Terminate TLS at the balancer: upload a certificate or let DigitalOcean issue and renew a free Let's Encrypt certificate for a domain whose DNS is hosted on DigitalOcean; traffic then travels as HTTP inside the VPC. Or use TCP/SSL passthrough, where the backend holds the certificate and the balancer cannot see the request. With termination, the backend sees the client's address in X-Forwarded-For, and you can enable the PROXY protocol if you need it for TCP. More in TLS certificates.

Also turn on HTTP to HTTPS redirect in the forwarding rules, and lock the backends to the balancer with a Cloud Firewall so people cannot bypass it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why attach backends to a Load Balancer by tag?

    Tag-based membership lets scripts and Terraform add or remove capacity without editing the balancer.
  2. 2. When does least connections beat round robin?

    It sends new requests to the backend with the fewest open connections, so slow ones do not pile up.
  3. 3. What does the unhealthy threshold control?

    Consecutive failures are counted to avoid reacting to one dropped probe.
  4. 4. What is the main drawback of sticky sessions?

    Pinning users to one backend causes uneven load, and in-memory sessions are lost on failure.

Summary

  • A Load Balancer gives one public IP in front of several Droplets, with forwarding rules, health checks and optional TLS termination.
  • Add backends by tag, keep them private in a VPC and restrict them with a firewall.
  • Round robin is simple; least connections handles uneven requests better.
  • Thresholds in health checks prevent flapping; prefer stateless apps over sticky sessions.

Next: SSH Hardening and Access Control