elements
Start Docs Blog Demos UI Pricing
Elements Manual
  • Getting Started
  • CLI
  • Build
  • Browser
  • Packages
  • TypeScript
  • Elements HTML
  • Assets
  • RPC
  • Router
  • Session
  • Database
  • Migrations
  • LiveTable
  • Channel
  • Jobs
  • Email
  • Payments
  • JSON
  • Async
  • Style
  • UI
  • JSOC
  • Config
  • Tests
  • Logs
  • Deploy
    • Setup
    • Sizing
    • AWS
    • DigitalOcean
    • Email Setup
    • CDN
    • Walkthrough
    • Domain
    • What Deploy Does
    • Environments
    • Load Balancer
    • Operations
    • Machines
  • Editors
  • Recipes
  • Licenses
  • Issues
  • Changelog
Manual / Deploy/ Load Balancer

Load Balancer

elements man deploy/load-balancer

On This Page

  • Domains
  • Auto SSL (acme)
  • Static TLS Certs
  • DNS
  • Routing
  • Round-Robin and the public/private Distinction
  • Failover
  • Maintenance Page
  • CDN

TLS, DNS, routing, and how Elements issues and renews certificates.

Every Elements deploy machine runs its own load balancer. It is part of Elements and starts with the app. It issues a Let's Encrypt certificate for every configured domain, and it balances requests across every machine in the environment. There is no extra software to install, no separate appliance to put in front of the cluster, and no separate config file to maintain. The load balancer reads the same config.jsoc your app reads, and reloads it after a build that changes it, so a change to domains, certificates, or services takes effect with no restart.

Every machine runs the same load balancer, and each load balancer reads the list of the environment's machines from config.jsoc. So you can point your DNS at one of your machines, several of them, or all of them. The machine that receives a request forwards it to one of the environment's machines, a different machine on each request.

What it does:

  • HTTP listener on port 80 (configurable). Serves ACME challenges and 301-redirects to HTTPS when TLS is on.
  • HTTPS listener on port 443 (configurable). Terminates TLS, then proxies to the app or service backend.
  • Auto SSL via ACME / Let's Encrypt. Certificates issued and renewed automatically for every configured domain.
  • Static SSL: bring-your-own certificates served by SNI match (overrides ACME for those hosts).
  • Host routing: dispatches by Host header to the app or a named service.
  • Round-robin: balances requests across all machines in the environment.
  • Failover: retries other machines when a backend fails. Serves the maintenance page only when every backend is unreachable.
  • Redirects: 301 from the www pair of each server.domain value to the value, before any service routing.
  • Maintenance: serves a static maintenance page on elements maint on.

When the project config changes, the load balancer switches to the new config while it keeps running. Adding a domain, changing ssl, or replacing a static cert takes effect without dropping connections.

Domains

The LB serves every value of server.domain (elements man config), read from the deploy machine's own environment: the machine evaluates config.jsoc with its config/env/<name>.env, so env!("DOMAIN") is the production host there. Each value is certified along with its www pair, and the pair is a 301 to the value with the path and query kept: www.example.com goes to example.com, or example.com goes to www.example.com when the value is the www form. A host that is itself a value is never redirected, so listing both serves both. Local names (localhost and anything under .localhost) are skipped.

Auto SSL (acme)

When ssl: true (the default when server.domain has a public host) and the machine has a publicIp, the LB enables ACME via Let's Encrypt. When a deploy changes the host list, the LB requests a certificate for every server.domain value and service host right away, a few at a time, using the HTTP-01 challenge served on port 80. A www pair is requested only when its name resolves, and a host with a valid cached certificate is not requested again. Handshakes use the cached certificate. Renewal happens automatically before expiry.

Set up:

  1. Configure the domain in server.domain (and any service domains).
  2. Point DNS at the machine's public IP.
  3. Open ports 80 and 443 to the public internet.

A host whose certificate fails (DNS not pointing at the machine yet, ports closed) is logged on the machine with the reason, and is tried again on its first HTTPS request.

ACME is enabled when deploy.ssl is true (default if server.domain has a public host), at least one domain is configured, and the machine has a publicIp. When any condition is missing, TLS is disabled and the LB serves cleartext on port 80. Useful for internal-only deployments or local testing.

Static TLS Certs

To override ACME for specific hosts, put a cert file and a key file in the project and name them in deploy.tlsCerts:

config.jsoc
deploy: { tlsCerts: { "*.example.com": { cert: "config/tls/wildcard.crt", key: "config/tls/wildcard.key" }, "example.com": { cert: "config/tls/wildcard.crt", key: "config/tls/wildcard.key" }, "api.example.com": { cert: "config/tls/api.crt", key: "config/tls/api.key" }, }, }

