← Back to Pull requests

Group SQLite Durable Object commits on local workerd #8803

Open
danlapid opened this pull request with 135 lines diff across 7 files
View on GitHub
danlapid danlapid commented on Nov 22, 2026 03:52 UTC
Author

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() unless HAVE_FDATASYNC is defined. fdatasync() still
flushes 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:

  • On macOS, SQLite's full_fsync() always uses fsync() or F_FULLFSYNC, and never calls

  • fdatasync().
  • Windows uses os_win.c.
  • So the define is unconditional rather than a select(), which also keeps
    SQLITE_DEFINES_FOR_LEMON a plain string. It also has no effect on a SqliteDatabase::Vfs built
    with a custom LockManager, since that constructor never uses SQLite's native VFS.

    2. An ActorSqlite option 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 after kj::yield(), on the next turn of the
    event 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 groupCommit constructor parameter set, it waits for
    kj::yieldUntilQueueEmpty() instead. Every request that is ready in this pass of the event loop
    then 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:

  • Each request's output is still held by the output gate until the commit that holds its writes

  • has completed, so durability is unchanged.
  • A failed commit already breaks the output gate for the whole object, so failure behaviour

  • doesn't change either.

    What changes:

  • Implicit transactions of requests that interleave in one pass of the event loop are committed

  • atomically together.
  • An async transaction() that starts while an implicit transaction is open already waits for

  • that transaction (ImplicitTxn::waitForCompletion()). It may now wait until the queue drains,
    rather than for one turn.
  • Code that relied on an await having closed the implicit transaction behaves differently.

  • sql-test.js did this twice: it ran PRAGMA foreign_keys = OFF (and later = ON) inside
    blockConcurrencyWhile(), right after writes. That pragma is a silent no-op inside a
    transaction, 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_sync count 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: ActorSqlite has other users whose commits don't block
    the 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 await between them are still atomic.
    Only the undocumented foreign-keys pattern above notices the difference.

    Measurements

    Setup:

  • The benchmark pits a single SQLite-backed Durable Object on local workerd against Postgres,

  • over the same datasets and workloads, with clients over WebSocket and postgres.js.
  • Each engine is pinned to 1 core with 1 GB, on a Linux VM whose fdatasync p50 is

  • 7–11 ms.
  • Peak ops/s, median of 2 interleaved reps of 10 s each.

  • "stock" is the npm workerd 1.20261121.1 binary. The other DO columns are main at

  • 485ebb7da built with --config=release_linux, with the changes switched on one at a time
    by environment variables (this PR has no such switches).
  • Every reply is held until its writes are on disk, in every column.
  • | 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 |

  • fdatasync alone gives 2.5–3.5× at every concurrency.

  • Group commit then gives up to another 11× at c=32. Durable inserts go from 186 to 7,150/s and

  • TPC-B from 188 to 2,762/s, which is faster than Postgres's 2,132.
  • perf stat at insert c=32 counted about 25–30 commits per fdatasync, against Postgres's 27.5.

  • DO CPU per insert fell from 198 µs to 62 µs, because COMMIT, WAL-index and locking work is

  • shared across the batch.

    These numbers are for local workerd, where durability is a local fsync.

    Testing

  • //src/workerd/io:actor-sqlite-test passes. There are two new tests, each of which writes once

  • and 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.
  • With group commit temporarily made the test default, every other actor-sqlite-test test also

  • passed.
  • sql-test (size enormous, so it needs --test_size_filters=) passes in the @,

  • @all-autogates, @all-compat-flags and @eslint variants. Without the test changes, its
    @ variant fails, which confirms that workerd's server now uses group commit.
  • The other DO, SQLite and storage tests (100 targets in all, from

  • //src/workerd/{io,util,api,api/tests,server/...}, in the @, @all-autogates and
    @all-compat-flags variants) pass on Linux. This includes sqlite-test, sqlite-kv-test,
    sqlite-metadata-test and sqlite-metering-test, which are built with the new define.
  • The "Run workers-sdk tests" check fails until

  • 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 group
    commit, the engine's reply to step.do() waits for its batch's commit, and the test's
    status() request runs first. That PR makes both tests wait for completion. It passes against
    this branch, with and without group commit.
  • The macOS and Windows builds and tests pass in CI.
  • 🤖 Generated with Claude Code

    Diff: 135 lines (+124 / -11) across 7 files Reviewer: jasnell Review duration: 7215m
    All Vouch review confidence checks passed
    Residual risk 0.07 = Change Risk (0.41) × (1 − Review Confidence (0.84)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.41 · 135 lines diff across 7 files
    0.41
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.84 · 5 substantive comment(s)
    0.84
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    84%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.294
    Real PR Conversation Timeline
    10 items
    clanky-the-clanker[bot] clanky-the-clanker[bot] commented on code on Nov 22, 2026 04:59 UTC
    Code Review
    src/workerd/server/factory/worker-factory-actor.c++:1
    @@ -525,7 +525,10 @@ Worker::Actor::MakeActorCacheFunc makeActorCacheFunc(kj::Own<ActorStorage::Impl>
                        SpanParent) -> kj::Promise<void> { return result_task_run(c->commit()); };
           committer = kj::none;
         }
    -    auto sqlite = kj::heap<ActorSqlite>(kj::mv(db), outputGate, kj::mv(commit), *sqliteHooks);
    +    // Each commit syncs the WAL to disk, blocking the thread, so let the requests that run in one
    +    // pass of the event loop share a commit.
    +    auto sqlite = kj::heap<ActorSqlite>(kj::mv(db), outputGate, kj::mv(commit), *sqliteHooks,
    +        /*debugAlarmSync=*/false, ActorSqliteGroupCommit::YES);

    <!-- 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.

    clanky-the-clanker[bot] clanky-the-clanker[bot] commented on code on Nov 22, 2026 04:59 UTC
    Code Review
    src/workerd/io/actor-sqlite.h:1
    @@ -9,10 +9,14 @@
     #include <workerd/io/trace.h>
     #include <workerd/util/sqlite-kv.h>
     #include <workerd/util/sqlite-metadata.h>
    +#include <workerd/util/strong-bool.h>
     
     namespace workerd {
     
     // An implementation of ActorCacheOps that is backed by SqliteKv.
    +// See the `groupCommit` parameter of ActorSqlite's constructor.
    +WD_STRONG_BOOL(ActorSqliteGroupCommit);
    +

    <!-- 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.
    

    clanky-the-clanker[bot] clanky-the-clanker[bot] commented on Nov 22, 2026 04:59 UTC
    Reviewer

    <!-- 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

  • [INFO] Group commit changes local workerd behaviour that user code can see, with no opt-out. The decision should be recorded in the commit message. (src/workerd/server/factory/worker-factory-actor.c++:531)

  • [SUGGESTION] Class doc comment is now attached to the WD_STRONG_BOOL declaration instead of the ActorSqlite class. (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>

    danlapid danlapid commented on code on Nov 22, 2026 15:48 UTC
    Code Review
    src/workerd/io/actor-sqlite.h:1
    @@ -9,10 +9,14 @@
     #include <workerd/io/trace.h>
     #include <workerd/util/sqlite-kv.h>
     #include <workerd/util/sqlite-metadata.h>
    +#include <workerd/util/strong-bool.h>
     
     namespace workerd {
     
     // An implementation of ActorCacheOps that is backed by SqliteKv.
    +// See the `groupCommit` parameter of ActorSqlite's constructor.
    +WD_STRONG_BOOL(ActorSqliteGroupCommit);
    +

    Moved the WD_STRONG_BOOL declaration above the class comment, so the comment is attached to ActorSqlite again (0e004bc2a).

    _🤖 Addressed by Claude Code_

    danlapid danlapid commented on code on Nov 22, 2026 15:48 UTC
    Code Review
    src/workerd/server/factory/worker-factory-actor.c++:1
    @@ -525,7 +525,10 @@ Worker::Actor::MakeActorCacheFunc makeActorCacheFunc(kj::Own<ActorStorage::Impl>
                        SpanParent) -> kj::Promise<void> { return result_task_run(c->commit()); };
           committer = kj::none;
         }
    -    auto sqlite = kj::heap<ActorSqlite>(kj::mv(db), outputGate, kj::mv(commit), *sqliteHooks);
    +    // Each commit syncs the WAL to disk, blocking the thread, so let the requests that run in one
    +    // pass of the event loop share a commit.
    +    auto sqlite = kj::heap<ActorSqlite>(kj::mv(db), outputGate, kj::mv(commit), *sqliteHooks,
    +        /*debugAlarmSync=*/false, ActorSqliteGroupCommit::YES);

    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_

    clanky-the-clanker[bot] clanky-the-clanker[bot] commented on Nov 22, 2026 15:50 UTC
    Reviewer

    <!-- 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:

  • The class doc comment is attached to ActorSqlite again (src/workerd/io/actor-sqlite.h:19-20).

  • The behaviour change on local workerd is recorded in the commit message of 0e004bc2.
  • The rest of the change is sound:

  • startImplicitTxnImpl switches between kj::yield() and kj::yieldUntilQueueEmpty() based on the option.

  • The new tests in actor-sqlite-test.c++ cover both the on and off paths.

  • The 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>

    clanky-the-clanker[bot] clanky-the-clanker[bot] commented on Nov 22, 2026 22:07 UTC
    Reviewer

    <!-- 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:

  • The class doc comment is attached to ActorSqlite again (src/workerd/io/actor-sqlite.h:19-20).

  • The behaviour change on local workerd is recorded in the commit message of 0e004bc2.
  • The rest of the change is sound:

  • startImplicitTxnImpl switches between kj::yield() and kj::yieldUntilQueueEmpty() based on the option.

  • The new tests in actor-sqlite-test.c++ cover both the on and off paths.

  • The 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-commenter codecov-commenter commented on Nov 22, 2026 22:34 UTC
    Contributor

    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>

  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

  • </details>

    jasnell jasnell approved these changes on Nov 23, 2026 19:55 UTC
    Approved Reviewer

    Approved these changes.

    Reviewers
    Assignees
    danlapid danlapid
    Labels
    risk: low confidence: 84% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 41.2% (MEDIUM) · Higher = larger blast radius
    41.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 84% · Lower = rushed or shallow review
    84%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 6.6% · Safe to merge
    6.6%