Under the hood / One core, four languages
One core, four languages
TinyStore's engines are written once, in Rust. A Rust program calls them directly. Bun, Node, Python and Go programs reach the same core through one wire protocol, whether the core runs inside their process, beside it as a sidecar, or on another machine. This chapter explains the protocol, the four C functions that carry it inside a process, and how a program finds and trusts its sidecar.
The shape
The engines know nothing of the protocol, and the protocol knows nothing of the transport. An SDK writes the same bytes into whichever it was given.
Frames
Everything on the wire is a frame: a header of twelve bytes, then a body in
MessagePack.
A call is a REQUEST and its RESPONSE on a stream of its own, and many
streams share one connection, so a slow download never blocks a quick call.
A receiver grants credit, and a sender never sends more than it was granted,
so a client that reads slowly holds back the server's sending, not its
memory. Every message is written from one schema per engine in
protocol/*.wire, which also writes the vectors every SDK is tested against,
so the four SDKs cannot drift apart. The wire protocol page
has every frame and rule.
The pipe: four functions
Inside a process, the frames go through four C functions:
size_t tinystore_open(dir, options, wake, ctx, &conn, error, error_room);
size_t tinystore_send(conn, frames, len, into, room);
size_t tinystore_recv(conn, wait_ms, into, room);
void tinystore_close(conn);send takes frames and writes back the ones ready at once. A write's answer
is ready only after its commit, so it arrives later through recv. When
frames become ready, the core calls wake once, and the host reads them:
| Language | Loads the core through | Learns of an answer by |
|---|---|---|
| Bun | bun:ffi and the dynamic library | a threadsafe JSCallback |
| Node | the napi-rs addon, tinystore.node | a threadsafe function |
| Python | the PyO3 module, tinystore._core | a thread waiting in recv with the GIL released |
| Go | the core's library: cgo, or a DLL on Windows | a goroutine waiting in recv |
No memory of the core's crosses the boundary: the host gives the buffers, the
core reads one and writes the other before it returns, and nothing is left to
free. A panic in the core never crosses either: it ends the connection with
GOAWAY and the code internal.
Every new call of every engine is a new message in the protocol. The four functions do not change when a feature is added.
The sidecar and its proof
A store's directory has one owner, the process that holds its LOCK. When a
Bun, Python or Go program opens a directory without embedded, its SDK
looks for the owner:
- It reads
server/SERVEin the directory: the server's endpoints, a Unix socket or a Windows named pipe, and a secret. - It connects and sends
HELLOwith 16 random bytes as a challenge. - The server's
WELCOMEcarries a proof: the HMAC-SHA256 of the challenge, keyed with the secret. The client checks it before it sends any request. - If any step fails, the SDK starts
tinystore serve --localfor the directory. That process takesLOCKand publishes a newSERVE, or findsLOCKheld and exits, and the client tries again, for up to 15 seconds or until the program cancels itsopen.
Only the user the server runs as can open the server/ folder and SERVE,
or connect to the server's socket or pipe, even when the store's directory
lets other users read: mode 0700 on Linux and macOS, and on Windows a DACL
that grants that user alone and does not inherit the store directory's. If
another user created the server/ folder first, the server refuses to start
and names the folder, since that user could still open it. A name such as a
Windows pipe can be taken by anyone once its server is gone, so the proof,
not the name, says that the server really serves this directory.
The sidecar exits 30 seconds after its last client leaves. Four web processes and a cron script that open the same directory all join one sidecar.
A remote server
tinystore serve ./data --listen tls://0.0.0.0:7443 serves the same protocol
over TLS, to clients that connect with a token of admin, data or read
(serve). A local connection, through the
pipe or a sidecar, needs no token: its permission is the file system's.
Why this design?
Why the protocol is the boundary, even in one process. The first versions had a Go engine and re-implemented parts of it in each SDK. A C API with a function per call would have needed a new function in four bindings for every feature. With the protocol as the boundary, a feature is a message, its dispatch and its typed call in each SDK, and the core's behaviour is the same for every language because it is the same code. It also makes the embedded store and a remote server the same code to test.
Why MessagePack with integer keys. It is compact, every language has a fast encoder, and integer keys keep a message small without a schema at run time. The profile is strict, one way to write each value, so that the vectors can check bytes, not only meaning.
Why a sidecar by default and not embedded. An embedded store belongs to
one process. Web servers run several, plus scripts and cron jobs, and only
one process may own a directory. The sidecar gives all of them one owner. A
program that is the only one ever to open its store chooses embedded and
saves a hop per call.
See also
- The journey of a write: one call through these layers.
- One directory, many engines: the four ways to open a store from the program's side.
- Wire protocol: every frame, rule and method.