When the SNI hostname of a TLS handshake matches an entry, the LB serves that entry's static cert and requests no ACME certificate for the host. Match priority:

  1. Exact match (api.example.com).
  2. Single-label wildcard (*.example.com).

ACME continues to operate for any host without a static entry. The two modes coexist per-host.

Cert paths resolve relative to the project root. Keep keys out of version control (mode 600; add to .gitignore).

DNS

Point DNS at the public IP of one or more machines. For a small app, one A record at production0 is enough:

DNS records
A example.com <production0 publicIp>

For redundancy or scale, point at multiple machines:

DNS records
A example.com <production0 publicIp> A example.com <production1 publicIp> A example.com <production2 publicIp>

DNS round-robin distributes incoming requests across machines. The LB on each machine then round-robins to all the other machines, so a request that hits the busiest machine still gets balanced. Multiple A records also give you DNS-level failover: if one machine is unreachable at the network level, the resolver uses another.

Failover is built in: each load balancer retries the next machine on a failed request. A DNS provider with health checks works alongside it.

Routing

The LB looks at the Host header on each request:

  1. Strip the port.
  2. If the host is the www pair of a server.domain value, return 301 to the value.
  3. If the host matches a service's domains, proxy to that service's port (round-robined across machines).
  4. Otherwise, proxy to the app backend (default port 4000, round-robined across machines).
config.jsoc
deploy: { services: [ { name: "api", port: 8080, domains: [env!("DOMAIN_API")], bin: "services/api/bin/api", }, ], }

Service hosts are plain values, usually from env!() so each environment has its own. They are not server.domain keys: the load balancer proxies a request for a service host to the service's port, so the app's router never receives it. They get no www pair.

Round-Robin and the public/private Distinction

Public requests (source IP outside the private network) are round-robined across every machine in the environment that has a privateIp. Each LB picks a different machine on each request via an atomic counter.

Requests from a private IP (other LBs forwarding to this one) are served locally instead of being round-robined again. This prevents infinite loops where two LBs forward to each other indefinitely.

Failover

When a backend connection fails (refused, timeout), the LB tries the next machine. It cycles through every backend before giving up. Only when every backend is unreachable does the LB serve the maintenance page.

When one machine goes down, the other machines keep serving: a request that fails on the down machine is retried on the next machine. Add machines for spare capacity.

Maintenance Page

Terminal
elements maint on # every production machine elements maint off elements maint on -remote=staging elements maint on -remote=production#production1

While it is on, every machine serves the static maintenance page on every request instead of proxying. It takes effect at once, from any terminal, and does not wait on a build or a deploy in progress: use it for a planned outage, a release you want hidden while you fix it, or a deploy that is taking longer than it should. It stays on until elements maint off, and a deploy does not turn it off. A build server restart clears it.

A normal deploy never shows the page. Deploys are zero-downtime: the previous release serves until the new one is ready. For a deploy with a long-running migration, elements deploy -maint shows the page on every machine from just before the app database migration until the release, then takes it down. The build, the test database migration and the tests run before that with the site up.

The page is static/maint.html in the project. It is also served when no backend is reachable, and while the app starts.

CDN

A CDN isn't required. Asset URLs are content-hashed and the LB serves them with Cache-Control: immutable, so a browser fetches each asset from the LB once and caches it indefinitely.

CDN (opens in a new tab) sets up CloudFront step by step. With any CDN (CloudFront, Cloudflare, Fastly, Bunny, etc.) the integration is one config setting:

  1. Create a distribution at the CDN. Point it at the LB's public IP or hostname as the origin. The CDN gives you a hostname back, e.g. d1234abcd.cloudfront.net.

  2. Set build.assetUrl in config.jsoc:

    config.jsoc
    { build: { assetUrl: env("ASSET_URL", ""), }, }
    config/env/production.env
    ASSET_URL=https://d1234abcd.cloudfront.net
  3. Deploy.

No other setup is needed. The build rewrites every emitted asset reference in your pages to use that base URL. Browsers fetch assets from the CDN. The first time an asset is requested, the CDN fetches it from the LB and caches it. After that the CDN serves its cached copy.

In development, leave ASSET_URL empty. Elements emits relative URLs, so the browser fetches each asset from the same host as the page. There is nothing to switch on between development and production beyond the environment variable.

Configure the CDN to forward the Host header so the LB's host routing still works for any non-asset request that hits the same distribution.

Prev EnvironmentsNext Operations
Product
Manual Pricing Compare
Company
History Contact
Legal
Privacy Policy Acceptable Use Terms License Agreement
© Elements Dev Corporation 2026