Positioning and architecture · Engineering
LMDB and the single-writer model
Why NonoDB uses LMDB, how copy-on-write MVCC works, and what a single writer per database means for throughput and crash safety.
NonoDB does not implement its own storage engine. It uses LMDB through the Rust heed crate, and it makes LMDB’s single-writer property explicit with one writer thread per database.
That choice explains a lot of the database’s behaviour: fast reads without a buffer pool, predictable crash recovery, group commit, and a hard ceiling on write concurrency.
LMDB in one paragraph
LMDB is a memory-mapped B+ tree with copy-on-write pages. A writer never overwrites pages that a reader can still see. Instead, it writes new pages and publishes a new root when the transaction commits. Readers keep a stable snapshot for the lifetime of their read transaction.
The result is MVCC without a separate undo log: old versions are simply pages that the writer has not reused yet. The operating system maps the file, so reads touch pages on demand and the kernel manages the page cache.
NonoDB stores JSON documents, indexes, history, TTL metadata, and graph adjacency in named LMDB sub-databases. A write transaction updates the document and all of its derived structures together. There is no asynchronous index queue that can drift from the data.
One writer, many readers
LMDB allows one writer at a time. NonoDB does not try to hide that behind multiple writer connections. The daemon has a dedicated writer thread fed by an mpsc channel. Read requests run on blocking worker threads and open their own read transactions.
This gives a simple concurrency story:
- reads are concurrent and see a consistent snapshot;
- writes are serialized per database;
- different databases have independent writers;
- a write batch commits atomically;
- a process crash leaves the last committed state intact.
The cost is equally simple: write throughput does not scale with cores. If one collection is the bottleneck, adding vCPUs helps reads and background work, not the writer itself.
Group commit
Sending every request through a channel would be wasteful if each one fsynced separately. The writer groups pending writes and commits them together. That amortises the fsync cost across requests and keeps durability without making every caller wait for a separate disk flush.
The exact batching depends on load and configuration, but the important property is that the application sees a normal request/response API. It does not manage transactions or flush points. write_batch gives an explicit all-or-nothing group when the application needs one.
What about a WAL?
LMDB itself is crash-safe without a write-ahead log. A committed transaction is durable; an interrupted write is discarded. NonoDB does not need a WAL to recover the primary database.
PITR is a separate, opt-in feature. It keeps a segmented logical WAL plus periodic snapshots so an operator can restore to a timestamp. It is not the normal write path, and it is disabled by default. This is an important distinction: “no WAL” does not mean “no point-in-time recovery”; it means the core storage engine does not depend on one.
The high-water mark
Copy-on-write has a disk consequence. Deleting documents frees pages for reuse, but the LMDB file does not shrink. The file keeps its high-water mark.
That is why a time-series collection can report a compacted size of 1.02 MB while the live data.mdb is larger. A compacting backup or nonodb-cli compact produces a smaller copy; a normal delete does not. The compaction guide explains the workflow.
What the single writer buys
The model is not an apology for missing concurrency. It is a trade:
- Predictable semantics. There is no write-write conflict resolution to debug.
- Atomic derived data. Indexes, history, TTL, and events commit with the document.
- Crash safety. Recovery is LMDB’s problem, not an application-level journal.
- Operational simplicity. One writer thread, one lock file, one data directory.
If you need multi-master writes, use a system designed for that. If you need a small, durable, single-node database with concurrent reads, a single writer is a reasonable price.
The architecture document goes deeper into the read path, query planning, and the exact LMDB layout.