Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

A site’s own store, and a bus of Rust crates

Status: started 2026-11-17 on branch site-store. Done: the benchmarks (bench/) and portal_core::store, which began with JetStream and redb behind it and is now redb alone. Nothing uses the store yet. From step 3 on, the order below is replaced by Portal 1.0: 1.0 leaves NATS with no migration.

Why

NATS does two jobs for portal: it is the database (the EVENTS log, a KV bucket per record type, sessions, cursors, the chain) and the bus (the mail, payments, credentials and rooms streams, the access and ranking questions, the announcements n8n hears). Most of what portal has had to build around it is the database job done on a message broker:

  • a decision writes its event, then its record, and the gap between the two is why answers.rs has SETTLE and the mend;
  • sites on one account are kept apart by name prefixes and adoption (namespace.rs, adopt.rs and adopt’s tests, about 3,500 lines);
  • reads are round trips, which is why #134 keeps every bucket in memory, and why each load of an aggregate’s log makes a consumer;
  • sign-in is a password in NATS_URL, and whoever holds an account’s password reads every record on it;
  • about 4,000 lines of tests need a live JetStream.

The numbers are in bench/README.md: a decision is 7.5 ms on JetStream today, 3.3 to 3.7 ms through the store on JetStream, and 1.5 ms on redb with every commit waiting for the disk.

What goes where

Storage: redb, one file per site, in the unit’s StateDirectory, opened by the site’s process alone. A batch is one transaction.

Bus: irpc, over iroh when it leaves the process. irpc is a channel and no encoding in-process, and QUIC across processes or hosts, with the same protocol either way. iroh dials by key, so a program is known by its key, not a password.

NATS todaythen
EVENTS with expected_last_subject_sequencethe store’s log, an append naming the aggregate’s last event
KV buckets, sessions, cursors, chain, invitesthe store’s records
mail, payments, credentials, rooms streams and their consumersan outbox written in the decision’s batch; the program it is for pulls it over irpc and acknowledges each
portal.mail.received, portal.payments.changedirpc calls into the site, deduplicated by id
portal.access.<site>.may, learn.rank, $SRV.PINGirpc requests, the asker known by its key
ANSWERS and n8n’s NATS triggera webhook from the outbox, retried until n8n takes it (irpc is Rust to Rust only)
the content reload signalan irpc call from the deploy

One rule for every program beside a site: it dials the site, the site serves, and work is pulled and acknowledged. Only the collaborator needs the site’s address; the site answers only the keys its configuration names, protocol by protocol.

Content decides how far a site reaches

No new key: what a site needs is what its content already says.

  1. Pages, forms, lists, states: one process, one file, nothing listening but HTTP.
  2. A program beside it: mail: on a state (gdo), payment: (kron), search: (learn), rooms (the chat). The same irpc protocol, linked into the binary or a separate unit on the host; which is the instance’s configuration, not code. kron stays its own unit because it holds the Vipps keys, learn because it holds the model.
  3. A program on another host: an iroh endpoint dialled by key, the same protocol.

iris already knows what content uses; portal refuses to start when the content needs something the instance does not provide, as PAY_URL decides what payment: may do today. One build with all of it compiled in, since PORTAL_RELEASE pins one artifact for every site; only what the content needs is started.

The chat becomes kino

cnats is named for its bus, so it is renamed when it leaves it: kino. It keeps its rooms in its own process and its history in its own redb file, and meets portal on one protocol, dhd/rooms/1, which portal serves:

todaydhd/rooms/1
KV <ns>_portal_groups, which the chat readsGroups: every group, then each change, as a stream
portal.access.<site>.mayMay: the same Query, strong included
<ns>.portal.rooms.<group> in <ns>_ROOMS, read by <ns>-cnats-relayListen: what portal says, from its outbox, each acknowledged
<ns>.portal.rooms.thread, read by <ns>-portal-threadsThread: the chat tells portal

The chat’s rooms, threads and call signalling become a broadcast per room inside it. It depends on dhd for the first time, so its copy of portal’s names in site.rs goes. Its first release off NATS ships under the new name, importing its history once, with replaces=('cnats') in the PKGBUILD.

What it costs

  • irpc does no versioning, and postcard does not take a changed message the way JSON on NATS does. The version goes in the protocol name (dhd/mail/1), two run side by side during a change, and what content shapes travels as JSON inside the message. dhd’s types are JSON-shaped today (tagged enums, a serde_json::Value), so that is how the first protocols carry them.
  • redb opens in one process. portal import, bratac job file, the lint’s replay and the kit worker ask the site over irpc, or open the file while the site is stopped.
  • No nats account backup. A snapshot from a read transaction, which varde mirrors to a peer, the job it already does. The log only grows, so a live replica later is “events since N” over iroh.
  • nats kv get goes; a portal db subcommand takes its place.

Order

  1. Benchmarks, before anything moves (bench/).
  2. portal_core::store: one API, JetStream and redb behind it, the same checks on both.
  3. The decision path through the store: aggregates and answers read the record and its log once and write one batch. Half the decision on JetStream already; on redb the mend has nothing left to mend.
  4. A site on redb: Instance holds a Store, a site’s configuration chooses its file, and portal migrate copies a site’s buckets and log into it. Once every site has moved, the mend, SETTLE, namespace.rs and adopt.rs go.
  5. The outbox, in the same batch, relayed onto today’s subjects so gdo, kron and the chat do not change yet.
  6. irpc protocols in dhd; each collaborator moves when it is ready. The chat moves as kino.
  7. n8n’s webhook.
  8. NATS goes once no site’s content needs it.