Load Balancer
elements man deploy/load-balancerTLS, 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
Hostheader 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
wwwpair of eachserver.domainvalue 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:
- Configure the domain in
server.domain(and any servicedomains). - Point DNS at the machine's public IP.
- 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.jsocdeploy: { 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:
- Exact match (
api.example.com). - 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 recordsA example.com <production0 publicIp>
For redundancy or scale, point at multiple machines:
DNS recordsA 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:
- Strip the port.
- If the host is the
wwwpair of aserver.domainvalue, return 301 to the value. - If the host matches a service's
domains, proxy to that service's port (round-robined across machines). - Otherwise, proxy to the app backend (default port 4000, round-robined across machines).
config.jsocdeploy: { 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
Terminalelements 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:
-
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. -
Set
build.assetUrlinconfig.jsoc:config.jsoc{ build: { assetUrl: env("ASSET_URL", ""), }, }config/env/production.envASSET_URL=https://d1234abcd.cloudfront.net -
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.