All posts

7 min readPost 29

SQLite, or JSON with one resident writer: what the two cost when measured on the same scenarios

Sixteen concurrent AI agents pushed Obituary's hook latency past 1 second. Once we had a resident daemon, we built both a row-level SQLite ledger and a format-preserving JSON single-writer path, measuring both against identical latency, size, compatibility, and crash-kill gates—and chose JSON plus resident.

On this page
  1. Path A: two rounds of SQLite hit 297 ms hooks—and exposed a nasty init gap
  2. Stepping back: the bottleneck was queueing and Git, not JSON
  3. Path B: v18 JSON untouched, writes batched inside the resident
  4. Side by side: real numbers at 1k and 10k entries
  5. Engineering costs: binary size, dependencies, rollback, and crash kills
  6. Where the line sits

When 16 AI coding agents edit files simultaneously, every short-lived obit hook process used to line up behind the JSON ledger lock, rewrite the whole file, and spawn its own Git subprocesses—pushing single-hook p95 past 1,049 ms.

Once our resident tray daemon (obitd) existed, two clean architectural paths opened up:

  • Path A (row-level SQLite): migrate the ledger onto pure-Go SQLite with the resident holding a persistent database connection.
  • Path B (JSON + resident single writer): keep the v18 JSON format unchanged and funnel concurrent hook writes through the resident over local IPC in batches.

Between 22 and 23 November we built both paths and ran them against the exact same latency gates: 600 ms single hook, 800 ms full wave, and 250 ms concurrent obit list.

Both cleared the 600 ms hook gate, and we chose Path B: at real-world ledger sizes (our own daily machine held 443 active entries and 1,080 graves in a 282 KB store.json), SQLite's extra speed did not pay for its binary bloat, one-way migration, and new failure modes.

Path A: two rounds of SQLite hit 297 ms hooks—and exposed a nasty init gap ​

Round 1 wired in modernc.org/sqlite v1.62.0 behind the old full-ledger interface (113 ms to update one row at 10k entries vs. our 15 ms gate). Round 2 (branch 2a618fc) moved to genuine row-level I/O behind the resident daemon:

  • Passed: 1-row update at 10k entries hit 5.33 ms p95 on Windows (7.13 ms on WSL); 16-way real hook p95 hit 296.71 ms (beating our 300 ms stretch goal) with 318.87 ms wave p95; forced kills at two critical points lost and duplicated zero entries.
  • Missed: concurrent obit list still sat at 281.33 ms (gate 250 ms); direct short-process writes lagged JSON by ~7 ms p95; several write paths and full upgrade matrices remained unfinished.
  • Data-safety blocker: if initial database creation committed the DB file yet failed publishing its version fence, normal rollback restored rows without restoring "the database did not exist"—leaving an orphan database that refused to open (SQLite ledger exists without its version fence). Round 2 also had to fix read-only diagnostics mutating WAL sidecars and tampered graveyard summaries failing to block purge.

Stepping back: the bottleneck was queueing and Git, not JSON ​

Three facts made us pause SQLite round 3 immediately (stashing the half-finished orphan-DB fix and preserving 2a618fc):

  1. Most of the 1-second stall came from 16 processes contending on locks and spawning Git—which a resident single writer solves regardless of file format.
  2. At 1,000 + 1,000 entries, direct short-process write p95 was 85.78 ms for SQLite versus 83.87 ms for JSON; SQLite only pulls away past 10,000 entries.
  3. SQLite's costs were immediate and permanent: +4 MiB binaries, a one-way migration older binaries reject, and a larger failure state machine.

We spent a day implementing JSON + resident single writer (24edcff) to measure both side by side.

Path B: v18 JSON untouched, writes batched inside the resident ​

