Scaling
Summary: how an application with auth-chad grows from one process and one database to many processes and many databases, without changing its handlers.
Key terms: step, users’ database, directory, place, load balancer, with_proxies, worker, lease, shared state.
Related: Flows · load-balancer recipe · directory recipe · spec 15
An application with auth-chad scales in three steps: one process with one database, many processes with one database, and many processes with data spread over several database servers. Every request checks its session in the users’ database before your code runs, so processes share no state and need no trust between them. Moving to the next step changes configuration and hosting; handlers stay the same. Most applications stay at step 1.
The picture as text
flowchart TB
subgraph s1 [Step 1]
direction LR
A1[One process] --> D1[(One database)]
end
subgraph s2 [Step 2]
direction LR
L2[Load balancer] --> A2[Process 1] & B2[Process N]
A2 & B2 --> D2[(PostgreSQL)]
end
subgraph s3 [Step 3]
direction LR
L3[Load balancer] --> A3[Process 1] & B3[Process N]
A3 & B3 --> U3[(Users' database:<br/>who and where)]
A3 & B3 --> X3[(Data server 1)] & Y3[(Data server 2)]
end
s1 --> s2 --> s3
Which step
Answer these questions in order and stop at the first that fits.
| Question | Step |
|---|---|
| Does one process carry the load? | Step 1. Most applications. Tenants do not change the answer |
| Do you need more processes, or a second machine for failover? | Step 2. Move to PostgreSQL, put a load balancer in front |
| Is the data too large for one database, or do you want a database for each customer? | Step 3. The users’ database also says where each tenant’s or user’s data is |
Multiple regions, and servers that cannot reach the users’ database, are out of scope.
| Data | Processes | Database | |
|---|---|---|---|
| Step 1 | One database | One | SQLite (recommended) or PostgreSQL |
| Step 2 | One database | Many identical, behind a load balancer | PostgreSQL |
| Step 3 | Users’ database, and your data on one or many servers | Many identical | PostgreSQL |
Step 1: one process, one database
- Signup and your first rows in one transaction. Foreign keys and joins to users. One backup.
- Limits: one machine (with SQLite, several processes on that machine can share the file, for example during a deploy), and all data must fit in one database.
Step 2: many processes, one database
- More capacity; deploys and failures without downtime. Any process serves any request.
- Needs: PostgreSQL (SQLite is not synced between machines), the real client address with
with_proxies(n), and the worker. Rate limits, single-use marks and leases are stored in the database, so every process sees them.
Step 3: your data on database servers
- Any number of PostgreSQL servers, each with one shared database or one database for each customer. A tenant can be moved, restored or deleted alone. The session carries the place, so no extra lookup is needed.
- Costs: no shared transaction and no foreign keys between users and your data (an outbox joins signup and your data); every process must reach the users’ database and every data server.
use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize)]
struct Place {
server: String, // "db2": the process maps it to a connection string from its own settings
database: String, // "customer_7". Never a password
}
Requests get 503 not_ready until provision answers true, and 503 moving during a move.
The three independent decisions
| Decision | Choices | What it changes for auth |
|---|---|---|
| A. Data | One database, or users’ database plus data databases | The second adds the places feature |
| B. Processes | One, or many identical processes behind a load balancer | Many processes need PostgreSQL, with_proxies and a worker |
| C. Tenant address | None, a path (/t/acme/), or a host (acme.example.com) | The tenants feature, cookies, your DNS. Every step works with every choice |
- A path for each tenant: one host name and one certificate; one cookie per tenant (
__Host-session-acme, at most 20† at once). All tenants share one web origin, so a script flaw in one tenant’s pages affects all of them. - A host for each tenant: a wildcard DNS record and certificate. Each tenant has its own origin and cookie. A browser enters with a one-use code of 60 seconds. One passkey for the parent domain works on all hosts.
What we assume about the network
| Assumption | Why |
|---|---|
| Every process reaches the users’ database, and in step 3 every data server | Each request is checked there |
The load balancer passes the Host header | Host tenants and key proofs name that host |
The load balancer adds the client address to X-Forwarded-For, and the program knows the number of proxies (with_proxies(n)) | Rate limits count each client; a client cannot write its own entry |
| The processes are reachable only through the load balancer | Otherwise a client can write X-Forwarded-For itself |
TLS can end at the load balancer or at the process. Sticky sessions are not needed.
The worker
work(&auth) does the background work: removes expired rows, sends mails that wait, runs the work of places.
- Axum or your own server: call it every few seconds in a loop, in the web process (step 1) or in a worker process
of the same binary, for example with
ROLE=worker(steps 2 and 3). - Loco: register the task
auth_workand run it from Loco’s scheduler, for example every 10 seconds. - Any number of processes can run it. Each piece of work holds a lease (a mail one minute, a place five minutes).
provisionandremovemust be safe to repeat.
Rules in every step
| Subject | Rule |
|---|---|
| Sessions, tokens, devices, API keys, memberships | Only in the users’ database |
| The check | Once for each request, before your code. Never cached |
| Logout, removal, disabled user | Works from the next request, in every process |
| Check not possible | 503 |
| WebSocket, event stream | Checked when it opens; your server closes it after 5 minutes† at the latest |
| Signed link (media, download) | Works until its own end, at most 10 minutes†, also after a logout |
| What a person may do | Your code, with your data |
| Place | By tenant or by user. An application chooses one, never both |
Patterns we do not support
| Pattern | What goes wrong |
|---|---|
| Sessions in application databases | Each logout must reach every database |
| A JWT that processes check without the database | Logout and removal take effect only when the token expires |
| Cached answers of the check | The same, for the time of the cache |
A header like X-User from a proxy | Whoever reaches the process can write it |
| A proxy that checks sessions for the processes | A second way in, with its own rules |
| SQLite on several machines | Syncing a database is a problem of its own |
| Routing tenants to machines at the load balancer | Not needed: any process serves any tenant |
What lives where
- With the users: users, passkeys, sessions, tokens, devices, invites, events, the outbox, rate limits, single-use marks, leases; tenants, memberships (with your columns) and places with their features; your own tables about people.
- Your data: everything else, with the user ID and tenant ID as plain values.
auth-chad memberships postgres role:text=member seat:int?prints the migration for your membership columns and the Rust declaration.
Moving between steps
| Step | What you do |
|---|---|
| 1 → 2 | Move to PostgreSQL. Put a load balancer in front, set with_proxies. Run the worker. Start more processes |
| 2 → 3 | Move your tables to data servers. Remove foreign keys to users. Implement Placement |
| Back | The same steps in the other order |
Costs of step 3
| You lose | What the library gives |
|---|---|
| One transaction for auth and your data | An outbox: provision after signup, perhaps more than once |
| Foreign keys and joins to users | The user ID |
| A consistent backup | place::in_states lists places that wait for their data |
| Few connections | Nothing: keep pools small, or share a database between customers |
| Valid sessions after restoring an old users’ database | auth-chad after-restore prints SQL that ends all sessions, tokens, codes and API keys |
Capacity of one process
This estimate uses published SQLite measurements; auth-chad has no benchmark of its own yet. On a 5-dollar machine, one published test reached about 300,000 reads and 28,000 write transactions a second. One million people a day, with 100 requests each, is about 23,000 reads and 6,100 writes a second at peak: well inside one process.
Status: all three steps work today, for SQLite and PostgreSQL, with tenants, memberships and places.