trunkdb
An embedded document database in Rust. No server, no schema: open a database, get collections of typed documents. Every step written down as a spec with its reasons and measurements.
- status
- version 0.43, in active development; API and file format may still change
- language
- Rust; bindings for Python, C and C#
- license
- MIT
What it does
Open a database, get a collection, start inserting and retrieving documents. Any type serde can handle becomes a document, or an untyped Document when the shape isn't known or specified. It fits where SQLite would, but the data is documents: desktop and command-line apps, local caches, tools, small services, test data.
use serde::{Deserialize, Serialize};
use trunkdb::{Database, DocId, query::Filter};
#[derive(Serialize, Deserialize)]
struct User {
#[serde(rename = "_id")]
id: Option<DocId>,
name: String,
team: String,
age: i64,
}
let db = Database::open("app.trunkdb")?;
let users = db.collection::<User>("users");
users.ensure_index(["team", "age"])?;
let id = users.insert(User {
id: None,
name: "Ada".into(),
team: "core".into(),
age: 36,
})?;
let youngest = users.find(
Filter::new()
.eq("team", "core")
.sort_asc("age")
.limit(5),
)?;
let same: Filter =
r#"team = "core" order by age limit 5"#.parse()?;- Queries in code or as text, with sort, skip and limit, parameters, projection, and aggregates by group.
- Secondary indexes: unique, compound, sparse, on nested paths and on array elements.
- Atomic batches across collections, crash-safe through a write-ahead log, with preconditions checked at commit.
- Live queries that report what comes to match a filter and what leaves it, and live aggregates over a window of time.
- Readers in other processes beside the one that writes, so an admin tool or an AI agent can look into the file while an app runs.
The same database from the command line:
# format, pages and sizes, collections, indexes
trunkdb info app.trunkdb
# the plan, index reads, counts
trunkdb find app.trunkdb users 'age >= 30' --explain
# every path, in how many documents, as which types
trunkdb fields app.trunkdb users
# what the running app commits, as it happens
trunkdb watch app.trunkdb users 'team = "ops"'
# consistency check, then rebuild into as few pages as needed
trunkdb check app.trunkdb
trunkdb compact app.trunkdbThree decisions
Every step is a numbered spec section
Before a change is made, it gets its own section in spec/: what it is for, the options, why this one, and sometimes the measurements before and after. There are more than two hundred of them now. The roadmap and changelog only point to the sections, so the reasoning lives in one place, and a part I come back to months later explains itself.
One writer, and a planner you can follow
One process writes; readers run in parallel beside it, in the same process or others, each with a snapshot that stays consistent. Several writers at once are out of scope on purpose. The query planner follows fixed rules instead of a dynamic cost model, and explain says which rule it took, what it read, and what the plans it passed over would have read. Predictable beats clever here.
Measured against the others
A benchmark runs one document workload against trunkdb, SQLite, redb and sled, with the same indexes, durable commits everywhere, and the same answers checked across all four. 100,000 documents on an M1 Pro, trunkdb 0.40:
| Operation | trunkdb | SQLite | redb | sled |
|---|---|---|---|---|
| get by id | 2–4 µs | 18 µs | 3.5 µs | 3.2 µs |
| 1000 documents by index | 0.89 ms | 1.8 ms | 1.1 ms | 2.0 ms |
| insert, 1000 per commit | 43k/s | 40k/s | 44k/s | 29k/s |
| compact | 0.64 s | 0.18 s | 0.45 s | — |
| file size after compact | 18 MB | 22 MB | 30 MB | — |
Reads through an index are level with the others or ahead, and the file is the smallest. Compaction is still behind. The full table and why each number is what it is are in DESIGN.md.
Status
Version 0.43, used from git; not on crates.io yet. It stays below 1.0, and the API and file format are kept stable by practice: each release says what breaks and how to move up, and export and import move a file between formats.
Around it: the command line, bindings for Python, C and C#, an Agent Skill that helps AI agents to inspect a file safely, and TrunkDB Admin, a desktop app to look into a trunkdb database without any code.
In use today
It started as a way to get the easy embedded document store of LiteDB outside .NET, and to learn how a database is built, from the page layer up.
Since then it has become the standard data storage for my projects. The last big feature sets were built around live queries and a more compact way of storing field names and index keys to shrink database sizes.
For upcoming features, have a look at the roadmap.
Feel free to contact me for feature requests, discussions or collaborations.