Skip to content

Write each changed B-tree node once per async batch - #307

Merged
reeceyang merged 1 commit into
mainfrom
batch-node-writes
Dec 5, 2026
Merged

reeceyang merged 1 commit into
mainfrom
batch-node-writes

Conversation

@reeceyang

@reeceyang reeceyang commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Keeps node writes pending in the batch's NodeCachingDatabaseWriter and writes each changed node once when the batch ends. Pending writes count against the transaction's headroom so the flush always fits.

With the whole stack of changes, a 1024-op batch over 220 trees went from about 11k reads and 2.4k writes to 2.2k reads and 600 writes, and drain throughput rose from 279 to 481 ops/s. A batch over 11 trees used to run out the 1s user-code limit about 2.7s in and be retried forever, stalling the worker; it now finishes in about 0.4s.

Stack


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

🤖 Generated with Claude Code

@pkg-pr-new

pkg-pr-new Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@convex-dev/aggregate@307

commit: 4e0a605

@reeceyang
reeceyang force-pushed the batch-node-writes branch 2 times, most recently from b13d55a to 2968655 Compare December 3, 2026 18:58
@reeceyang
reeceyang force-pushed the batch-node-writes branch 2 times, most recently from 51642f8 to 68730cd Compare December 3, 2026 22:02
@reeceyang
reeceyang force-pushed the batch-node-cache branch 2 times, most recently from 4f93c01 to dcbf793 Compare December 3, 2026 22:27
@reeceyang
reeceyang force-pushed the batch-node-writes branch 2 times, most recently from fb302cc to daf838b Compare December 3, 2026 22:43
@reeceyang
reeceyang force-pushed the batch-node-cache branch 2 times, most recently from 26ee7df to e363966 Compare December 3, 2026 22:43
@reeceyang
reeceyang force-pushed the batch-node-writes branch 2 times, most recently from 8e7cceb to bc66a1f Compare December 3, 2026 22:44
@reeceyang
reeceyang force-pushed the batch-node-writes branch 2 times, most recently from 2a9f92c to f2ba2f7 Compare December 4, 2026 00:33
@reeceyang
reeceyang force-pushed the batch-node-writes branch 3 times, most recently from 0ac8c1e to 03058d4 Compare December 4, 2026 01:27
@reeceyang
reeceyang changed the base branch from batch-node-cache to main December 4, 2026 01:27
@reeceyang
reeceyang force-pushed the batch-node-writes branch 4 times, most recently from 276478e to b23eac9 Compare December 4, 2026 18:58
@reeceyang
reeceyang marked this pull request as ready for review December 4, 2026 18:58

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread src/component/worker.ts Resolved
Comment thread src/component/worker.test.ts Resolved
@reeceyang
reeceyang marked this pull request as draft December 4, 2026 19:56
@reeceyang
reeceyang removed the request for review from ianmacartney December 4, 2026 19:56
@reeceyang
reeceyang marked this pull request as ready for review December 4, 2026 21:41

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 5 files

Re-trigger cubic

if (entry?.node && entry.pendingSize !== undefined) {
this.pendingDocuments--;
this.pendingWriteBytes -= entry.pendingSize;
this.pendingReadBytes -= entry.storedSize;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still charge for reads when the user deletes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, though we directly delete the node from the database, so the usage will have been charged already (and won't be charged again when flushing). Added a comment to clarify

Comment thread src/component/worker.ts
return BATCH_LIMITS.every(
(limit) => metrics[limit].remaining > FINISH_RESERVE[limit],
(limit) =>
metrics[limit].remaining - pending[limit] > FINISH_RESERVE[limit],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
metrics[limit].remaining - pending[limit] > FINISH_RESERVE[limit],
metrics[limit].remaining - (pending[limit] ?? 0) > FINISH_RESERVE[limit],

we may add more metrics in the future. unless BATCH_LIMITS is already type-checked against BatchLimit keys in pending?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it is defined as

const BATCH_LIMITS = [
  "bytesRead",
  "bytesWritten",
  "databaseQueries",
  "documentsRead",
  "documentsWritten",
] as const;

so it should be future proof

@ianmacartney ianmacartney left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting a bit nervous about how much of the convex internals we're having to duplicate.. ideally we could have the syscalls be so efficient that we don't need to do these user-space optimizations of optimizations we already have in Rust

@reeceyang

Copy link
Copy Markdown
Contributor Author

Yeah... maybe runtime v2 will help? or maybe this is a good candidate for a Rust component...

Every write patches each node on its path to keep counts and sums current, so
a batch rewrote a tree's root once per operation. Hold node writes in the
batch's node cache and flush them when the batch ends, counting the held
writes against the transaction's headroom so the flush always fits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@reeceyang
reeceyang merged commit 19631ef into main Dec 5, 2026
3 checks passed
@github-actions github-actions Bot mentioned this pull request Dec 5, 2026
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.

2 participants