Deep dives Chapter 2 of 9

The log of facts

How Chatto records each change as a fact in one ordered log, and how an append stays safe.

5 min Checked against its records on 2026-12-01 Source file

Chatto does not keep a table of rooms that it updates in place. It records what happened. A user posted a message. A member joined a room. An administrator changed the server name. Each of these is a fact, and Chatto appends each fact to one ordered log.

This method is named event sourcing: the list of events is the source of truth, and Chatto computes the current state from it (ADR-033). The log is a JetStream stream named EVT. A stream is an append-only log that the NATS server stores on disk. Chatto calls the full design the Loom Architecture (ADR-073).

What a fact is

A fact is one Event value. It has a small wrapper and a payload. The wrapper holds the event ID, the creation time, and the ID of the user who caused the event. The payload is one of many event types, for example message_posted, user_joined, or room_created.

Chatto encodes each event with Protocol Buffers, a compact binary format with a typed schema (ADR-008). Each field has a number. A new server version can add a field, and it can still read the events that an old version wrote. This is important, because stored events stay in the log.

These stored shapes are not the public API. The API handlers convert a stored event to a separate public message (ADR-008). The stored shapes have their own protobuf package, chatto.core.evt.v1, apart from all other internal records (ADR-084).

One stream, many subjects

All domain facts go into the one stream EVT (ADR-034). ADR-034 is partially superseded: later records define how events reach connected apps. The single stream and its subject layout are still in effect.

In NATS, each message has a subject, which is a name made of tokens with dots between them. Each fact in EVT has a subject of this shape:

evt.{aggregateType}.{aggregateId}.{eventType}

evt.room.R1.message_posted
evt.room.R1.user_joined
evt.config.server.server_name_changed

An aggregate is one thing that has its own history: a room, a user, an invitation. The third token is its ID. An aggregate that exists only once, such as the server configuration, uses a fixed ID such as server.

The code does not write the last token by hand. One function, EventTypeOf, computes it from the payload type. Thus the subject and the payload cannot disagree.

The subject makes filters possible. A reader that needs only joins asks for evt.room.*.user_joined, and the NATS server sends nothing else.

The stream gives all facts one order. But the order between two different aggregates has no meaning for the domain. Chatto relies only on the order of the facts of one aggregate (ADR-073).

Event identity

Each event gets a random ID, a NanoID, before Chatto appends it. The API and the apps use only this ID (ADR-026).

JetStream also gives each stored message a sequence number. Chatto uses this number internally, for example to wait for a write. It never shows the number to an app.

The event ID has a second task. Chatto sends it to JetStream as the message ID of the append. If a retry sends the same event again in a short time window, JetStream does not store it twice.

How an append stays safe

Two requests can arrive at the same time. Each of them reads the state of room R1, makes a decision, and appends a fact. Without protection, the second request can decide from state that is already old.

Chatto uses optimistic concurrency control (OCC). The writer first notes the sequence number of the last fact that matches a filter, for example evt.room.R1.>. Then it makes its decision. The append includes that number as a condition. JetStream accepts the append only if no newer fact matches the filter (ADR-033).

If a different writer was faster, JetStream rejects the append. Chatto then reads the new state and makes the full decision again. The shared framework repeats this up to five times (ADR-068). ADR-068 is partially superseded: its special rule for permission changes is gone. The boundaries that it defines are still in effect.

The writer selects the boundary. The usual boundary is one aggregate, so a post in room R1 does not conflict with a post in room R2. A boundary of the complete stream exists, but it is for rare cases, because each unrelated fact then causes a conflict.

Not every fact needs a condition. A fact that does not depend on state, such as an audit fact, has no guard (ADR-033). When one command must write more than one fact, Chatto can append them as one batch that succeeds or fails as a unit.

What does not go in the log

A fact stays in EVT. Chatto sets no time limit on the stream (ADR-034). One deliberate exception exists: the text of a message is in a separate message_body fact, and Chatto can delete that fact when the message is deleted (ADR-033).

Thus the log is only for facts that Chatto needs to build its domain state again. Other data has other places (ADR-036):

  • RUNTIME_STATE is a key-value bucket for operational records where only the latest value is important. Examples are the read position of a user in a room, a push subscription, and a session record.
  • Volatile state that the processes share is in the memory-backed bucket MEMORY_CACHE. A typing indicator is only a transient signal, and Chatto never stores it.
  • Files and images are in an object store.

ADR-036 is partially superseded for one detail, the expiry of session records. The boundary above is still in effect.

The names of these resources, their subjects, and their encodings are persisted contracts. A change needs a migration plan and is not a simple refactor (ADR-114).

Read the code

The records behind this chapter

Esc