Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"test:paged-vfs": "pnpm --filter @platformatic/sqlite-js-stream-paged-vfs build:native && pnpm --filter @platformatic/sqlite-js-stream-paged-vfs build && pnpm --filter @platformatic/sqlite-js-stream-paged-vfs test && node --test packages/sqlite-js-stream/test/integration/paged-restore.test.ts",
"bench": "pnpm --filter @platformatic/sqlite-js-stream bench",
"bench:encode": "pnpm --filter @platformatic/sqlite-js-stream bench:encode",
"bench:small-writes": "pnpm --filter @platformatic/sqlite-js-stream bench:small-writes",
"golden:go": "cd packages/sqlite-js-stream/tools/golden && go run ."
},
"devDependencies": {
Expand Down
13 changes: 13 additions & 0 deletions packages/sqlite-js-stream/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,18 @@ BASELINE=1 pnpm bench:encode
PAGE_SIZE=65536 PAGES=256 pnpm bench:encode
```

[`bench/bench-sync-small-writes.ts`](bench/bench-sync-small-writes.ts) measures what one write pays when
every write is followed by a sync: the time of the local step of a sync, block after block, as the local
LTX files and the WAL grow. It should stay flat:

```bash
pnpm bench:small-writes
# more writes:
SYNCS=5000 BLOCK=500 pnpm bench:small-writes
# with the WAL checkpointed as an application which bounds it does:
CHECKPOINT=250 pnpm bench:small-writes
```

### Compaction & retention

Over time `sync()` writes one level-0 LTX file per transaction. To keep the replica bounded and
Expand Down Expand Up @@ -759,6 +771,7 @@ single-writer, single-file database. If host A has `row id=1 → "X"` and host B
| Each host owns a **different** database | Give each its own prefix, e.g. `app/${tenantId}` — separate backup per DB, no conflict. |
| Multiple app instances sharing one consistent dataset | Don't use SQLite + this replication. Use a client-server DB (Postgres/MySQL) or a real shared-file lock + single writer. |
| Same data on every host (failover / DR) | Keep exactly one writer. On failover, **restore the standby from S3 first**, then let it take the lease — don't let a stale local file start writing. |
| Several `Replica` objects on the same database file | Don't. Create one `Replica` per database file, in one thread of one process: it owns the local files in `<db>-litestream/` and keeps its position in them in memory. A second one writing there at the same time can write a transaction ID twice. |

---

Expand Down
96 changes: 96 additions & 0 deletions packages/sqlite-js-stream/bench/bench-sync-small-writes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* Small writes benchmark.
*
* An application which replicates every write syncs after each of them: a sync
* then holds one small transaction, and its cost is what one write pays. This
* measures that cost as the writes add up, i.e. as the local LTX files and the
* WAL grow: it should stay flat. A cost which grows with the number of local
* files or with the size of the WAL shows as a time which rises block after
* block.
*
* Only the local step of a sync is measured, `LocalDatabase.sync()`: reading
* the WAL and writing the LTX file. Nothing is uploaded.
*
* Tunables (env):
* SYNCS syncs, one after each write (default 1000)
* BLOCK syncs in each reported block (default 100)
* CHECKPOINT checkpoint the WAL every this many syncs, as an
* application which bounds its WAL does; 0 never (default 0)
*
* Run:
* pnpm bench:small-writes
* SYNCS=5000 BLOCK=500 pnpm bench:small-writes
* CHECKPOINT=250 pnpm bench:small-writes
*/

import { readdir, rm, stat } from 'node:fs/promises';
import { dirname } from 'node:path';

import { LocalDatabase } from '../src/db.ts';
import { runCheckpoint } from '../src/index.ts';
import { createWALDB } from '../test/helpers/make-db.ts';

const SYNCS = Number(process.env.SYNCS ?? 1000);
const BLOCK = Number(process.env.BLOCK ?? 100);
const CHECKPOINT = Number(process.env.CHECKPOINT ?? 0);

async function sizeOf(path: string): Promise<number> {
try {
return (await stat(path)).size;
} catch {
return 0;
}
}

async function main(): Promise<void> {
const tdb = createWALDB(['value']);
const db = await LocalDatabase.open(tdb.path);
const update = tdb.db.prepare('UPDATE t SET name = ? WHERE id = 1');

try {
await db.sync();

console.log('small writes: one sync after each write of one row');
console.log(`syncs=${SYNCS} block=${BLOCK} checkpoint=${CHECKPOINT === 0 ? 'never' : `every ${CHECKPOINT} syncs`}`);
console.log('');
console.log('syncs ms/sync local files WAL bytes');

let first = 0;
let last = 0;

for (let done = 0; done < SYNCS; ) {
const count = Math.min(BLOCK, SYNCS - done);
const start = performance.now();

for (let i = 0; i < count; i++) {
done++;
update.run(`value ${done}`);
await db.sync();

if (CHECKPOINT > 0 && done % CHECKPOINT === 0) {
// As an application does between its transactions: the next sync resumes in the WAL which follows.
runCheckpoint(tdb.db, await db.checkpointToken());
}
}

const each = (performance.now() - start) / count;
const files = (await readdir(db.ltxLevelDir(0))).length;
const wal = await sizeOf(tdb.walPath);
console.log(
`${String(done - count + 1).padStart(6)}-${String(done).padEnd(8)} ${each.toFixed(2).padStart(8)} ${String(files).padStart(13)} ${String(wal).padStart(11)}`,
);

if (first === 0) first = each;
last = each;
}

console.log('');
console.log(`last block against the first: ${(last / first).toFixed(2)}x`);
} finally {
db.close();
tdb.close();
await rm(dirname(tdb.path), { recursive: true, force: true });
}
}

await main();
3 changes: 2 additions & 1 deletion packages/sqlite-js-stream/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"build": "tsc -p tsconfig.build.json",
"prepack": "pnpm build",
"bench": "node bench/bench-sync-restore.ts",
"bench:encode": "node bench/bench-ltx-encode.ts"
"bench:encode": "node bench/bench-ltx-encode.ts",
"bench:small-writes": "node bench/bench-sync-small-writes.ts"
},
"dependencies": {
"lz4js": "^0.2.0"
Expand Down
Loading
Loading