SQL and data types · Engineering

Document history and audit trails without extra tables

How per-collection revisions work, what they cost, and why history is not the same as an audit log.

“Who changed this document, and what did it look like before?” is a common requirement that often turns into a second table, a trigger, or an application-level change log. NonoDB can keep revisions per collection in a separate LMDB sub-database, atomically with the current document.

It is useful, but it is important to be precise about what it is: document history, not a complete security audit system.

Enabling history

History is configured per collection with a maximum number of revisions retained per document. 0 disables new history writes for that collection. There is no daemon-wide switch.

Terminal window
nonodb-cli history users 50
nonodb-cli history users 0
nonodb-cli history users

The API uses set_collection_history / get_collection_history. Changing the retention setting does not create history for past writes; enabling it after documents already exist starts recording future writes. There is no retroactive backfill.

What gets recorded

For a history-enabled collection, inserts, updates, and deletes append an immutable revision or a delete tombstone. The history append shares the same LMDB transaction as the current-document change, so a crash cannot leave the current document and its revision inconsistent.

Every write receives a global monotonic rev. Revisions for one document are ordered, but not necessarily consecutive, because other documents consume revisions too.

History entries are not indexed by search or secondary indexes. find, count, update, and delete always operate on current documents. This keeps query results current and avoids index maintenance for old versions.

Reading history

The history operation returns revisions newest first:

{
"op": "history",
"coll": "users",
"id": "0b7f...",
"limit": 50,
"include_docs": true
}

limit defaults to 50 and is capped at 1000. With include_docs: false, you get metadata without full document bodies.

get_version returns one exact revision:

{
"op": "get_version",
"coll": "users",
"id": "0b7f...",
"version": 1001
}

rev is accepted as an alias for version. The result is null if the revision no longer exists. A deleted document has a tombstone with "deleted": true and no body.

The cost

History is write amplification: roughly one document copy per write for a history-enabled collection. It also consumes disk and backup space.

The maintenance worker submits bounded pruning jobs to the single writer every 30 seconds. Pruning never scans history on the hot write path, but it still competes for the writer. Size NONODB_MAP_SIZE and backup retention accordingly, and consider a smaller retention for write-heavy collections.

Deleting a document creates a history tombstone when history is enabled. TTL expiry uses the normal delete path too. A collection can set purge_history on TTL settings to delete stored revisions instead of keeping tombstones.

History is not an audit log

Document history answers “what were this document’s previous versions?” It does not answer every security question:

  • It does not record authentication failures or permission changes.
  • It does not capture reads.
  • It does not provide tamper-evident signing or external retention.
  • It is stored in the same database as the documents, so a database restore can roll it back.
  • It is not a substitute for an append-only audit system with independent storage.

NonoDB also has optional JSONL audit records for requests and auth events. That is a different mechanism with a different retention and threat model. Use both when you need both document history and security audit.

A practical pattern

Use history when:

  • users need undo or “view previous version”;
  • an approval flow needs to compare revisions;
  • a support tool needs to see what changed recently;
  • retention can be bounded and the write amplification is acceptable.

Do not use it when:

  • you need a legally defensible, tamper-evident audit trail;
  • reads must be audited;
  • retention must survive database restores independently;
  • the collection is extremely write-heavy and a copy per write is too expensive.

The history API and operations guide cover retention, pruning, and TTL interaction.

All engineering notes →Read the docs