Cache vs memory perf in convex function scope
My understanding is that any query that runs in a convex query/mutation only runs against the database once, and further calls return a cached response. I'm trying to understand if this is effectively in-memory.
Contrived scenario:
- A query needs to return a list of hundreds of restaurants from the restaurants table
- Each restaurant can be in one of 10 total regions from the regions table
Option A:
- Run an indexed query that gets all of the regions up front
- Synchronously map over the list of restaurants and add the region data to each
Option B:
- Asynchronously map over the list of restaurants and run
ctx.db.get()
for each region id, resulting in many duplicate calls to the same query
One would assume that option A will be more performant. But if caching is deduped and truly in-memory, it probably won't be meaningfully different. Is one approach better? Does it become worthwhile at scale?2 Replies
Yeah option A is a bit more performant but not by much.
The tradeoff is:
- A is a single db.query that returns 10 documents.
- B is 10 db.queries that run in parallel and each return one document.
The performance difference might come down to the json serialization, since A will serialize 10 region documents and then share the pointers in js, while B will serialize hundreds of region documents to get them from rust to js.
Personally i would do the thing that's simplest to code, since perf difference should be tiny
and eventually we want to remove the json serialization difference altogether with zero-copy operations. Fun stuff
Okay, that's pretty clear. Thanks, Lee.