Operations · Engineering
Compaction, map size, and the high-water mark
Why data.mdb does not shrink after deletes, how to grow the map online, and when a compacting copy is the right tool.
Two numbers confuse almost everyone running an embedded database: the file size and the space the data actually needs.
In LMDB, deleting documents frees pages for reuse but does not truncate the file. The file keeps a high-water mark. NonoDB inherits that behavior.
The high-water mark
LMDB is copy-on-write. A write allocates new pages and publishes a new root. Pages freed by a delete or update can be reused by later writes, but the file is not shrunk.
So this sequence is normal:
- Load 10 million rows.
- Delete 9 million.
- The file is still close to its peak size.
- New writes reuse free pages.
- Disk usage does not fall until compaction.
The same is true for ordinary collections and time-series buckets. Time-series just makes it more visible because retention deletes a large fraction of the data.
Map size is not disk or RAM
NONODB_MAP_SIZE is the maximum size of the memory-mapped database. It is an address-space reservation, not a disk allocation and not a RAM reservation.
- The file grows as data is written.
- RSS grows with the pages actually touched, not with the map size.
- Virtual size includes the whole reservation; that is not a leak.
The default is 1GiB. If the database reaches the map limit, writes fail. Grow it online:
nonodb-cli mapnonodb-cli resize 4GiBresize_map waits for active LMDB transactions and briefly blocks new ones. It does not restart the daemon or rewrite the data. It can only grow the map, and the size must be page-aligned. If the request timeout is shorter than a long transaction, the client may time out even though the resize completes.
Shrinking the file
A compacting copy walks the live tree and writes a new file with only reachable pages. That is how the file shrinks.
Stop the daemon and run:
nonodb-cli compact /var/lib/nonodb/defaultThe command copies data.mdb through LMDB’s compacting copy, fsyncs it, swaps it in, and keeps data.mdb.before-compact until the swap succeeds. It refuses to run against a live environment.
A backup is often safer on a primary:
nonodb-cli --db app1 backup /var/lib/nonodb /var/backups/nonodb/app1The backup path uses a compacting copy without touching the source. Restoring it reduces the live file size. The daemon can also run online compaction when the database is idle, but it is still a copy; plan for extra disk and I/O.
Do not run an external backup or inspect process at the same time as an online resize_map. Finish the resize first.
What compaction does not do
- It does not change document semantics.
- It does not rebuild indexes unless the copy does so as part of the environment.
- It does not fix a corrupt database; restore from a verified backup instead.
- It does not make a live database smaller in place; the swap is the mechanism.
- It does not remove the need for backups.
Sizing the disk
Leave room for:
- the live database;
- one compacted copy;
- Docker images and the OS;
- snapshots and WAL if PITR is enabled;
- search and vector sidecars.
A backup needs at least as much free space as the compacted database, plus the existing file. A compaction on a full disk fails at the worst time.
When to compact
- after a large delete or retention purge;
- after dropping large indexes;
- before moving a database to a smaller volume;
- after prolonged churn where free pages dominate;
- when a restore/backup copy is much smaller than the live file.
Do not compact on every delete. Free pages are reusable, and a compacting copy costs I/O proportional to the live data.
Monitoring
Track:
data.mdbsize;- map size and usage;
- free-page ratio if exposed by your tooling;
- disk free space;
- backup size versus live size;
- write failures that mention the map limit.
A file that stops growing while writes continue is often reusing free pages, not losing data.
The practical rule
If the file is large because of history you intentionally deleted, compact. If it is large because it holds live data, compacting will not help; add disk or remove data.
Map size is a limit, not a preallocation. Disk size is a high-water mark, not a bug. Once those two are clear, most “why is my database so big?” incidents answer themselves.
The operations guide and backup guide document resize, compact, and restore.