You can run several Chatto processes for one server. Each process is a replica. All replicas connect to the same NATS cluster and run the same code. No replica is the leader, and no replica asks a different replica for permission.
This works because the shared data is in NATS, not in a process. Each replica reads the same log and builds its own state in memory.
The Loom Architecture
The overall design has a name: the Loom Architecture (ADR-073). The name is short for Log-Oriented Outcomes and Materializations. It has three parts:
- The log. Each durable domain decision is a fact in one event log, the
EVTstream. - Materializations. State that is computed from the log, such as a projection. Chatto can discard it and build it again.
- Outcomes. Reliable work that a committed fact causes, such as a webhook request. A durable worker does it.
One replica is sufficient to serve a community. More replicas are mainly for redundancy (ADR-115). Each replica runs the complete set of domain services, and a request stays in the replica that received it.
A projection is state in memory that a process builds from the facts of EVT. Each replica builds its own projections from the same log (ADR-033).
Take two replicas, A and B. A user posts a message through A. A appends the fact to EVT, waits for its own projections, and answers. B reads the same fact a moment later. For a short time, B is behind A.
Thus, a projection must not cause effects. If a projection sent an email, each replica would send it.
Atomic operations, not locks
A lock in memory protects only one process. A second replica cannot see it. Thus, Chatto never uses an in-process lock for correctness. It uses the atomic operations of NATS:
- A conditional append. A replica appends a fact to
EVTonly if no other fact changed the same subject since it decided. JetStream does this check and the append as one step. If the check fails, the replica decides again from the current state. - A conditional key write. A key-value bucket is a store of named values in JetStream.
Createsucceeds only when the key does not exist.Updatesucceeds only when the value did not change since the read.
For example, a moderator removes a user from a room through replica A. At the same moment, that user posts a message to the room through replica B. B checked the membership before the removal, so it tries to append. JetStream rejects the append, because the room changed. B checks again, finds no membership, and rejects the post.
Some background work needs only one replica at a time, such as the repair of call state. For this, a replica takes a lease: a key with a time limit. The code treats a lease as a way to prevent duplicate work, not as a guarantee. The work must stay correct when two replicas briefly do it together.
Two buckets for state that is not history
Not every value belongs in the log. Chatto has two key-value buckets for state of which only the latest value is important.
RUNTIME_STATE is stored on disk and survives a restart (ADR-036). It holds operational records: the position to which a user read a room, session records, and push subscriptions. It keeps one version for each key, and a key can have a time limit.
MEMORY_CACHE is stored in memory only. It holds volatile state that all replicas must see. Examples are the live presence of each user, which expires after 60 seconds, and the leases.
The bucket names, keys, and value formats are persisted contracts (ADR-114). During a rolling upgrade, replicas of two versions run together, so a format change needs a safe migration.
Sharing work between processes
Background work must not run once for each replica. JetStream solves this with a durable consumer: a named read position on a stream, stored in NATS. All replicas of one worker role pull from the same consumer, and JetStream gives each message to one of them. The position belongs to the deployment, not to a process (ADR-069).
Background jobs use this mechanism (ADR-123). When a replica crashes during a job, JetStream gives the job to a different replica.
Realtime delivery does not share work in this way. Each replica runs one hub that receives every live event and sends it to its own connections (ADR-049).
Retries and credential checks
A message post can succeed while its response gets lost. The app then sends the request again, possibly to a different replica. ADR-117 prevents a duplicate.
The app sends a UUID with the post. The replica creates the key message_post.{actorId}.{uuid} in RUNTIME_STATE with Create. The record holds a new message ID and a keyed hash of the request. Only one replica can create the key. A retry finds the record and uses the same message ID. If EVT already contains that message, the replica returns it. If not, the replica appends it.
The key expires after 30 minutes. Only EVT proves that the message exists.
When an administrator deactivates an account, a fact goes to EVT. A replica that is behind does not know this yet. To be exact, a replica can ask NATS for the newest position of that user and wait until its projection has applied it. This strict check costs requests to NATS for each API request.
ADR-127 sets a limit for the delay. After a strict check accepts a user, the replica remembers this for 60 seconds. During that time, it checks the credentials of that user against its own projection only.
Only a "valid" answer can be old. If the projection says that the credential is not valid, the replica runs the strict check before it rejects. Thus, the worst case has a limit: a replica that is behind can accept a revoked credential for 60 seconds at most.
Read the code
- docs/architecture/INDEX.md: the system map, with the rule for more than one replica.
- docs/architecture/runtime-state.md: each key family of
RUNTIME_STATEandMEMORY_CACHE. - cli/internal/core/storage.go: the configuration of each stream and bucket.
- cli/internal/lease/lease.go: the lease for background work that one replica does.
- cli/internal/core/message_idempotency.go: the claim that makes a message post safe to retry.
- cli/internal/core/auth_generation_window.go: the 60-second window for credential checks.