Group SQLite Durable Object commits on local workerd #8803
<!-- durable-reviewer:finding -->
ℹ️ INFO · Code Quality
Group commit changes local workerd behaviour that user code can see, with no opt-out. The decision should be recorded in the commit message.
With group commit on, an await no longer reliably closes the implicit transaction. Code that runs PRAGMA foreign_keys = OFF/ON after writes (as sql-test.js did) now silently does nothing on local workerd. The repository's backward-compatibility guidance asks for an explicit acceptance of this risk in the commit message. The PR description explains it well, so it would be worth copying that into the commit message. A config switch to turn group commit off could also help users who hit this.
<!-- durable-reviewer:finding -->
💬 SUGGESTION · Code Quality
Class doc comment is now attached to the WD_STRONG_BOOL declaration instead of the ActorSqlite class.
The // An implementation of ActorCacheOps that is backed by SqliteKv. comment used to sit directly above class ActorSqlite. It now sits above the new WD_STRONG_BOOL declaration. Put the strong-bool and its own comment first, so the class comment stays attached to the class.
// See the `groupCommit` parameter of ActorSqlite's constructor.
WD_STRONG_BOOL(ActorSqliteGroupCommit);
// An implementation of ActorCacheOps that is backed by SqliteKv.
<!-- durable-reviewer:review run=24776aa9-4856-46ea-bed7-b1cf152c150b -->
Superseded by the review of 0e004bc2.<details>
<summary>This review, of <code>ed216689</code></summary>
💬 Approved with comments — The group-commit change is correct and tested. There is no blocking or warning-level issue, only a misplaced doc comment and a note that the local behaviour change should be recorded in the commit message.
Findings: 1 info · 1 suggestion (2 are inline comments on the diff)
<sub>The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.</sub>
---
📋 Code Quality: 💬 1 info · 1 suggestion
One misplaced doc comment, and a note on recording the local behaviour change.
Recommendation: 💬 Optional Nits
src/workerd/server/factory/worker-factory-actor.c++:531)src/workerd/io/actor-sqlite.h:17)<details>
<summary>View full review</summary>
src/workerd/io/actor-sqlite.h:16-18: the class doc comment // An implementation of ActorCacheOps... now sits above WD_STRONG_BOOL(ActorSqliteGroupCommit) instead of class ActorSqlite.src/workerd/server/factory/worker-factory-actor.c++:528-531: turning on group commit changes local behaviour that user code can see. One example is PRAGMA foreign_keys run after writes inside blockConcurrencyWhile(), which now silently does nothing. The repository's backward-compatibility guidance asks for that decision to be recorded in the commit message; at the moment it is only in the PR description.</details>
---
<details>
<summary>✅ All other checks passed</summary>
🔒 Security: ✅ LGTM
No security concerns.
---
📖 AGENTS.md: ✅ LGTM
No AGENTS.md updates needed.
---
⚡ Performance: ✅ LGTM
HAVE_FDATASYNC and group commit both reduce the cost of each commit. The commit can't be held back indefinitely: KJ runs yieldUntilQueueEmpty() events before polling for new I/O, so the wait ends at the end of each pass of the event loop.
---
📝 Documentation: ✅ LGTM
No documentation issues.
</details>
---
<sub>Reviewed commit: ed216689</sub>
</details>
Moved the WD_STRONG_BOOL declaration above the class comment, so the comment is attached to ActorSqlite again (0e004bc2a).
_🤖 Addressed by Claude Code_
Recorded the accepted behaviour change in the message of 0e004bc2a: an await no longer reliably closes the implicit transaction on local workerd, so e.g. PRAGMA foreign_keys right after writes can silently do nothing; documented guarantees are unchanged, and embedders that don't set the option keep today's behaviour. I didn't add a config switch to turn it off; happy to if maintainers want one.
_🤖 Addressed by Claude Code_
<!-- durable-reviewer:review run=605dd944-b68c-4ee5-b642-6343662e8363 -->
Superseded by the review of c322e844.<details>
<summary>This review, of <code>0e004bc2</code></summary>
✅ Approved — Both earlier notes (the doc comment placement and recording the behaviour change in the commit message) are addressed, and no new issues were found in the group-commit or HAVE_FDATASYNC changes.
<sub>The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.</sub>
---
<details>
<summary>✅ All checks passed</summary>
🔒 Security: ✅ LGTM
Group commit still holds each request's output behind the output gate until its writes are committed. HAVE_FDATASYNC doesn't change what reaches disk, and it doesn't change who can access it.
---
📖 AGENTS.md: ✅ LGTM
No AGENTS.md updates needed.
---
📋 Code Quality: ✅ LGTM
Both earlier points are resolved at this commit:
ActorSqlite again (src/workerd/io/actor-sqlite.h:19-20).The rest of the change is sound:
startImplicitTxnImpl switches between kj::yield() and kj::yieldUntilQueueEmpty() based on the option.actor-sqlite-test.c++ cover both the on and off paths.sql-test.js and sql-test-tail.js updates match the added storage.sync() calls.---
⚡ Performance: ✅ LGTM
HAVE_FDATASYNC and group commit both reduce the cost of each commit. yieldUntilQueueEmpty() resumes at the end of each pass of the event loop, so a commit can't be deferred indefinitely. A single request still commits after one turn.
---
📝 Documentation: ✅ LGTM
No documentation issues.
</details>
---
<sub>Reviewed commit: 0e004bc2</sub>
</details>
<!-- durable-reviewer:review run=8d9ac4e4-fbe6-441e-9934-ce3645768402 -->
✅ Approved — Both earlier notes (the doc comment placement and recording the behaviour change in the commit message) are addressed, and no new issues were found in the group-commit or HAVE_FDATASYNC changes.
<sub>The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.</sub>
---
<details>
<summary>✅ All checks passed</summary>
🔒 Security: ✅ LGTM
Group commit still holds each request's output behind the output gate until its writes are committed. HAVE_FDATASYNC doesn't change what reaches disk, and it doesn't change who can access it.
---
📖 AGENTS.md: ✅ LGTM
No AGENTS.md updates needed.
---
📋 Code Quality: ✅ LGTM
Both earlier points are resolved at this commit:
ActorSqlite again (src/workerd/io/actor-sqlite.h:19-20).The rest of the change is sound:
startImplicitTxnImpl switches between kj::yield() and kj::yieldUntilQueueEmpty() based on the option.actor-sqlite-test.c++ cover both the on and off paths.sql-test.js and sql-test-tail.js updates match the added storage.sync() calls.---
⚡ Performance: ✅ LGTM
HAVE_FDATASYNC and group commit both reduce the cost of each commit. yieldUntilQueueEmpty() resumes at the end of each pass of the event loop, so a commit can't be deferred indefinitely. A single request still commits after one turn.
---
📝 Documentation: ✅ LGTM
No documentation issues.
</details>
---
<sub>Reviewed commit: c322e844 — same changes as 0e004bc2, so this is that commit's review (no new review was run; line numbers are as of 0e004bc2)</sub>
Codecov Report
:x: Patch coverage is
73.43750% with 17 lines in your changes missing coverage. Please review.:white_check_mark: Project coverage is 67.74%. Comparing base (
ca34b08) to head (c322e84).:warning: Report is 9 commits behind head on main.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/workerd/io/actor-sqlite-test.c++ | 69.09% | 0 Missing and 17 partials :warning: |
<details><summary>Additional details and impacted files</summary>
@@ Coverage Diff @@
## main #8803 +/- ##
==========================================
- Coverage 67.78% 67.74% -0.04%
==========================================
Files 842 846 +4
Lines 214756 214954 +198
Branches 32572 32595 +23
==========================================
+ Hits 145569 145631 +62
- Misses 49947 50065 +118
- Partials 19240 19258 +18
</details>
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
Approved these changes.
jasnell
danlapid
Two changes that let a local workerd Durable Object group its SQLite commits, as Postgres does.
Profiling a SQLite-backed Durable Object on local workerd showed durable writes capped at about
190 commits/s, whatever the concurrency: each request's implicit transaction committed on its own,
each with a full
fsync()of the WAL.Changes
1. Build SQLite with
HAVE_FDATASYNC=1(build/BUILD.sqlite3).SQLite's unix VFS falls back to
fsync()unlessHAVE_FDATASYNCis defined.fdatasync()stillflushes the metadata needed to read the data back, including the file size. It only skips
timestamps, and Postgres uses it by default for the same reason.
The define only matters on Linux:
full_fsync()always usesfsync()orF_FULLFSYNC, and never callsfdatasync().os_win.c.So the define is unconditional rather than a
select(), which also keepsSQLITE_DEFINES_FOR_LEMONa plain string. It also has no effect on aSqliteDatabase::Vfsbuiltwith a custom
LockManager, since that constructor never uses SQLite's native VFS.2. An
ActorSqliteoption to commit implicit transactions once the event queue is empty,turned on by workerd's server (
io/actor-sqlite.{h,c++},server/factory/worker-factory-actor.c++).Today,
ActorSqlite::startImplicitTxnImpl()commits afterkj::yield(), on the next turn of theevent loop. An event that becomes ready after the commit was scheduled starts a new transaction,
so concurrent requests almost never share a commit. With the new
ActorSqliteGroupCommit groupCommitconstructor parameter set, it waits forkj::yieldUntilQueueEmpty()instead. Every request that is ready in this pass of the event loopthen lands in one transaction, one WAL append and one sync. While that sync blocks the thread, the
next batch queues up in the socket buffers, so the batch size adapts to load (Postgres's group
commit works the same way). A single request still commits after one turn, so its latency doesn't
change.
What stays the same:
has completed, so durability is unchanged.
doesn't change either.
What changes:
atomically together.
transaction()that starts while an implicit transaction is open already waits forthat transaction (
ImplicitTxn::waitForCompletion()). It may now wait until the queue drains,rather than for one turn.
awaithaving closed the implicit transaction behaves differently.sql-test.jsdid this twice: it ranPRAGMA foreign_keys = OFF(and later= ON) insideblockConcurrencyWhile(), right after writes. That pragma is a silent no-op inside atransaction, and it only took effect because the earlier implicit transaction happened to commit
before the critical section's callback ran. With group commit, that transaction is still open
there: the pragma does nothing, and later steps fail. Both steps now
await storage.sync()first, and the tail worker's expected
durable_object_storage_synccount goes from 4 to 6.The documented way to relax foreign keys in a Durable Object is
PRAGMA defer_foreign_keys,but applications run on local workerd may copy the same pattern.
Why an option that only workerd turns on:
ActorSqlitehas other users whose commits don't blockthe thread on a local sync, and for them the trade-off is different. So the option defaults to off
and workerd's server turns it on. The new parameter is last and defaulted, so existing callers
compile unchanged and keep today's behaviour.
Not a compatibility flag: the guarantees a Worker is promised don't change. Writes are still
durable before output is released, and writes with no
awaitbetween them are still atomic.Only the undocumented foreign-keys pattern above notices the difference.
Measurements
Setup:
over the same datasets and workloads, with clients over WebSocket and postgres.js.
fdatasyncp50 is7–11 ms.
workerd1.20261121.1 binary. The other DO columns aremainat485ebb7dabuilt with--config=release_linux, with the changes switched on one at a timeby environment variables (this PR has no such switches).
| cell | stock | + fdatasync | + fdatasync + group commit | Postgres |
|---|---:|---:|---:|---:|
| insert c=1 | 162 | 576 | 578 | 594 |
| insert c=8 | 184 | 612 | 2,231 | 2,705 |
| insert c=32 | 186 | 625 | 7,150 | 10,059 |
| pgbench TPC-B c=1 | 184 | 456 | 444 | 336 (529 as PL/pgSQL) |
| pgbench TPC-B c=8 | 185 | 467 | 1,358 | 1,397 (1,475) |
| pgbench TPC-B c=32 | 188 | 476 | 2,762 | 1,917 (2,132) |
| YCSB-A c=32 | 631 | 1,661 | 5,557 | 6,392 |
fdatasyncalone gives 2.5–3.5× at every concurrency.TPC-B from 188 to 2,762/s, which is faster than Postgres's 2,132.
perf statat insert c=32 counted about 25–30 commits perfdatasync, against Postgres's 27.5.shared across the batch.
These numbers are for local workerd, where durability is a local
fsync.Testing
//src/workerd/io:actor-sqlite-testpasses. There are two new tests, each of which writes onceand then again from an event that becomes ready after the commit was scheduled:
- "with group commit, writes of requests that run in one turn share a commit": one commit, and
the output gate holds both writes until it completes.
- "without group commit, a write that runs later in the same turn gets its own commit": two
commits, as before.
actor-sqlite-testtest alsopassed.
sql-test(sizeenormous, so it needs--test_size_filters=) passes in the@,@all-autogates,@all-compat-flagsand@eslintvariants. Without the test changes, its@variant fails, which confirms that workerd's server now uses group commit.//src/workerd/{io,util,api,api/tests,server/...}, in the@,@all-autogatesand@all-compat-flagsvariants) pass on Linux. This includessqlite-test,sqlite-kv-test,sqlite-metadata-testandsqlite-metering-test, which are built with the new define.cloudflare/workers-sdk#17125 is merged.
Two Miniflare Workflows tests asserted that the request creating a Workflow instance already
sees it
complete, which held only because of how quickly the run used to finish. With groupcommit, the engine's reply to
step.do()waits for its batch's commit, and the test'sstatus()request runs first. That PR makes both tests wait for completion. It passes againstthis branch, with and without group commit.
🤖 Generated with Claude Code