Skip to content

perf: keep the cost of a sync flat as the local files and the WAL grow - #57

Merged
marcopiraccini merged 6 commits into
mainfrom
perf/sync-small-writes
Dec 2, 2026
Merged

marcopiraccini merged 6 commits into
mainfrom
perf/sync-small-writes

Conversation

@marcopiraccini

@marcopiraccini marcopiraccini commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

A sync after every small write got slower with each write, until the next snapshot cleared the local files.

To find its position, a sync listed the local LTX files of every level and called stat on each, three times. It now reads their names only, once, and keeps what it published in memory for the next sync; anything else which changes the local files (a snapshot, a compaction, the retention, a restore) makes the next sync read them again. A sync which resumes also read the whole WAL: it now reads its header, the frame before the point it resumes from and the frames after it, and reads it whole whenever it cannot resume. It holds those bytes only, not a buffer of the size of the WAL.

Since a sync no longer reads its position from the disk each time, two instances on the same database could write the same TXID. The position is the one of the local files, kept by the canonical path of the local cache and shared by the instances of a process, so one which syncs after another starts from its file, also once the retention removed the files in between. Their syncs and snapshots run one at a time, from the choice of a TXID to its publication, so that none publishes a TXID which another one wrote and the retention removed meanwhile. A change of the level-0 directory by anything else, another thread or process, makes the next sync read the local files again. Each attempt of a sync writes a temporary file of its own, and publishes it with a hard link, which fails when the name is taken where a rename wrote over it: the sync then reads the local files again.

One change of behavior: on a file system without hard links a sync now fails, with an error which says so. Nothing there publishes a file without writing over another one, and a rename let two syncs write the same TXID.

pnpm bench:small-writes is new: it writes one row, syncs, and repeats, and reports the time of the local step of a sync block after block, as the local files and the WAL grow. Nothing is uploaded. On main and on this branch, on one machine:

Syncs Local files WAL bytes main, ms/sync This branch, ms/sync
1 to 100 101 424,392 3.64 1.26
101 to 200 201 836,392 6.95 1.14
201 to 300 301 1,248,392 10.00 1.07
301 to 400 401 1,660,392 13.71 1.08
401 to 500 501 2,072,392 16.45 1.05
501 to 600 601 2,484,392 21.49 1.18

The last block against the first is 5.9 times on main and 0.9 times here. With the WAL checkpointed every 250 syncs (CHECKPOINT=250) it stays at about 1.1 ms. In an application which syncs after every write, to a Postgres replica, with 16 writes in flight, the writes went from 332 to 639 a second and their median from 47 to 21 ms; one run each.

New tests cover the state kept against the one read from the disk, a new instance, two instances which write in turn, at the same time, across a file the retention removed and through two spellings of one path, a file added by something else, a directory or a link named as an LTX file, a snapshot, a restore, a WAL reset between two syncs, and what a resuming sync allocates for a WAL of several megabytes.

… WAL frames a sync reads

Two instances on the same database could write the same TXID: each keeps
its position in memory, and a rename wrote over the file of the other.
A file is now published with a hard link, which fails when the name is
taken, and the sync reads the local files again.

A sync which resumes allocated a buffer of the size of the WAL to read a
few frames of it. It now holds its header and the frames it reads, which
the reader takes by their offsets.
…te each sync to a file of its own

Two instances on the same database each kept their position: once the
retention removed a file, one of them could write its TXID again. The
position is now the one of the local files, shared by the instances of a
process.

Two syncs which wrote the same TXID at once wrote the same temporary
file, so the one published first could be changed by the other: each
attempt has its own.

Without hard links a file is renamed, as before: looking for the name
first only looked safe.
…t outside changes, and no rename over a file

The position of the local files was shared by the path as it was given:
two spellings of one path had two positions. It is kept by the canonical
path of the local cache.

A thread or a process which does not share it leaves a level-0 directory
which changed: the local files are then read again.

On a file system without hard links two syncs could both rename to the
same TXID. A sync fails there, and says why: nothing else publishes a
file without writing over another one.
A sync picks its TXID and publishes it some awaits later. Another
instance could publish that TXID and the next one meanwhile, and the
retention remove the first: the name was free again, and the sync
published a second file under it, behind the newest one. The syncs and
the snapshots of the instances of a process on the same database now
wait for each other, from the choice of a TXID to its publication.
Comment thread packages/sqlite-js-stream/src/db.ts Outdated
const run = (syncQueues.get(key) ?? Promise.resolve()).then(fn);
const done = run.then(
() => {},
() => {},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can cache these

Comment thread packages/sqlite-js-stream/src/db.ts Outdated
syncQueues.set(key, done);
void done.then(() => {
if (syncQueues.get(key) === done) syncQueues.delete(key);
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't you move this into the previous .then()?

Comment thread packages/sqlite-js-stream/src/db.ts Outdated
const syncStates = new Map<string, SyncState>();

/** The last sync or snapshot of each local cache which runs or waits, see {@link exclusive}. */
const syncQueues = new Map<string, Promise<void>>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please avoid global state

Comment thread packages/sqlite-js-stream/src/db.ts Outdated
let range: { minTXID: bigint; maxTXID: bigint };
try {
range = parseFilename(entry.name);
} catch {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

genrically we should try to swallow errors

Comment thread packages/sqlite-js-stream/src/db.ts Outdated
let fh: FileHandle;
try {
fh = await open(this.walPath(), 'r');
} catch {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please document while swallowing this error is ok

Comment thread packages/sqlite-js-stream/src/db.ts Outdated
}
// The WAL may have grown since its size was read, never shrunk without being reset: what was read is its length.
return new PartialWAL(start + read < size ? start + read : size, header, start, tail.subarray(0, read));
} catch {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please document while swallowing this error is ok

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@marcopiraccini
marcopiraccini merged commit 6c46f15 into main Dec 2, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants