Several instances
One instance needs nothing more. This page is for running several behind a load balancer: what they must share, what Redis adds, and what stays per instance.
Quick start
- Build with the
redisfeature and setREDIS_URL([redis] url): one standalone Redis (Redis Cluster is not supported). - Give every instance the same
APP_KEY,DATABASE_URLandredis.prefix; two applications sharing a Redis need different prefixes (defaultrustcms:<app.name>). - Share uploads: S3 (feature
s3), or one volume mounted by every instance (Storage). - Run
migrateonce per deploy, before the new instances start. - Keep the clocks synchronized (NTP): the queue and the scheduler compare times written by different instances.
# config/redis.toml (or REDIS_URL / RUSTCMS_REDIS__PREFIX); the file is the [redis] section
url = "redis://redis.example.com:6379/0"
prefix = "rustcms:blog"API mode
An instance in API mode serves the application API only: your ApiRoutes, the API guard's auth routes (/api/v1/auth/token, …/jwt, …/refresh, …/me) and the routes you mount yourself. Scale these out behind a load balancer; keep at least one instance in full mode for the admin panel.
rustcms serve --api-only # or [app] mode = "api" / RUSTCMS_APP__MODE=api- Left out: the admin panel and its assets, the admin API, the panel's sign-in routes, and every
AdminRoutesroute (streams and sockets too). Every request underauth.admin_pathanswers a JSON 404 before routing, whatever its method, and no session is loaded. - Declared as in
full: permissions, job kinds, schedules, commands and encrypted columns, so every instance agrees (permission:syncfrom any of them, jobs enqueued by one and run by another). - Background work off by default:
queue.enabledandschedule.enabledleft out arefalsein API mode; set them totrueto run workers or the scheduler there. Each instance still sweeps the files it staged. - Your own code: check
state.mode()(orapp.mode()in a provider) before mounting panel-only routes withapp.routesor starting services only the panel uses. servein API mode logs a warning for what several instances need and this one lacks: a SQLite database, no Redis (rate limits per instance), alocalstorage disk (one shared volume, ors3).route:listlists the routes of the mode it runs in:RUSTCMS_APP__MODE=api rustcms route:list.
A typical deployment:
| Instances | Command | Serves |
|---|---|---|
| 1 | rustcms serve | the admin panel (the load balancer sends auth.admin_path here), the scheduler |
| N | rustcms serve --api-only | the API, no sticky sessions needed |
| M | rustcms queue:work | queued jobs (mail, exports, imports, yours) |
The instances run the same build with different configuration. An API instance's configuration may add the opt-in caches of Credential revocation when it serves heavy authenticated traffic and a second's delay for revocations made elsewhere is acceptable:
# config/app.toml on API instances (or RUSTCMS_APP__MODE=api)
mode = "api"
# config/auth.toml on API instances: optional
permission_generation_cache_ms = 1000
[jwt]
session_cache_ms = 1000All of them share APP_KEY (and APP_PREVIOUS_KEYS), DATABASE_URL, the storage, redis.prefix, http.trusted_proxies (the load balancer's addresses) and APP_URL: links in mails are built from the APP_URL of the instance that runs the job, so it must reach a full instance. Size database.max_connections so that all instances together stay under the database's limit. Health checks use GET /health (it pings the database).
Before you scale out
API mode removes the panel; what a public API needs beyond that is the application's to decide. Check these before putting API instances in front of a site:
- Who may read what. Your API routes filter drafts, scheduled dates and trashed records themselves: a resource's
scope()and policy protect the panel's operations, not the queries your handlers write. - Files. The only built-in file route is the panel's (signed-in users, under
auth.admin_path, refused in API mode). Serve the files your readers may see from an API route of your own (rustcms::storage::of(&state)), with your rules for which ones and how long they are cached (Headless rendering). - Browsers calling the API. There is no CORS layer: a frontend server calling the API needs none; for browsers on another origin add one with
app.middleware(or at the proxy), with the origins you allow. - The reader's address. When a frontend server calls the API for its readers, every request comes from that server: per-IP limits would count all readers together. List the frontend servers in
http.trusted_proxiesand have them sendX-Forwarded-For. - Fresh pages. A frontend that caches pages learns about changes only if you tell it: dispatch a job from your resources' hooks (and media hooks) that calls it.
- Paging.
ApiPagepages by number: rows added or removed while a reader pages may repeat or be skipped. Page by key (id > last) where that matters. - Releases. Every instance runs the same build. Run
migrateonce, before the new instances start, with migrations that only add, so the instances still running the previous build keep working.
Redis configuration
| Key | Default | |
|---|---|---|
url (REDIS_URL) | unset: no Redis | redis://host:port/db; empty counts as unset |
prefix | rustcms:<app.name> | starts every key of this deployment; may not be empty or contain whitespace |
- Boot fails when Redis cannot be reached, when the prefix is invalid, or when
urlis set but the application was built without theredisfeature. - Commands time out after a second, connecting after two.
What Redis is used for
- Rate limits (sign-in,
Throttle<N>, uploads, …) are counted in Redis for all instances, atomically across the keys of one check. If Redis does not answer, checks fail closed (500).
The cleanups need no Redis. The auth, idempotency, media and exports cleanups are framework schedules (rustcms.auth-prune, rustcms.idempotency-prune, rustcms.media-prune.<disk>, rustcms.exports-prune.<disk>): each slot is claimed once in the database and run only by serve with the scheduler. Files an instance stages on its own disk (uploads, exports and imports being written) are swept by that instance: every serve and queue:work removes its abandoned ones, older than an hour, every 10 minutes. See Data retention.
Permissions need no Redis: a change made on one instance applies on every instance from the next request (the permission cache follows a counter kept in the database). API instances under heavy load may reuse that read, and a JWT's sign-in, for a short time with auth.permission_generation_cache_ms and auth.jwt.session_cache_ms (off by default; changes then reach them within that time: Credential revocation).
The queue and the scheduler need no Redis either: they coordinate through the database (see below).
What stays per instance
- Without Redis, several instances still work, but each keeps its own rate limits: run one instance unless Redis is configured.
- Password-hashing and upload concurrency limits are always per instance (each bounds its own CPU and disk).
- Runtime branding is cached per instance:
AdminBranding::invalidate()clears this instance only; the others load it again once their[admin] branding_ttl_secshas passed. Redis does not broadcast it. - Realtime events are per instance too. Each instance runs its own hub producers, and the
[realtime]connection limits apply to each one. A change on one instance still reaches every open stream within a recheck, because rechecks read the database.
The queue on several instances
Every instance may run queue workers: a job is claimed by one worker at a time, with a lease of queue.lease_secs (60) renewed while it runs. A lease counts until queue.clock_skew_secs (5) after it ends, so keep clock differences below that. Run workers in their own processes with rustcms queue:work and set queue.enabled = false on web instances if you want to scale them apart (Running the queue).
Your own periodic jobs
Use a schedule. The demo declares its own from the bootstrap:
/// The demo's schedules.
pub fn schedule(app: &mut AppBuilder) {
app.schedule(Schedule::every("posts-ping", Duration::from_secs(3600)).job(|| PingPosts).without_overlapping());
app.schedule(
Schedule::cron("posts-digest", "0 6 * * *")
.expect("a valid cron expression")
.task(|ctx| async move {
let posts = post::Entity::find().count(ctx.state.db()).await?;
Ok(Some(format!("{posts} posts")))
})
.timeout(Duration::from_secs(60)),
);
}Schedules run only in serve. Each slot is claimed once in the database across instances, with or without Redis, and enqueues the job (or runs the task); the job must still be safe to retry. Turn the scheduler off on instances that should not run it with [schedule] enabled = false.
rustcms::jobs::exclusive(&state, name, ttl, f) is a separate, best-effort helper. It runs f unless another instance holds a Redis lease named name (held at most ttl), and returns whether this instance ran it. Without Redis it always runs. If Redis does not answer, f runs anyway, and a lease can expire during a long run: write work that is correct when two runs overlap (claim rows in the database), the lease only saves work. Prefer a schedule for anything periodic.