Skip to content

Version 0.7.0: speculative snapshots of the writers - #9

Merged
marcobambini merged 4 commits into
mainfrom
version-0.7.0
Nov 27, 2026
Merged

marcobambini merged 4 commits into
mainfrom
version-0.7.0

Conversation

@marcobambini

@marcobambini marcobambini commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Why

The external comparison "SQLite vs WAL vs Turso MVCC vs sqlite-multiwriter" showed the engine flat at ~360 tx/s on a durable disk with 8-24 writers (Turso: 1,000-2,500), with p99/p99.9 latencies of seconds and up to 40 retries per 100 transactions. Its workload inserts rows with the rowid that SQLite picks, so every commit depends on the one before; a commit became visible only after its fsync, so the chain paid one fsync per commit.

What changes

  • Speculative snapshots of the writers (URI mw_spec, on by default; mw_spec=0 or MW_NO_SPEC=1 turn it off). A write transaction reads the newest installed commits; it is still acknowledged only when its own record is durable, and the log is durable in order, so never before the commits it read. Readers that do not write see only durable commits, as before. A commit whose record cannot be written is taken back, and the transactions that read it get SQLITE_BUSY_SNAPSHOT.
  • Hot spots (one process): the turn is taken after the first refusal (was 16) and given back when the commit is installed or conflicts (was after the fsync).
  • Rebase: the helper reads speculatively; the gate waits for installed, not durable, commits.
  • Fixes: a data race reported only by gcc's ThreadSanitizer (bools next to the atomic inflight); mw_multiproc killed its victim before its first commit on slow machines; duplicated test output under TSan; every compiler warning with gcc 12, clang and MinGW-w64.
  • Processes mode durability fix (third commit): a group-sync leader that found its own record already durable synced nothing but still advanced sy_done to the end of the log it had read late, so commits installed meanwhile were acknowledged without an fsync. Pre-existing (reproduced with 0.6.5); found by mw_syncwindow on linux-x86_64 in this PR. New deterministic scenario: old code 459-531 of 800 commits unsynced, now 0.
  • CI: the Android x86_64 job enables KVM; emulator 37 refuses -accel off, so the emulator never booted.
  • bench/events_bench.c (the comparison's workload), test mw_spec, version 0.7.0, README and docs/design.md.

Measurements

Workload of the comparison, 24 writers (before → after):

tx/s p99 retries / 100 tx
macOS, fsync ~3 ms (MW_FULLFSYNC=1) 333 → 2,547 694 → 20 ms 545 → 7
Linux (Docker), fsync ~0.3 ms 1,820 → 7,625 145 → 8.6 ms 239 → 7
Linux, tmpfs / synchronous=OFF -3..-7% 64 → 14 ms 113 → 15

mw_bench, Linux, 4/16/64 threads: contended workloads (hot, groups, samepage, cols, insert-autoinc, crdtinsert) from 1-4k to 17-34k tx/s; bulk, independent, mixed within ±6%. Known trade-offs: without a durable fsync the appends are 3-7% slower from 8 writers up (much shorter tails; MW_SHORT_STARVE=16 restores the old turn); samepage with the rebase at 4-16 threads on macOS -9..-11%. The processes mode is unchanged.

Tested locally

  • macOS: make test 95/95
  • Linux glibc (Docker, arm64): make test 95/95, test-mp, test-io, make test-tsan (clang) 87/87, the TSan list with gcc 87/87
  • Linux musl (Alpine): make test-ci 94/94
  • Windows: cross-built with MinGW-w64 (47 tests and multiwriter.dll, no warnings); the windows-x86_64 build + test job of this PR passed on a real Windows.

The thread tables of the README were measured again with 0.7.0 (second commit): with 16 threads, bulk 8,910 → 51,558 tx/s, own rows on shared pages with rebase 9,788 → 71,788, the same 4 rows 10,189 → 73,234 (p99.9 717 → 0.86 ms).

🤖 Generated with Claude Code

marcobambini and others added 4 commits November 27, 2026 14:03
…he first refusal

A write transaction reads the newest installed commits, not only the durable
ones (URI mw_spec, on by default; mw_spec=0 or MW_NO_SPEC=1 turn it off). A
commit is still acknowledged only when its own record is durable, and the log
is durable in order, so never before the commits it read. Commits that depend
on each other (inserts with SQLite's rowid) now share one group fsync instead
of paying one each: the workload of the external comparison "SQLite vs WAL vs
Turso MVCC vs sqlite-multiwriter", 24 writers, fsync of about 3 ms, goes from
333 to 2,547 tx/s, p99 694 -> 20 ms, retries 545 -> 7 per 100 tx; on Linux
(Docker, fsync 0.3 ms) 1,820 -> 7,625 tx/s.

- spec_inst: a ring of installed epochs, no committer waits for another;
  spec_gen refuses a speculative transaction whose base was taken back (a
  record that could not be written, a recovery); a speculative transaction
  that ends without a commit waits until what it read is durable.
- The helper of the rebase reads speculatively, and the gate it closes waits
  until the assigned commits are installed, not durable.
- One process: the hot-spot turn is taken after the first refusal and given
  back when the commit is installed or conflicts.
- bench/events_bench.c: the workload of the comparison. Test mw_spec.

Also: a data race that gcc's ThreadSanitizer reported (bools next to the
atomic inflight read with one 8-byte load); mw_multiproc kills its victim
after it has committed, not after 150 ms, and flushes stdout before fork; no
compiler warnings with gcc 12, clang and MinGW-w64.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ison of commits that depend on each other

The tables of threads (bulk, groups, hot; 1, 4, 16, 64 threads, 8 s per run,
bench/compare_sqlite.py) and the latency of bulk measured with 0.7.0 on the
same Mac, all runs valid. The rows with processes stay those of 0.6 (the
processes mode did not change). New section: the workload of the external
comparison (bench/events_bench.c), 24 writers, before and after, on macOS and
on Linux.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ble point; CI: KVM for the Android emulator

A committer that became the leader of the group sync and found its own record
made durable already (by the leader before it, which finished between its
look and its turn) synced nothing but still moved sy_done to log_pos as it
read it, after a delay: past records that others had installed meanwhile.
Those commits were acknowledged without an fsync of the log. sy_done now
moves only after this leader's own msync and fsync.

Found by mw_syncwindow in the CI (linux-x86_64; about 1 run in 20 on a Linux
of 4 CPUs, also with 0.6.5). Its new scenario 3 (MW_TEST_SYNC_PRELEAD_US, a
delay before a committer tries to be the leader; run first, in a child, since
the knobs are read once per process) shows it in every run: 459-531 of 800
commits acknowledged without a sync with the old code, 0 now.

CI: the Android x86_64 job gives the runner's user access to /dev/kvm. The
action ran the emulator with -accel off, which emulator 37 (the latest, which
the action installs) refuses: the emulator never started.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…written

The scenario must exercise the rewrite of a prepared relocation (commits that
extend the file while another waits between its preparation and the
publication lock). Under ThreadSanitizer on the CI runner one run had 5
relocations and rewrote none ("FAIL mw_relocprep.c:60: reloc_prep_rewrote >
0"); the data were right. It runs again, up to three times, until a rewrite
happens; the checks of the data are made in every run, and the rewrite must
still happen.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@marcobambini
marcobambini merged commit 6754b90 into main Nov 27, 2026
38 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.

1 participant