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

Frames of the wire protocolBun · Node · Python · Go programSDK: one typed API per languagethe pipein processa sidecarsocket or named pipea serverTCP and TLS, tokenssessiondecodes frames, answers themRust programengines · store · SQLitedirect calls
Tap to enlarge

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:

c
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:

LanguageLoads the core throughLearns of an answer by
Bunbun:ffi and the dynamic librarya threadsafe JSCallback
Nodethe napi-rs addon, tinystore.nodea threadsafe function
Pythonthe PyO3 module, tinystore._corea thread waiting in recv with the GIL released
Gothe core's library: cgo, or a DLL on Windowsa 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:

  1. It reads server/SERVE in the directory: the server's endpoints, a Unix socket or a Windows named pipe, and a secret.
  2. It connects and sends HELLO with 16 random bytes as a challenge.
  3. The server's WELCOME carries a proof: the HMAC-SHA256 of the challenge, keyed with the secret. The client checks it before it sends any request.
  4. If any step fails, the SDK starts tinystore serve --local for the directory. That process takes LOCK and publishes a new SERVE, or finds LOCK held and exits, and the client tries again, for up to 15 seconds or until the program cancels its open.

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

Edit this page on GitHub Updated 3 November 2026

esc
↑↓ navigate ↵ open ⇥ filter 0 results