Problem and how it was found
sandkiln-store keeps a durable record of every sandbox sandkiln has created: when, with what tags, and how it ended (destroyed, snapshotted, or orphaned by a daemon restart). It is a sqlite database, ~/sandkiln-tools/history.db by default, set with SANDKILN_HISTORY_DB_PATH. The daemon writes to it with HistoryStore::record_created once a boot has succeeded.
The per-phase profile of a cold create was added to find where the time went (see the performance model post for the tooling). It surfaced one line that nobody had been tracking. The cold create complete debug event carries a history_write_us field, which times the call in create_sandbox_cold. The run was 20 sequential creates on an idle box:
history_write_us was 9.24 ms, which was 5.5% of the 167.65 ms total. In that breakdown it was the third largest cost, after the rootfs clone (124.09 ms) and Vm::boot (34.00 ms). A 5.5% cost is easy to dismiss, but the context made it hard to leave alone. The call site in create_sandbox_cold is explicitly best-effort:
Root cause
HistoryStore::open opened the database with sqlite’s defaults: journal_mode=DELETE and synchronous=FULL. In that configuration a commit works against a rollback journal. The old page contents are copied to a journal file, the journal is flushed to disk, the main database file is then modified and flushed to disk, and finally the journal is deleted. The doc comment in the store counts two flushes per write: one for the rollback journal and one for the main database file. Each flush waits for the storage device to confirm the data is durable, which is why a single-row INSERT on an ordinary disk costs milliseconds and not microseconds.
It is worth being precise about what the profile did and did not show. It showed that the write cost 9.24 ms on a live daemon. It did not break that cost down by flush, so “the double flush is the cause” is an explanation for the cost, supported by the isolated A/B below and not itself timed inside the live run.
The change
The fix is a function incore/crates/store/src/lib.rs called from HistoryStore::open right after the connection opens (core/crates/store/src/lib.rs:116):
core/crates/store/src/lib.rs:148. Two settings change together.
journal_mode=WAL replaces the rollback journal with a write-ahead log. A commit appends the changed pages to a separate log file and flushes that, rather than rewriting the main database file in place and flushing it. The main file is only updated later, in batches, when the log is checkpointed.
synchronous=NORMAL relaxes when sqlite waits for the device. In WAL mode, NORMAL means the log is flushed at checkpoint time and not after every transaction. This is safe specifically in WAL mode. In the default rollback-journal mode, NORMAL carries a different and larger corruption risk, which is why the two settings are changed as a pair and not one at a time.
HistoryStore::open_in_memory deliberately does not call it. WAL is not meaningful for :memory: (sqlite silently keeps an in-memory journal), and durability is not a concern for a store nothing persists. That constructor is test-only.
The test
A configuration that can silently fail to apply is a configuration that has not been shipped. sqlite can decline a journal mode switch in some situations, so the test does not merely check that opening the store succeeds. It reads the pragmas back:core/crates/store/src/lib.rs:395. The comment in the source explains the 1: sqlite reports synchronous as an integer level, where 0 is OFF, 1 is NORMAL, 2 is FULL and 3 is EXTRA. A related test in the same file confirms that opening an existing database preserves its rows, since switching a database into WAL mode touches an existing file.
Result
There are two measurements, and they are not the same experiment.
The isolated A/B is a cut of about 51 times: 1897.8 us down to 37.1 us.
Do not subtract those numbers from each other, and do not read the A/B as the expected saving on a live create. They are not directly comparable. The 9.24 ms figure came from a live daemon with its real, already populated database and normal background activity. The A/B ran against an isolated fresh database file with nothing else running. The A/B’s default-settings figure (1.9 ms) is itself far lower than the live 9.24 ms, which shows how much the conditions differed. What both numbers support is the relative claim: removing the per-write flushes makes a write dramatically cheaper. The live saving after the change was not re-measured as its own
history_write_us mean, so this post does not claim a specific millisecond figure for a create.
The change was verified with the full workspace tests (300 of 300 passing at the time), a clean cargo clippy --workspace --all-targets, and the integration suite, which showed only two already-documented unrelated flakes and nothing involving the store.
What it cost or risks
The risk is durability, and it should be stated without softening. Withsynchronous=NORMAL in WAL mode, a transaction is flushed at checkpoint time, not at commit. The store’s own doc comment states the consequence. A write is durable against an application crash. Against an operating system crash or power loss, it is durable too, except for whichever transactions landed in the last, not-yet-flushed part of the log. Those could be lost. sqlite documents that this does not corrupt the database in WAL mode; the loss is of recent commits, not of integrity.
That loss is tolerable here for reasons that are specific to this store, and not a general claim that WAL with NORMAL is always fine:
- The write was already best-effort. A failure to record creation only logs a warning. A power loss that drops the last few unflushed rows is a milder form of the same outcome the code already accepts.
- History only means something across a restart, and restarts orphan everything. No live sandbox survives a daemon restart today.
mark_unended_as_orphaned_on_startupmarks every record that was still live as orphaned by restart. Arecord_createdrow lost to a power cut simply never existed after the reboot, andrecord_endedis documented to be a no-op, not an error, when its row is missing. - It is not a ledger. The store records what happened for the benefit of
GEThistory queries. Nothing billing-grade or security-critical depends on it. If that ever changes, the settings should be revisited, and for such a usesynchronous=FULLis the setting to return to.
- Sidecar files. WAL mode creates
-waland-shmfiles next to the database. Anything that copies or backs uphistory.dbas a single file must account for them, or checkpoint first. - A write that is faster in a flush-free way is not free of cost. The log is checkpointed later, so some of the cost moves from the request to a background step inside sqlite. The request path no longer waits for it.
- The live saving is unmeasured. As above: the A/B is isolated, and no live re-measurement of
history_write_usis recorded, so the end-to-end benefit on a create is expected but not quantified here. - Conditions for WAL. sqlite’s WAL mode needs the database on a local filesystem with shared memory support, and is not meant for network filesystems.
SANDKILN_HISTORY_DB_PATHshould point at local storage.
How to reproduce
The A/B harness that produced the 1897.8 us and 37.1 us figures was not committed, so there is no script in the repository that prints them. The live figure can be read from any running daemon, and the isolated comparison can be rebuilt in a few lines. To read the live cost on the current code:history_write_us against total_us in each event. On the current code the write should be a small fraction of a millisecond, not the 9.24 ms that was measured before the change, though this has not been re-recorded.
To rebuild the isolated A/B, write a throwaway test or bench in sandkiln-store that opens two fresh databases on the same disk. Open the first with the store as it is. For the second, open a raw rusqlite connection, leave the default pragmas, and apply the same schema (CREATE TABLE IF NOT EXISTS sandbox_history plus its two indexes) with PRAGMA journal_mode=DELETE; PRAGMA synchronous=FULL;. Time 20 sequential single-row inserts with the same INSERT statement record_created uses, on each, with nothing else running, and compare the means. Keep it out of the repository, since the result depends entirely on the disk. To confirm the settings took effect on a real database file:
journal_mode=WAL persists in the database file, so this prints wal, but synchronous is a per-connection setting and prints sqlite’s default (2) from a fresh shell. The unit test, which reads the pragmas on the store’s own connection, is the accurate check.