Skip to content

Cache B-tree nodes while applying an async batch - #306

Merged
reeceyang merged 1 commit into
mainfrom
batch-node-cache
Dec 4, 2026
Merged

reeceyang merged 1 commit into
mainfrom
batch-node-cache

Conversation

@reeceyang

@reeceyang reeceyang commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Gives the batch worker a NodeCachingDatabaseWriter, a wrapper around ctx.db that keeps B-tree nodes in memory for the batch and passes other tables through, since most reads in a batch re-read a root or internal node that an earlier write already read. On top of #305, this cut document reads per 1024-op batch from about 7.4k to 4.2k in the same load test.

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@306

commit: 1168555

@reeceyang
reeceyang force-pushed the batch-node-cache branch 2 times, most recently from 6b09aeb to f80b0e1 Compare December 2, 2026 23:44
@reeceyang
reeceyang changed the base branch from btree-max-node-size to main December 2, 2026 23:44
@reeceyang
reeceyang force-pushed the batch-node-cache branch 7 times, most recently from 4f93c01 to dcbf793 Compare December 3, 2026 22:27
@reeceyang
reeceyang marked this pull request as ready for review December 3, 2026 22:41
@reeceyang
reeceyang force-pushed the batch-node-cache branch 2 times, most recently from 26ee7df to e363966 Compare December 3, 2026 22:43
Most reads in a batch re-read a node the batch already read, since
consecutive writes to a tree share its root and internal nodes. Route the
B-tree's node reads and writes through a typed NodeStore, and give the batch
worker one that keeps nodes in memory for the batch so those reads skip the
database. The worker stops a cycle once the cache holds 8 MiB of nodes, which
keeps it well under the JavaScript heap limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copy link
Copy Markdown
Member

This seems reasonable, but i'm curious how much of this is already happening automatically? Given that multiple db.get requests will short-circuit and db.patch just changes the local proposed data on each change. And we mark resource limits as de-duped fetches, so I don't think this will keep them from hitting that limit.

Does this affecting timing / resource limits? Or is it just hypothetically faster? I ask in part b/c a cache is a bit risky, since if there's a codepath that doesn't use the cache then it'll be incorrect.

Copy link
Copy Markdown
Member

Or is the thesis that the syscall overhead contributing to the 1s timeout?

@reeceyang

Copy link
Copy Markdown
Contributor Author

Yeah, the thesis is that the syscall overhead contributes to the timeout, especially repeated db.get calls to the same node (and copying the objects over into JS each time). I had an agent benchmark it and the caching helps a lot with throughput

Comment on lines +124 to +128
await this.db.replace(
table as TableName,
id as Id<TableName>,
value as WithOptionalSystemFields<Doc<TableName>>,
);

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.

why aren't we updating the cache in this scenario?

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.

ah we're skipping all replaces on this table, nm

@reeceyang
reeceyang merged commit e31ed22 into main Dec 4, 2026
3 checks passed
@github-actions github-actions Bot mentioned this pull request Dec 4, 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