Path B kept every byte of the v18 JSON schema intact:

  • Durable receipt on handoff: PostToolUse hooks send observations over a current-user Windows named pipe (Unix domain socket on POSIX) to obitd, which coalesces concurrent arrivals into one append to observations.jsonl before replying. Without a resident, hooks fall back to direct writes; a 250 ms timeout drops a deduped fallback file using the same request ID.
  • Heavy work off-lock: the resident replays batches of up to 32 entries on a private ledger copy outside store.lock (running Git, rules, and identity checks), takes the lock briefly to confirm store.json bytes haven't changed, commits once, and flushes audit. Three consecutive conflicts fall back to holding the lock.
  • Lock-free reads: obit list, obit stats, and grave notices read atomically renamed store.json with zero writer queueing.

Side by side: real numbers at 1k and 10k entries ​

Measured on a 1,000-active + 1,000-grave ledger with real obit hook claude, real obit list, and real Git (separate windows on a shared dev host, evaluated against each path's gates):

Bar chart comparing p95 latencies across pre-change JSON, SQLite round 2, and JSON + resident at 4, 8, and 16 hooks: 16-hook single latency drops from 1,049 ms to 297 ms (SQLite) and 393 ms (JSON + resident), while concurrent list sits at 469, 281, and 357 ms
p95 comparison on 1k+1k entries: baseline JSON (gray), SQLite round 2 (orange), and JSON + resident single writer (blue).
Metric (p95, ms)GateBaseline JSONSQLite R2JSON + resident
Single hook, 4 concurrentNo regression438.45170.28283.57
Single hook, 8 concurrentNo regression559.93303.60286.67
Single hook, 16 concurrent600 (stretch 300)1,049.36296.71392.72
Whole wave, 16 concurrent8001,271.59318.87408.75
Concurrent obit list, 16 hooks250468.73281.33357.09

Both passed hooks and waves, and both missed the 250 ms list gate. At 10,000 + 10,000 entries, JSON + resident still kept 16-way single-hook p95 flat at 396.4 ms (vs. 1,269.4 ms on main) because full JSON rewrites (~0.2 s per batch) moved onto the daemon; the trade-off is higher background drain latency before all items appear in the ledger (3.44 s p95 vs. 1.20 s baseline at 1k, and 6.92 s on SQLite R2).

Engineering costs: binary size, dependencies, rollback, and crash kills ​

Terminal git diffstat comparing both experimental branches: SQLite R2 adds modernc.org/sqlite v1.62.0 plus 6 indirect dependencies, while JSON + resident leaves go.mod and go.sum unchanged
Branch diffstats side by side: SQLite pulls in a new dependency tree; JSON + resident adds zero dependencies.
DimensionSQLite R2 (2a618fc)JSON + resident (24edcff)
Binary size delta+4.2 MB on Windows amd64 (+3.82 to +4.14 MiB across 6 platforms)Negligible
New dependenciesmodernc.org/sqlite + 6 indirectZero (go.mod untouched)
Rollback / old-binary compatOne-way: older binaries refuse new formatFull: 1.7.0 lists 1,000 items and replays queued observations cleanly
Non-test Go diff+1,798 / -14 lines (partial write coverage)+862 / -76 lines (Post path)
Kill fault injection2 targeted test-process kills: 0 lost, 0 dupes1k: 50 random TerminateProcess kills / 560 writes → 0 lost, 0 dupes; 10k: timed out at 180 s drain deadline (298/320, marked fail; completed to 10,320 exact / 0 lost / 0 dupes after)

Where the line sits ​

ConditionBetter choice
Up to a few thousand entries; contention is locks and GitJSON + resident single writer
10,000+ entries where hot-file rewrites dominate wall timeSQLite (5 ms vs. 82 ms row update)
Upgrades must remain safely roll-backable to prior binariesJSON + resident single writer
Willing to pay +4 MiB, one-way migration, and new failure statesSQLite

We chose JSON + resident and kept the SQLite branch cleanly parked at 2a618fc if real-world 10k-entry ledgers ever demand row-level writes. And as segment profiling immediately showed next, fixing the stubborn >250 ms concurrent obit list latency had nothing to do with switching databases.

All posts