ChronDB's vacuum deletes nothing
A project of mine that runs ChronDB reached 100 GB on disk. ChronDB keeps every version of every document, and I kept all of it. That was a choice. What I never chose was the way all of it was being stored.
The obvious fix for a database that big is to delete old versions. That is not what I wrote. ChronDB is a chronological database, the history is the product, so the first tool I built for this is a vacuum that removes no object at all. Reachable or not. It landed on December 1st in one commit, followed by a fix for a bug I had put in it, a fix in the Rust binding, and the commit that exposes it to Rust. Four commits in a little over an hour.
Before the vacuum makes sense, the storage has to.
How ChronDB keeps history in git
ChronDB maps a database straight onto a git repository. The architecture doc has the mapping:
- repository is the database
- branch is the schema
- directory is the table
- file is the document
- commit is the transaction
A document with the key user:1234 is a JSON file inside a user/ directory. Saving it writes the file and makes a commit, through JGit. Deleting it is a commit too, so the old content is still in the history. The README says it plainly, "nothing is ever truly deleted".
Reading history is git history. Asking for the versions of user:1234 runs a git log filtered to that one file path and reads the document at each commit. A point-in-time read takes that list and picks the most recent commit at or before the timestamp. Every commit also gets a JSON note under refs/notes/chrondb, with a transaction id, the origin (rest, redis, sql, cli) and request metadata, so git log --show-notes=chrondb shows who wrote what and from where.
That is the whole trick, and it is why I like it. Your database is a repository you can clone, diff and inspect with tools that already exist. It is also why the bill showed up where it did.
Where the disk went
I covered how that core turned into a native library in the post about ChronDB as a polyglot library. What I never wrote down is the other half. ChronDB writes one commit per document and never packs.
In plain git that would be odd. A commit creates loose objects, one zlib file per blob, tree and commit, under objects/. From time to time git gc --auto sees too many of them and packs them into a packfile, where similar objects are stored as deltas of each other. Pro Git shows the classic example, about 15K of loose objects turning into a 7K pack. ChronDB never ran that step. A long-lived store keeps every object it ever wrote, loose, and none of them delta compressed. The funny part is that the version control doc in my own repo says "Git's compression and deduplication help minimize impact" and suggests running git gc. True for git. ChronDB just never asked for it.
The surprise was what kind of object. Two local stores I had on disk had reached 20 GB this way, and 99% of that was tree objects. Not documents.
That one follows from the layout above. Documents live in one directory per table. When a commit changes one file, git writes a new tree for every directory on its path, and a tree lists every entry in that directory. So each new document writes a fresh copy of its table's tree, holding the name of every entry written before it. Loose, each of those trees is a full copy. In a pack, each one is a delta against the previous tree, which differs by a single entry.
What vacuum does
vacuum! runs JGit's garbage collector with one setting changed.
(-> (Git/wrap repo)
(.gc)
(.setExpire (Date. 0))
(.setPreserveOldPacks true)
(.setPrunePreserved true)
(.setAggressive false)
(.call))
A gc prunes unreachable loose objects older than an expire date. In git that date defaults to two weeks ago, and you can move it with gc.pruneExpire or --prune (git gc docs). Setting the expire date to the epoch means no object is older than it, so nothing is ever a prune candidate. The comment above that line in the source says "Do not 'fix' this to a real date."
So the whole gain comes from two things. Delta compression of everything into a pack, and deleting the loose copies that the new pack now holds. From the second run on, the previous generation of packs stays under objects/pack/preserved as the rollback path. setPrunePreserved only discards the generation before that, which is duplicates the current pack already has.
The Rust binding documents one real run. An 8.1 GB store went down to 38 MB, and the repack took 332 seconds.
The tests pin the promise down instead of trusting the comment:
- a blob that no ref reaches is written before the vacuum and still exists after it
- every ref tip is identical before and after,
refs/notes/chrondbincluded - all 40 test documents are still readable through ChronDB
- a second vacuum does not leave more loose objects than the first
- with the pack directory read-only, the vacuum throws, the documents are still there, and a retry works once it is writable again
Why this is not git gc
The difference is mostly in what it refuses to do. A default git gc prunes. Vacuum prunes nothing. It does not rewrite history, it is not a shallow clone, it does not squash commits. Every commit stays reachable, so a query for how a document looked last March answers the same way after the vacuum as before.
What it means for a git-based store is simple. Same objects, different container. The size goes down and the history doesn't move.
That is also why it carries the name it has. SQLite's VACUUM "rebuilds the database file, repacking it into a minimal amount of disk space". People who embed a database expect to find that command. Issue 164 opened in October asking for exactly that, for the Git object store itself.
The gate is borrowed from git
Deciding whether to vacuum has its own cost. Counting loose objects on a neglected store means a stat call on every one of them, which is the cost you were trying to avoid. Git solved this a long time ago in too_many_loose_objects. It opens one directory, objects/17, out of 256, and trusts that hashes spread evenly.
ChronDB does the same arithmetic. The threshold is (gc.auto + 255) / 256 objects in that one bucket, read from the store's own git config. With git's default gc.auto of 6700, that is 27 files. Zero or a negative value disables the gate, like in git.
Off by default, with a ceiling
chrondb.vacuum.mode is seeded as off in the store's git config when the key is absent. That happens on open too, not only on create. A store that existed before this change is only ever opened, never created again, so a setting written on create would never reach it. An explicit auto is never overwritten.
In auto mode, opening the store checks the gate and runs a vacuum when the bucket is over the threshold. In off mode the check costs one config read and touches no files.
A first vacuum on a neglected store is a different event from every later one. That 332 second run is the example. Opening a database should not block for five and a half minutes, so the automatic path has a ceiling, chrondb.vacuum.ceiling, 50,000 estimated loose objects by default. Above it, the open logs a warning and skips the vacuum. The automatic path also never throws. It sits on the open path, and an exception there makes the database unopenable.
git -C /path/to/chrondb/data config chrondb.vacuum.mode auto
git -C /path/to/chrondb/data config gc.auto 6700
git -C /path/to/chrondb/data config chrondb.vacuum.ceiling 50000
Locks that last for minutes
A vacuum holds JGit's gc.pid for the whole repack, and on a big store that is minutes. That exposed an old rule in ChronDB's lock cleanup. A lock file older than 60 seconds was considered stale and removable, even while a process still held it. So a second process could delete a live packed-refs.lock or gc.pid in the middle of a vacuum.
Now the OS lock decides. If the file is held, nobody removes it, whatever its age. The 60 second timeout was only ever a guess about whether the owner was alive, and the OS knows for sure.
gc.pid also had a second problem. The cleanup only looked for *.lock files, so an orphan gc.pid left by a crashed run was invisible. It blocked every later gc for JGit's 12 hour gc.logExpiry window. There is a separate cleanup for it now, and it spares a gc.pid that is still held. Inside one process, a small guard keeps two threads from starting a vacuum on the same store.
The bug I shipped first
The first version wrote gc.pruneexpire = never into the git config on open. The idea was to stop a later gc from loosening packed objects again. JGit cannot represent never, and it still can't in 7.8, the version ChronDB pins. Turning it into a date calls toEpochMilli and throws ArithmeticException: long overflow. That broke every other caller of a plain .gc(), and ChronDB's own backup is one of them, because it runs a gc before writing the archive.
The fix removes the config write. vacuum! passes setExpire explicitly on each call, which is what actually disarms pruning, and leaves everyone else's gc alone. Two tests now hold that line. One checks the key is never written. The other runs a plain gc on a managed store, which is exactly what the backup does.
Getting it to Rust
The Rust binding loads ChronDB as a native library cached in ~/.chrondb/lib, and my mcp CLI is one of its consumers. The third commit is about that path. The binding resolved all 25 native symbols eagerly, and one missing symbol aborted the whole constructor. The error was memoized for the life of the process, and nothing refreshes a cached library once the file exists. A months-old library on disk killed put, get and query, not just the newer feature it lacked.
Now the two GraalVM symbols and the eleven the oldest published library exports load eagerly. The eleven added later are optional, and calling one that is missing returns an error naming the symbol and how to refresh the library. A binding that lags behind the library should lose the newer feature and keep the database.
That fix had to come first, because of the last commit. Until then the vacuum lived only in a Clojure namespace, with no entry point, so no binding could reach it. Now the native library exports chrondb_vacuum and chrondb_vacuum_stats, and the Rust crate has db.vacuum() and db.vacuum_stats(). Both symbols sit in the optional set, so an old cached library keeps serving put, get and query and only these two calls fail, with a message saying how to refresh it.
vacuum_stats() is the cheap one. It samples the same single bucket and answers with needs_vacuum, so it is safe to call on a schedule. vacuum() is the expensive one, and the doc comment says so in bold. Every call on the same store goes through one worker thread, so while a vacuum runs, every other call queues behind it, get included. It belongs in a maintenance job, never on a request path.
The commit is honest about one gap. The symbol names and the C signature were checked against the compiled bytecode, but the round trip from Rust stays unverified until a library ships that exports them.
What it does not do yet
There is no chrondb vacuum command yet and no SQL VACUUM. Those are listed in issue 164 along with repository stats. Today the vacuum runs three ways. The auto mode on open, vacuum! from Clojure, or vacuum() from Rust once a library with the new symbols is published.
It also does not shorten history. If a project really does not need old versions, dropping them is a different operation with a different risk, and it is not this one.
I kept the history forever on purpose. I just never looked at what forever looked like on disk.