Several copies
How more than one API process shares documents, Pip runs, and rate limits through Redis, and how to tell the copies apart.
Read this before you change collaborative editing, Pip, or rate limits, or before you run more than one copy of the API. Since API 491254f1, production runs two API copies and staging runs one. Anything that lives in one process's memory can now break in production without ever failing on staging.
What is shared, and what is not
Each copy keeps some things in memory. Everything the other copy needs goes through Redis.
| Shared through Redis | Stays in one copy |
|---|---|
| Edits to an open document, and the save lock | The open document itself |
| Close and "stateless" messages to document viewers | The Pip run's driver |
| Which copy owns a Pip run, and stop and undo signals | The connection kept open for Pip's undo |
| Rate-limit counts | Its Postgres connection pool |
| A heartbeat saying the copy is alive |
Telling the copies apart
Every process gets an identity when it starts (INSTANCE in libs/shared/src/instance/instance.ts).
| Field | Value | Where you see it |
|---|---|---|
replica | RAILWAY_REPLICA_ID, or the hostname on a laptop | The replica field on every log line, and the X-Served-By response header |
boot | 8 random characters, new on every restart | Part of id |
id | replica:boot | GET /health/detailed lists every copy that reported in the last 30 seconds |
Outside production, the Hub's document editor also shows Server and the first 8 characters of the replica it is connected to.
Collaborative documents
A browser's /collab socket connects to one copy, and that copy holds the document. CollabSync, a subclass of the Hocuspocus Redis extension, keeps the copies in step:
- Edits travel over Redis, so two people on different copies see each other's changes.
- Saving takes a Redis lock. If another copy holds it, the save retries every 100 ms for up to 6 seconds, then fails.
- Closing a document (for example when its year is locked) is one message on
collab:signalthat every copy acts on. - Reading the live text (
readLive) checks this copy first, then asks Redis whether another copy has the document open and reads it there. - Shutdown saves every open document first, and waits up to 10 seconds.
@hocuspocus/extension-redis is pinned to 4.5.0, and @hocuspocus/common is pinned so the server and the extension share one SkipFurtherHooksError. After any Hocuspocus upgrade, run collab-two-servers.e2e-spec.ts: CollabSync overrides two hooks and depends on that error class.
If Redis is down, collaboration logs a warning and carries on as if no other copy had the document open. There is no separate refusal.
Pip runs
Claim. The first copy to set pip:owner:{requestId} owns the run. A second request while the owner is alive gets Pip is already working on this request. A dead owner is replaced.
Stop and undo. Either copy can be asked. The request goes out on pip:signal, and the owner acts. An undo waits up to 10 seconds for the owner to reply. Undo stays available for 30 minutes after a run ends.
Clean up. Every 60 seconds one copy sweeps runs whose owner died, or that never started within 5 minutes.
Rate limits
Limits are counted in Redis, so two copies share one total. sharedThrottler(limit) uses a 60-second window:
| Module | Requests per minute |
|---|---|
| Auth, Device pairing, Enquiry | 10 |
| Guardian card | 20 |
| Library | 120 |
| Short link | 600 |
API keys have their own guard (ApiKeyRateLimitGuard): 60 a minute per key, plus 10 a minute per IP for a key that is not secret. A full bucket answers 429 Too many requests.
The clinic API counts in memory
The clinic API's key limiter (apps/ehr) keeps its counts in the process. That is fine with one clinic copy. Running two would double the effective limit.
Before you add a copy
- Redis is reachable. The server, worker, and audit app all refuse to finish booting without
REDIS_URLand a Redis that answers. - The database has room. Each copy holds up to
DATABASE_POOL_SIZEconnections (default 30). Copies times pool size, plus the worker and audit pools, must fit the database's connection limit. - The replica count is in git. On API
maind0fbb403,apiReplicasis 1 on staging and 2 on production, andworkerReplicasis 1 and 2.
Where the code lives
Tests
The two-server tests are *.e2e-spec.ts files, which pnpm test skips. Run them with pnpm test:e2e and a local Redis.
| Test file | Proves |
|---|---|
collab-two-servers.e2e-spec.ts | Edits, carets, closes, and the save lock work across two copies |
pip-request.store.e2e-spec.ts | Signals, replies, sweeps, and a single owner |
instance.registry.e2e-spec.ts | Copies see each other, and a dead copy drops out |
api-key-rate-limit.guard.e2e-spec.ts | Two copies share one key and IP budget |
shared-throttler.e2e-spec.ts | Two copies share one visitor total |
Not covered by any test
Two browsers editing one document on different copies, and a Pip run started on one copy and stopped or undone from the other. Staging now runs one API copy, so these can't be checked there as it stands: raise staging's apiReplicas to 2 for the test, or the first place they meet two copies is production.