The EVT stream holds facts, but a request needs answers. Which rooms exist? Who is a member of room R1? What are the last 50 messages? Chatto does not search the log for each question. It keeps the answers ready in memory.
A projection is state that Chatto computes from the log. It reads the facts in order and updates its data for each one. The log stays the source of truth (ADR-033). Chatto can discard a projection at any time and build it again.
From facts to state
Take the members of a room. The projection starts empty. It reads a user_joined fact and adds the user. It reads a user_left fact and removes the user. After the last fact, it holds the current member list.
Most projections are Go data structures in the memory of the process. Each Chatto process builds and owns its own projections (ADR-033). No process shares them with another.
A projection has two duties: it names the subjects that it reads, and it applies events in stream order (ADR-054). All other abilities are optional.
A projection only computes its own state. It must not send an email, call a webhook, or publish a live event when it applies a fact (ADR-073). Each process applies each fact, so such an effect would occur once for each process. Background workers do that work, and a different chapter explains them.
Projectors, components, and one apply barrier
Three terms describe the structure (ADR-088):
- A projector is the engine. It reads events from the log, tracks the sequence number of the last applied event, and knows if the projection is ready.
- A projection component is one focused part of the state, with the code that updates it. Examples are the room directory, the reactions, and the user profiles.
- A componentized projection combines related components behind one projector.
Chatto has one large componentized projection, the Server Content View (ADR-089). It holds the state that apps can read: rooms, timelines, threads, reactions, calls, assets, users, roles, and more. ADR-089 is partially superseded: the timeline no longer keeps full messages, as a later section explains.
The important part is the apply barrier. For each event, the projector takes an exclusive lock. Each component first prepares its change and does not touch live state. If one preparation fails, the projector discards all of them and marks itself as failed. If all succeed, the projector commits the changes, advances its sequence number, and releases the lock.
Thus all components always show the same position in the log. A reader never sees a new message in a room that does not exist yet in the room directory.
Some state stays outside the Server Content View and has its own projector, for example authentication data, invitations, and notifications. The reasons are privacy or a different lifecycle (ADR-089).
How a writer sees its own write
A projection applies a fact a short time after the append. A request that writes and then reads could miss its own change. Chatto prevents this (ADR-033).
A successful append returns the sequence number of the new fact. The writer gives this stream position to the projector and waits. The projector answers when it has applied that sequence number. Then the request reads the projection and returns.
This promise holds for each process alone. A request on a second process sees the new fact when that process applies it, usually a very short time later. Chatto accepts this short delay (ADR-033).
Start: replay and optional snapshots
Memory is empty when the process starts. Each projector opens its own ordered consumer on the stream. A consumer is a read position that the NATS server keeps for one reader. This one is temporary and belongs to one run of the projector.
The projector notes the last matching sequence number in the stream as its target. It then reads the facts from the start of the stream and applies them. This is a replay. The server does not finish its start before every registered projector is current.
A snapshot makes the start faster. It is a saved copy of projection state with the sequence number that it represents. The projector loads the snapshot and replays only the facts after that number (ADR-050).
Snapshots are off by default. The configuration key core.projection_snapshots enables them.
A snapshot is disposable. If it is missing, damaged, from an incompatible version, or not decryptable, the projector ignores it and does a full replay. The removal of all snapshots makes the start slower and changes nothing else (ADR-050).
A snapshot is also encrypted. Projection state contains user data, so Chatto compresses and then encrypts each payload before it stores it. The key comes from the secret key of the server. A change of that key makes the old snapshots useless, and the server does a full replay.
The Server Content View saves its components as one group with one shared sequence number. The projector restores the complete group or nothing (ADR-088). When more than one Chatto process runs, a lease selects one of them to write the snapshots.
Keeping memory small
State in memory has a cost for a server with a long history. Two decisions reduce it.
The timeline component keeps only a compact index for each message: IDs, the time, the type, and the sequence number. When a request needs the full message, Chatto reads it from EVT by its sequence number and keeps recent reads in a cache (ADR-090).
Event IDs are strings, and many indexes refer to them. Chatto stores each ID once in a table for the process and uses a small number in its place (ADR-110). These numbers never leave the process.
Read the code
- cli/internal/core/projection_wiring.go:
initializeCoreProjectionsregisters each projector and the components of the Server Content View. - pkg/events/projector.go: the projector with replay, the apply barrier, and
WaitFor. - pkg/events/component_projection.go: the componentized projection with its prepare and commit steps.
- cli/internal/core/server_content_view.go: the Server Content View.
- cli/internal/projectionsnapshot/repository.go: the storage of encrypted snapshots.
- docs/architecture/projections.md: the maintained inventory of projections.