All posts

6 min readPost 7

The ledger: JSON, SQLite, and a rewrite we did not merge

In a standalone prototype, moving Obituary's ledger from JSON to SQLite ran ten times faster on scattered writes—yet our first real implementation ran slower than JSON. Here is why we shelved the SQLite branch and how three structural changes to JSON—batching, compaction, and hot/cold snapshots—delivered the real speedup.

On this page
  1. Ten times faster in the prototype
  2. Slower in production: keeping the old interface erased every gain
  3. Shelving SQLite until a resident daemon exists
  4. What actually made JSON fast

Obituary tracks every file lifespan, buried grave, and pending proposal in a local JSON ledger. With one terminal active, rewriting the entire JSON file on each update is negligible. Launch a dozen AI coding agents at once, however, and every file edit spawns a short-lived hook process committing one small change—making full-file JSON rewrites look like an obvious bottleneck.

On 9 November we ran a full engineering evaluation of replacing JSON with SQLite. The outcome was a textbook benchmark lesson: SQLite ran ten times faster in a standalone prototype, yet our first real implementation ran slower than JSON on single-process scattered writes.

We never merged that branch. The ledger stayed on JSON and got an order of magnitude faster anyway.

Ten times faster in the prototype ​

We tested the pure-Go driver modernc.org/sqlite v1.62.0 (zero CGO) in WAL mode with synchronous=FULL so every commit hit disk, timing 1,000 mutations:

WorkloadJSON ledgerSQLite prototype
1,000 commits (1 item each)16.0 s1.57 s
Commits every 25 items0.49 s0.07 s
4 writers × 250 commits + 1 reader~24 s~1.9 s
1,000 items in 1 commit15 ms31 ms

The first three rows mirrored concurrent multi-agent hooks, where SQLite won by a factor of ten. Only the bulk single-commit row favored JSON, which wrote one file without per-row transaction overhead.

Based on those numbers, we greenlit a SQLite prototype branch while recording four explicit costs:

  • +5.3 MiB on obit.exe.
  • One-way migration for existing JSON ledgers.
  • Safe refusal by older binaries so they could never corrupt the new format.
  • Read-only diagnostics (obit doctor) must never silently create a database file.

Slower in production: keeping the old interface erased every gain ​

By evening the v9 SQLite implementation passed every test—row storage, JSON migration, old-binary rejection, and clean read-only checks. Then we benchmarked it against JSON:

WorkloadJSON ledgerSQLite v1 implementation
Single-process 1,000 scattered commits~38 s~60 s
1,000 items in 1 commit24 ms66 ms
16 concurrent writer processes124 s88 s

Single-process scattered commits ran nearly 60% slower than JSON, leaving SQLite ahead only under 16-process contention.

The cause was straightforward. The prototype read and updated only the touched rows. Our real implementation kept the existing core.Store interface intact, so every commit still loaded, decoded, and re-encoded the entire ledger—doing everything JSON already did while paying SQLite connection startup, WAL commits, and close-time checkpoints on top.

A second profiling pass the next day trimmed overhead, yet single-process commits still took 35 ms against JSON's 26 ms, with 16-process concurrency ahead by only ~10%.

Bar chart showing SQLite ledger runtimes relative to JSON (100%) across the prototype, first implementation, and post-profiling pass: 10x faster in the prototype, slower on single-process writes in real implementation
SQLite runtime relative to JSON (100%) across the same workloads. The prototype's 10x lead vanished in real implementation, leaving only a thin edge under heavy concurrency.

Shelving SQLite until a resident daemon exists ​

That same evening we committed to building a small resident tray daemon (obitd). A resident process changes SQLite's economics completely: holding one open connection and keeping hot state in memory removes per-hook open/close checkpoints so row writes actually pay off.

So we left the SQLite branch unmerged behind a concrete gate: scattered single-process writes must be at least as fast as JSON, and concurrent writes must beat JSON decisively.

What actually made JSON fast ​

Instead of forcing a database migration, we attacked the real waste inside our JSON write path over the next three days.

1. Batched reaping without dropping a single safety check ​

Reaping 1,000 expired entries previously rewrote the whole ledger with two fsyncs per item, causing quadratic I/O. Grouping up to 32 burials into one ledger commit kept per-item identity checks and crash manifests intact.

Code review caught a subtle race on round one: if a user ran git add on a file after a batch Git precheck and before the move, the file would still be buried. Round two moved the Git index verification right before each individual move (closing the check-to-move window to at most ~1.7 ms).

Reaping 1,000 auto-registered entries fell from 108.7 s to 14.4 s.

2. Compact encoding and batched deletes ​

On 11 November, ledger v15 and v16 tackled size and lock starvation:

  • Deduplicating shared parent chains, operation metadata, origins, and volume records shrank the ledger by roughly a quarter.
  • obit rm, Remove-Item, and git clean previously grabbed the ledger lock once per path, starving hooks in other sessions during big deletes. Moving them onto the same batching engine (up to 32 items or 100 ms per slice, releasing the lock between batches) cleared the jam:
MetricBeforeAfter
Deleting 1,000 paths~188 s16 s
Longest write-lock hold240 ms~100 ms

3. Hot/cold ledger split: immutable graveyard snapshots ​

Early on 12 November, ledger v17 eliminated the biggest source of write amplification. An agent hook registering one new scratch file never needs to rewrite a thousand historical graveyard entries.

We split storage into two files:

  • Hot ledger (store.json): active lifespans, pending proposals, counters, and a hashed reference to the graveyard snapshot.
  • Immutable graveyard snapshot: written first whenever graves change, then published atomically by replacing store.json as the sole commit point. Missing or hash-mismatched snapshots fail closed instead of looking like an empty graveyard.

On a ledger with 1,300 graves, hook rewrites shrank to one-seventh their prior size and single updates fell from 80 ms to ~14 ms. In BenchmarkStoreLedger (real disk sync on every commit), a hot-only commit (126 KB) takes 9.7 ms compared to 76.4 ms when also writing the 764 KB graveyard snapshot.

Bar chart of BenchmarkStoreLedger on a synthetic 1,300-grave ledger: hot-only commit takes 9.7 ms versus 76.4 ms when updating both hot ledger and graveyard snapshot
Real BenchmarkStoreLedger timings with full disk sync: 126 KB hot ledger versus 764 KB graveyard snapshot. Everyday hook writes only touch the hot file.

All posts