Ployter

Replicas and load balancer

How to run a service on several AWS or DigitalOcean servers behind a load balancer, with deploys one server at a time and HTTPS on the load balancer.

With replicas, the same service runs on two or more servers. The load balancer receives traffic for the domains and spreads it across them. If a server goes down or is deploying, it leaves the rotation by itself and the others keep serving.

Replicas exist on AWS and DigitalOcean: the panel creates the load balancer in your account. On Hetzner, Vultr, Linode and your own server, each service runs on a single server.

Requirements

  • The plan must include Replicas and load balancer.
  • Two or more AWS or DigitalOcean servers, all from the same provider and on the same network (VPC). See Servers and Private network and firewall.
  • The service can't have a volume: each server would have a different copy of the data. Store files in a bucket and data in a database.
  • The service can't have a public port.
  • A container database has no replicas. For database high availability, use a managed database.
  • Agent v0.7.0 or newer on the servers.

Turn on replicas

  1. If the service is live, click Stop at the top of the service screen. While the service is running, the server list is locked.
  2. Go to Settings. In the General card, under Servers, check two or more AWS or DigitalOcean servers from the same provider. The first one checked is the primary.
  3. Click Save.
  4. Run a deploy.
  5. Follow the steps in the Network tab to point each domain's DNS to the load balancer.

Service settings with two servers checked under Servers
Service settings with two servers checked under Servers

With autoscaling on, it picks the servers and the list is locked. To pick by hand, turn off autoscaling. See Autoscaling.

AWS and DigitalOcean

It works the same way on both clouds. What changes is the load balancer and how you point the domain:

AWS DigitalOcean
Load balancer One per project, shared by the services with replicas One per service with replicas, with its own fixed IP
Domain record CNAME to the load balancer's name A to the load balancer's IP
HTTPS certificate From AWS (ACM), validated by a CNAME From Let's Encrypt, issued by the panel, validated by an _acme-challenge CNAME
How to split traffic One at a time, Least busy and Same server One at a time and Same server
Cost About US$ 16–20/month per project, plus traffic About US$ 12/month per service with replicas

The load balancer

Without a domain, the replicas run, but nothing reaches them. The load balancer estimate appears in Costs. See Subscription, plans and costs.

Load balancer on AWS

Each project has one load balancer (Application Load Balancer), shared by the services with replicas. It's created with the first domain of a service with replicas and takes about 3 minutes. When no service in the project needs it anymore, the load balancer is deleted. AWS bills it by the hour, plus traffic.

HTTPS lives on the load balancer, with an AWS certificate (ACM). A new domain joins the load balancer within moments, without a deploy.

Load balancer on DigitalOcean

The DigitalOcean load balancer can't route by domain. That's why each service with replicas gets its own load balancer, with a fixed IP. It's created with the service's first domain and takes a few minutes. When the service no longer has replicas or domains, the load balancer is deleted. It costs about US$ 12/month per service with replicas.

  • HTTPS (port 443) is terminated at the load balancer, with the certificate the panel issues. Once there's a certificate, port 80 redirects to HTTPS.
  • Both reach port 20000 on the droplets, where Traefik serves them.
  • The load balancer's health check calls the service's Health check.
  • A firewall opens port 20000 on the droplets only to that load balancer.

How traffic is split

Under Settings → Servers, with two or more servers checked, pick How to split traffic:

  • One at a time: each request goes to the next server. This is the default. Good for APIs and apps that don't keep sessions on the server.
  • Least busy: sends to the server with the fewest requests in progress. Helps when some requests take longer than others. AWS only.
  • Same server: each visitor always stays on the same server, through a load balancer cookie. Use it for apps that keep sessions in files, like Laravel with SESSION_DRIVER=file. Without it, users get logged out on every page refresh.

How to split traffic options in the service settings
How to split traffic options in the service settings

On DigitalOcean, Least busy doesn't exist: the option is disabled on the screen and traffic is split in rotation (round robin), like One at a time.

With Same server, choose for how many hours a visitor sticks to the server (up to 168). Use more than the app's session length. The change applies in moments, without a deploy.

Tip

The safest option is to keep sessions off the server: in Laravel, SESSION_DRIVER=redis or database. Then any server serves any visitor, and One at a time works. If a server goes down, nobody gets logged out.

Point the domain to the load balancer

With replicas, each domain needs two records in your DNS. The Network tab shows both and checks them by itself every few seconds.

With Cloudflare connected, Point creates both records for you. See also Domains and HTTPS.

DNS on AWS

  1. Point the domain to the load balancer: create a CNAME record with the target shown. If an A or CNAME record with that name already exists, delete it first: it points to the old place.
  2. Validate the HTTPS certificate: create the second CNAME record, which proves to AWS that you own the domain. AWS takes 2 to 30 minutes to validate. Don't delete this record: it also renews the certificate every year.

Load balancer DNS steps in the Network tab
Load balancer DNS steps in the Network tab

On the apex domain (e.g. loja.com.br), most DNS dashboards don't accept CNAME. Use ALIAS or ANAME with the same target. On Cloudflare, CNAME works normally.

Warning

On Cloudflare, keep the proxy off (gray cloud, "DNS only") until step 2 finishes. With the proxy on before that, the site goes down. Afterward, you can turn on the proxy with SSL/TLS set to Full (strict).

DNS on DigitalOcean

  1. Point the domain to the load balancer: create an A record with the IP of the service's load balancer, shown in the Network tab. If an A or CNAME record with that name already exists, delete it first: it points to the old place.
  2. Validate the HTTPS certificate: create, only once, the CNAME _acme-challenge.<domain> with the target <code>.acme.<platform zone> shown in the Network tab. For example:
_acme-challenge.api.loja.com.br  CNAME  k3x9a1b2c3.acme.ployter.app

With this record, the panel issues the HTTPS certificate by itself (Let's Encrypt): one certificate per service, with all of its ready domains. The panel uploads the certificate to the load balancer and renews it about 30 days before it expires. Don't delete this record: without it, the certificate doesn't renew.

  • Domain added later: the domains already live keep working. When the new one's CNAME shows up, a new certificate with all the domains replaces the old one, with no downtime.
  • Domain removed: the next certificate leaves it out.
  • Issuing fails: the current certificate stays on the load balancer, and the error shows on the domain.
Warning

On Cloudflare, create both records with the proxy off (gray cloud, "DNS only"). Once the certificate is issued, you can turn on the domain's proxy with SSL/TLS set to Full (strict).

Note

On a self-hosted panel, the DigitalOcean certificate needs the AUTO_DOMAIN_ZONE and AUTO_DOMAIN_CLOUDFLARE_TOKEN variables set in the API.

How deploys work with replicas

The deploy updates one server at a time, on AWS and on DigitalOcean:

  1. The server leaves the load balancer and waits for open connections to finish.
  2. The new version starts on it.
  3. The server rejoins the load balancer when the health check passes.
  4. The deploy moves on to the next server.

In the deploy modal, each server shows its own progress (Updating, live, failed). Set up a Health check on the service so the load balancer knows when the new version is up. See Service settings.

What changes in the service

  • Extra processes (queues, workers, scheduler) run only on the primary server.
  • Volumes aren't allowed.
  • Metrics show the sum of the replicas.
  • If a replica goes down, the others keep serving. The load balancer's health check removes the one that went down.

Turn off replicas

  1. Turn off autoscaling, if it's on.
  2. In Settings → Servers, leave only one server checked and save.
  3. Run a deploy.
  4. Point the domains' DNS back to the server's IP (A record). The Network tab shows the value.

On DigitalOcean, the service's load balancer is deleted when it no longer has replicas.