Many processes behind a load balancer
The same binary runs in every process. Each request is checked in the users’ database, so any process serves any request. Steps and rules: the wiki.
use std::sync::Arc;
use std::time::Duration;
use auth_chad::Store;
use auth_chad::axum::{Auth, Service, work};
fn setup<S: Store + 'static>(auth: Auth<S>) -> Arc<impl Service> {
Arc::new(auth.with_proxies(1)) // one load balancer adds the client address to X-Forwarded-For
}
async fn worker<A: Service>(auth: Arc<A>) {
loop {
work(auth.as_ref()).await.ok(); // prunes, sends mails that wait, runs the work of places
tokio::time::sleep(Duration::from_secs(5)).await;
}
}
async fn start<A: Service>(auth: Arc<A>) {
match std::env::var("ROLE").as_deref() {
Ok("worker") => worker(auth).await,
Ok("web") => { /* serve the router only */ }
_ => {
tokio::spawn(worker(Arc::clone(&auth))); // one process does both: good for one machine
/* serve the router */
}
}
}
- Database: PostgreSQL for more than one machine. Rate limits, single-use marks of key proofs and leases are in it, so every process sees them.
- Worker: any number of processes can call
workat once. Each piece of work has a lease (a mail one minute, a place 5 minutes), so it runs in one place at a time. Work that takes longer, or a crash, can run it twice:provisionandremovemust be safe to repeat. With Loco: registerauth::Workand run the taskauth_workfrom Loco’s scheduler. with_proxies(n): the number of proxies that add an entry toX-Forwarded-For. The address is the entry that the outer one added. The processes must be reachable only through the proxies. Without proxies, keep the default 0.- What the load balancer must do: pass the
Hostheader, and add the client address toX-Forwarded-For. Sticky sessions are not needed.
Caddy, a path for each tenant
app.example.com {
reverse_proxy app1:3000 app2:3000 app3:3000 {
lb_policy round_robin
health_uri /healthz
}
}
Caddy passes Host and sets X-Forwarded-For to the client address: with_proxies(1). /healthz is a route of your
application. Tenants are at
app.example.com/t/acme/ (Tenancy::Path).
Caddy, a host for each tenant
example.com, *.example.com {
tls {
dns cloudflare {env.CLOUDFLARE_API_TOKEN}
}
reverse_proxy app1:3000 app2:3000 {
lb_policy round_robin
health_uri /healthz
}
}
A wildcard certificate needs a DNS challenge, so Caddy needs the module of your DNS provider (here
caddy-dns/cloudflare). Tenants are at acme.example.com (Tenancy::Host { parent: "example.com" }), and the login
origin is example.com.
nginx
upstream app {
server app1:3000;
server app2:3000;
}
server {
listen 443 ssl;
server_name example.com *.example.com;
location / {
proxy_pass http://app;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
$proxy_add_x_forwarded_for adds the client address at the end: with_proxies(1).
Two proxies
A CDN in front of Caddy is two proxies: with_proxies(2). The inner proxy must keep the entry of the outer one. In
Caddy, list the CDN’s addresses in trusted_proxies, or Caddy replaces the header.
Other load balancers
An AWS Application Load Balancer adds the client address to X-Forwarded-For: with_proxies(1).