Local-first state, live across devices, through a relay that can read none of it, or no relay at all.
The protocol is SPEC.md. This is the reference implementation, in Odin, with a query client in Luau.
zsp/ the protocol: store, merge, windows, reconciliation, text, keys, calls. No I/O, threads or clocks
find/ answering questions about it: an index made when first asked, kept encrypted, and never replicated
pipe/ carrying it: UDP links, WebSocket, TLS, STUN. No I/O either
host/ files, sockets, one thread, one event loop
zspd/ a device's node, or a self-hosted relay: one consumer of the above
relay/ the same relay as a Cloudflare Durable Object: zsp/ compiled to wasm, and plumbing
luau/zsp/ the query client
clients/ a whole client in the Python standard library, to show how little one needs
vectors/ what two implementations must agree on
docs/ how to lay out who sees what, and structured state; examples/ has it running
One kind of thing, one rule. Everything is a cell, written once: a value, a move, a deletion, a run of text. Replicas unite their cells, and a cell kills what it supersedes. The rule needs no key, so a relay that cannot read still merges.
Everything is a range. Rows sit in one ordered index of (list, order, row). A list is a range, a page is a range, a scope is a range; a subscription is a
set of ranges. The index is a B+tree that also sums fingerprints, so any two replicas compare any range in logarithmic time. That one
operation is sync, resume, paging, scrollbars and recovery. There are no version vectors, no per-peer state and no queues.
Questions are asked of what is there. Filters of any shape, words, phrases, misspellings, substrings, references either way between rows, JSON, any order, sums and facets: each answer stays true while it is open, and costs nothing once it is not. None of it is stored, replicated or seen by a relay. What answers is an index that a device makes of its own rows, of the columns asked about, when they are first asked about; it may be deleted at any time. A device that holds little asks one that holds everything, and says whose answer it has.
Whose it is, is yours to say. Where a scope is signed, every cell is known to be of the device that wrote it, and that of the user your own sign-in says: by the roster for a device somebody has, or by the host of one that serves many, if you let it vouch. No application can say otherwise, no relay sees it, and it can be shown, searched, ordered and counted like any other column.
Documents are tables. Maps, lists, records and trees, nested as deep as you like, are plain Luau tables: set a new one and what differs is written; what others write comes back with whatever did not change the very same table. Lists keep their order as text does, so nothing interleaves and nothing runs out of room. It is rows and texts underneath, so it costs the protocol nothing, and a document too big to hold can be asked about, as any rows can, at any depth (docs/mirror.md).
The log is the database. A sealed cell is appended once. The bytes on disk are the bytes on the wire are the bytes on the relay.
What was written long ago is written together. A text typed a word at a time is a cell a second. Whoever writes the next has a hundred and more of them put into one, and those into one, so that a text of 8,000 pieces is at most some 200 cells to fetch, check and open, in three quarters of the room they took, or less; and everything that was ever said about it still holds, whoever is still on the old ones.
Nothing happens unless something happened. An idle node makes no system calls. An idle link sends no packets and sets no timer.
./x bench, on one core of a Ryzen 9 9950X and a virtual disk, a million sealed 200-byte rows:
| memory, writing a million rows | 3.5 MB private |
| memory, holding them open | 2.0 MB private, the rest being file cache the system may take back |
| a running daemon | 0.4 MB private, one thread |
| an idle daemon | 0 wakeups, on Linux, macOS and Windows alike; one ping per relay per 45 s |
| written to disk | 560 bytes a row, 1.8 × the row itself, indexes and all |
| one small durable write | 0.26 ms; 4 KB, the one block it lies in |
| opening the store | 4 to 5 ms |
| a page of 50 rows found, read, decrypted | 0.14 ms |
| fingerprint of any range of the million | 0.2 µs |
| two level replicas of 20,000 rows reconnecting | under 400 bytes |
| one difference among 20,000 | under 6 KB |
| a streamed token, device to relay to device's screen | 1 ms |
| a call to another device through a relay | 0.15 ms on one machine; between continents, the ping |
| a link | 400 MB/s, one core doing both ends |
| checking all six platforms | 1 s |
| building the daemon | 1 s, or 10 s optimised; on an M5 Max, 0.3 s and 4.9 s |
| building and running the core's tests | 5 s; everything, 9 s |
./x bench find, the same machine, a million rows with a name, a number, a flag and twenty words of text, encrypted on disk. Every answer is the first 50 in order, and the exact count:
| memory, holding the rows and an index of all of it | 5.5 MB private, of which the rows take 3.9 |
| on disk | 255 bytes a row, for five columns, one of them every word |
| making it, when first asked | 1 s, and 2 s for each column; 11 s for the words; never more than a few tens of ms at a time |
| everything, newest first | 3 µs |
| everything, by a number | 32 to 40 µs |
| a name | 55 µs |
| a name, a range and a flag, by number | 0.3 ms |
| a wide range: 600,000 rows | 8 ms |
| a rare word; a word in 400,000 rows; a phrase | 20 µs; 5 ms; 8 to 11 ms |
| a word begun; misspelt; part of one | 2 ms; 5 ms; 3 ms |
| the sum of every number | 2 µs |
| the ten commonest names among 333,000 rows | 14 ms |
| one more row, known to all of it | 0.12 to 0.17 ms |
| opening it again | 11 to 19 ms |
| a text in 8,000 pieces, opened and put together: unmerged, and as it is kept | 8 ms; 2 ms |
| one word more in a text that is being written, of 200 words or 100,000 | 0.015 to 0.03 ms |
./x build # build/zspd
zspd serve /var/zsp # a relay; prints its key
zspd create ~/.zsp # a space; prints its name
zspd relay ~/.zsp <relay key> ws://host:8787
zspd run ~/.zsp # this device's node
zspd key ~/.zsp # on another device,
zspd admit ~/.zsp <that> 1 # on this one, at once, running or not,
zspd join ~/.zsp <space> <relay key> ws://host:8787 && zspd run ~/.zsp # and there
zspd admin ~/.zsp <that> # let it admit others; `off` to take that backlocal zsp = require("zsp")
local Message = { author = zsp.string(16), body = zsp.text(17), stars = zsp.number(18) }
local client = zsp.connect(host) -- a socket, a clock, a scheduler: see Host
local window = client:window({ scope = 1, list = "chat/42", table = Message }, function(state)
render(state.rows, state.before, state.after) -- at most once a turn; unchanged rows are the same tables
end)
window.visible(first, last) -- as the user scrolls, either way, for ever
client:write(1, function(change)
id = change.insert(Message, "chat/42", os.time(), { author = "model" })
end)
local w = zsp.where
local found = client:ask({
scope = 1, table = Message,
where = w.all(w.matches("body", "quick fox", { prefix = true }), w.any(w.is("author", "ann"), w.over("stars", 3))),
order = { "best" }, limit = 50, totals = { authors = { "facet", "author", 10 } },
}, function(answer)
render(answer.rows, answer.total, answer.totals.authors) -- again whenever it is no longer so
end)
found.move(50) -- another stretch of it
local now = client:fetch({ scope = 1, table = Message, where = w.is("author", "ann") }) -- once, and no more
local text = client:text(id, Message.body)
text.append(token) -- seen everywhere at once; durable a piece at a time
client:serve(1, "run", function(call, data) ... end)
local answer = client:request(device, 1, "run", data)On Cloudflare, authentication is a function:
import { relay, Space } from "./index.js";
export { Space };
export default relay({ authenticate: async ({ space, device, credential, env }) => isCustomer(credential, env) });cd relay && wrangler secret put RELAY_SECRET && wrangler deploy; GET / then answers with the key devices pin: fetch it once, with something that checks certificates, as a browser or curl does.
Embedding in Odin is the same question asked of host.Config.authenticate, or of zsp.Host.authenticate if you bring your own I/O.
docs/scoping.md: what should be a space, a scope, a list or a column; how much fits; keeping a space the same as your own directory of people; guests, announcements, leavers; who may use a relay, and what each application on a machine may do. Its examples are in examples/ and are run with the tests.
./x check type-checks every platform, ./x test runs everything, ./x sim 10000 runs ten thousand seeded networks that fragment, partition, crash, lose
unsynced disk, disagree on the time, have rights given and taken away, and conflict, ./x nat punches through real NATs in network namespaces, ./x slow watches a Durable Object sleep. Needs odin (dev-2026-11), wasm-ld, lune, luau-analyze, node, python3.