Skip to content

Checkpoint a WAL only once it holds 1,000 frames the database lacks - #8430

Merged
ymichael merged 1 commit into
mainfrom
bb/8345-slow-fsync-vps-stalls-100-ms-wal-checkpoint-thr_3dvi4j3nau
Dec 1, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/8345-slow-fsync-vps-stalls-100-ms-wal-checkpoint-thr_3dvi4j3nau

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The WAL checkpoint worker (#7108) ran wal_checkpoint(PASSIVE) every 100 ms on bb.db and every plugin data.db. Any commit in that window had its frames copied into the database file straight away, with one fsync of the WAL and one of the database file. Under synchronous=NORMAL commits never fsync on their own, so this turned each small commit (heartbeats, plugin kv rows, event appends) into two fsyncs. It also rewrote hot pages into the database file again and again, where SQLite's default 1,000-frame autocheckpoint writes each page once per batch. On slow-fsync Linux disks this saturated the device and stalled the server. On NVMe it showed up as sustained small 4 KB writes (alpine-vortex: about 150 GB/day).

The issue's second claim, that plugin data.db connections run at synchronous=FULL, does not hold. better-sqlite3 is built with SQLITE_DEFAULT_WAL_SYNCHRONOUS=1, and plugin connections read back synchronous=1 once in WAL mode, so that one-liner would change nothing. The main-thread stalls in the issue's third point are a symptom of the fsync storm, so this PR does not coalesce writes.

What changed

  • packages/db/src/wal-checkpointer.ts: every 100 ms the worker reads mxFrame and nBackfill from the WAL-index (-shm) header with a plain pread of the first 100 bytes, which stays clear of the lock bytes Windows enforces at offset 120 and up. It runs wal_checkpoint(PASSIVE) only once 1,000 frames are not yet backfilled, the point where SQLite's default autocheckpoint would have run inside a commit. If the header cannot be read or has an unexpected version, the worker checkpoints as before. Explicit checkpoint() requests (the vacuum sweep), the 4,096-frame catch-up, the starvation backstop and stop() are unchanged.
  • docs/configuration.md: describes the new trigger.
  • Durability matches bb before Checkpoint SQLite WALs from a worker thread instead of inside commits (behind an experiment) #7108 and SQLite's synchronous=NORMAL default: up to 1,000 frames of recent commits can sit un-fsynced, so a power loss can lose them but cannot corrupt the database.

How you verified

  • New test leaves a WAL alone until it holds a thousand frames the database lacks in packages/db/test/wal-checkpointer.test.ts. It fails on main (the WAL restarts after one small commit) and passes on this branch. All 6 checkpointer tests pass.

  • turbo run test --filter=@bb/server on database-maintenance-sweep, plugin-settings-storage, periodic-sweeps, plugin-state-snapshot and plugin-update: 90 passed. turbo run typecheck --filter=@bb/db: clean.

  • Live production server (macOS, 60 s, sampling the -shm headers): 626 commits caused 299 backfills (about 600 fsyncs/min). On bb.db about 6,000 WAL frames written led to 6,714 frames backfilled, so almost every write was copied straight into the database file. The process wrote about 2.1 MB/s.

  • Linux (node:22-bookworm, strace), real checkpointer module, 34 databases, about 16 small commits/s, 60 s, two runs each:

    fsyncs (setup excluded) block write_bytes slow checkpoint logs
    main about 2,200–2,450 123–132 MB 0–29
    this branch about 30 15 MB 0–1
    no worker (commit autocheckpoint) 0 15 MB 0
  • pnpm start:worktree on this branch, 90 s: bb.db took 4 commits and 16 frames with 0 backfills, and no "Slow WAL checkpoint" lines.

Fixes #8345

AGENT GENERATED

🤖 Generated with Claude Code

The WAL checkpoint worker ran wal_checkpoint(PASSIVE) every 100 ms on
bb.db and every plugin data.db. Any commit in that window got its frames
copied into the database file right away, with an fsync of the WAL and one
of the database. Under synchronous=NORMAL commits themselves never fsync,
so this turned each small commit into two fsyncs and copied hot pages into
the database file again and again. On slow-fsync Linux disks that stalled
the server; on NVMe it showed up as sustained small writes.

The worker now reads mxFrame and nBackfill from the WAL index header every
100 ms and checkpoints once 1,000 frames are pending, the point where
SQLite's default autocheckpoint would have run inside a commit. Explicit
checkpoints, catch-up passes and stop() are unchanged.

Fixes #8345

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit bd85575 into main Dec 1, 2026
54 checks passed
@ymichael
ymichael deleted the bb/8345-slow-fsync-vps-stalls-100-ms-wal-checkpoint-thr_3dvi4j3nau branch December 1, 2026 00:53
scowalt pushed a commit to scowalt/bb that referenced this pull request Dec 1, 2026
…et-bb#8430)

## Human comments

## What was wrong

The WAL checkpoint worker (get-bb#7108) ran `wal_checkpoint(PASSIVE)` every
100 ms on `bb.db` and every plugin `data.db`. Any commit in that window
had its frames copied into the database file straight away, with one
fsync of the WAL and one of the database file. Under
`synchronous=NORMAL` commits never fsync on their own, so this turned
each small commit (heartbeats, plugin kv rows, event appends) into two
fsyncs. It also rewrote hot pages into the database file again and
again, where SQLite's default 1,000-frame autocheckpoint writes each
page once per batch. On slow-fsync Linux disks this saturated the device
and stalled the server. On NVMe it showed up as sustained small 4 KB
writes (alpine-vortex: about 150 GB/day).

The issue's second claim, that plugin `data.db` connections run at
`synchronous=FULL`, does not hold. better-sqlite3 is built with
`SQLITE_DEFAULT_WAL_SYNCHRONOUS=1`, and plugin connections read back
`synchronous=1` once in WAL mode, so that one-liner would change
nothing. The main-thread stalls in the issue's third point are a symptom
of the fsync storm, so this PR does not coalesce writes.

## What changed

- `packages/db/src/wal-checkpointer.ts`: every 100 ms the worker reads
`mxFrame` and `nBackfill` from the WAL-index (`-shm`) header with a
plain `pread` of the first 100 bytes, which stays clear of the lock
bytes Windows enforces at offset 120 and up. It runs
`wal_checkpoint(PASSIVE)` only once 1,000 frames are not yet backfilled,
the point where SQLite's default autocheckpoint would have run inside a
commit. If the header cannot be read or has an unexpected version, the
worker checkpoints as before. Explicit `checkpoint()` requests (the
vacuum sweep), the 4,096-frame catch-up, the starvation backstop and
`stop()` are unchanged.
- `docs/configuration.md`: describes the new trigger.
- Durability matches bb before get-bb#7108 and SQLite's `synchronous=NORMAL`
default: up to 1,000 frames of recent commits can sit un-fsynced, so a
power loss can lose them but cannot corrupt the database.

## How you verified

- New test `leaves a WAL alone until it holds a thousand frames the
database lacks` in `packages/db/test/wal-checkpointer.test.ts`. It fails
on main (the WAL restarts after one small commit) and passes on this
branch. All 6 checkpointer tests pass.
- `turbo run test --filter=@bb/server` on database-maintenance-sweep,
plugin-settings-storage, periodic-sweeps, plugin-state-snapshot and
plugin-update: 90 passed. `turbo run typecheck --filter=@bb/db`: clean.
- **Live production server (macOS, 60 s, sampling the `-shm` headers):**
626 commits caused 299 backfills (about 600 fsyncs/min). On `bb.db`
about 6,000 WAL frames written led to 6,714 frames backfilled, so almost
every write was copied straight into the database file. The process
wrote about 2.1 MB/s.
- **Linux (`node:22-bookworm`, strace), real checkpointer module, 34
databases, about 16 small commits/s, 60 s, two runs each:**

| | fsyncs (setup excluded) | block `write_bytes` | slow checkpoint logs
|
  |---|---|---|---|
  | main | about 2,200–2,450 | 123–132 MB | 0–29 |
  | this branch | about 30 | 15 MB | 0–1 |
  | no worker (commit autocheckpoint) | 0 | 15 MB | 0 |

- **`pnpm start:worktree` on this branch, 90 s:** `bb.db` took 4 commits
and 16 frames with 0 backfills, and no "Slow WAL checkpoint" lines.

Fixes get-bb#8345

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

Slow-fsync VPS stalls: 100 ms WAL checkpoint polling across all plugin DBs, plugin data.db missing synchronous=NORMAL, and sync main-thread DB writes

1 participant