Starlord
Starlord•2mo ago

Suboptimal Caching Behavior for Public Queries

Current Behavior - Query results are only cached per user session - Cache is not shared between different users or sessions - Even public queries with stable parameters require fresh DB hits for each new user/session - Example: Product listing queries with same filters/pagination hit DB for each new visitor Expected Behavior - Public queries with identical parameters should be cached across users/sessions - Queries that don't depend on user-specific data (like ctx.auth.getUserId()) should leverage shared cache - Cache invalidation should only occur when underlying data changes Impact - Increased database bandwidth consumption - Higher latency for common queries - Unnecessary load on database for identical queries - Cost implications for high-traffic applications
47 Replies
Convex Bot
Convex Bot•2mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
lee
lee•2mo ago
If a query doesn't read ctx.auth, it is cached between users. Queries that do authentication and authorization checks are trickier. You usually don't want to just check that the user is logged in, you want to check that they have access to the requested data. So we need to run custom code based on the user and on the fetched data. Making sub-queries cachable is an active area of development. Thanks for bumping it, it helps with prioritizing
Starlord
StarlordOP•2mo ago
this problem might happen only for paginated queries
Starlord
StarlordOP•2mo ago
i even wrote own client side implementation where id is always same (caused cache invalidation on every component mount). that helped only if the query is used within same session.
No description
Starlord
StarlordOP•2mo ago
new user or new user session = no cache
lee
lee•2mo ago
Ah yes paginated queries are only cached in certain circumstances. I see you discovered the id field which intentionally busts the cache when it's not safe. I don't think this is related to users being logged in. What is your desired behavior?
Starlord
StarlordOP•2mo ago
can be tested with opening private tab (no user data) paginated queries are used on large datasets. that means every user and every new user session will fetch database again this is causing large database comsumsion and without this id "fix" it causing database hits even within same session if id increases so for some reason paginated queries cache is connected to user session and there is no reason for this
lee
lee•2mo ago
do your paginated queries not call ctx.auth?
Starlord
StarlordOP•2mo ago
no they dont all parameters are stable
lee
lee•2mo ago
then this behavior is unexpected i'll look into it
Starlord
StarlordOP•2mo ago
you can test it even for same user opening private tab this id thing needs also another solution thanks i came into this by seeing large database consumtions of paginated queries without caching when app is live it will hit all limits within a day
lee
lee•2mo ago
okay i see this behavior. i think it's due to this feature: https://stack.convex.dev/fully-reactive-pagination . i'm not sure if we can do anything about it in the short term, but we'll discuss. you can control your own caching by writing your own pagination with https://stack.convex.dev/pagination , but it's pretty tricky
Fully Reactive Pagination
Paginating over large datasets is tricky when the data is changing. Naive approaches result in missing or duplicated data. The trick is to rewrite lim...
Take Control of Pagination
Convex offers robust control over pagination with a powerful function, getPage, enabling complex edge cases. In this article, we go over how to use th...
Starlord
StarlordOP•2mo ago
I understand. Thanks for checking out. Would be great to find easier solution than custom pagination
lee
lee•2mo ago
Also if you want to turn off the reactive-pagination-guarantees, you can use import { paginator } from "convex-helpers/server/pagination"
Starlord
StarlordOP•2mo ago
So use different paginator
lee
lee•2mo ago
Yes. The risk is that pages might not be contiguous if there are insertions/deletions. But it does make caching work just like any other query
Starlord
StarlordOP•2mo ago
well i see its different syntax and need to rewrite queries
Starlord
StarlordOP•2mo ago
No description
Starlord
StarlordOP•2mo ago
did not help. first query after signin is not cached params are same i still think there is problem not only with paginated queries but queries in general
Starlord
StarlordOP•2mo ago
No description
Starlord
StarlordOP•2mo ago
even non pagianted queries are sometimes not cached for no reason
Starlord
StarlordOP•2mo ago
No description
Starlord
StarlordOP•2mo ago
this is everything for same user but after login. this is simple query without any dynamic params i am pretty sure queries are not cached between different sessions
lee
lee•2mo ago
Can you provide the source code of the query? The query cache doesn't know what a session is, so this seems unlikely to me
Starlord
StarlordOP•2mo ago
it doesnt depend on query code it happens for every query please look at the screenshot query is not used from cache when user logs in after findorcreateuser please try to login again and use same query
Starlord
StarlordOP•2mo ago
here is serverside query for example
No description
Starlord
StarlordOP•2mo ago
eveything static data
lee
lee•2mo ago
i can repro. will look further
Starlord
StarlordOP•2mo ago
maybe this will also solve paginated problem pretty sure the problem is that query cache is connected to user session id
lee
lee•2mo ago
yeah it does seem like that's the problem. i don't know why i thought query cache would be shared between user logins
Starlord
StarlordOP•2mo ago
What's the reason why it should not be shared betweens user sessions and different users for public queries?
lee
lee•2mo ago
It should. I think this is a missing feature (clearly useful to the extent that several of us thought it was already implemented). We'll probably implement it soon. Sorry about this missing feature
Starlord
StarlordOP•2mo ago
please update me later if a fix is planed. thanks i dont know otherwise how to solve large database badwith consumtion for alot of users any info maybe regarding this? 🙂
ballingt
ballingt•2mo ago
it's going to happen, I'm not going to put anyone on the hook with a deadline 😛 we agree this is the right behavior, we'll update this thread with more when we know
Starlord
StarlordOP•2mo ago
thank you
lee
lee•2mo ago
GitHub
[ENG-8173] share query cache for queries that don't check auth (#32...
Detect in the isolate whether ctx.auth.getUserIdentity is ever called. If not, store the query result with identity: None in the query cache. When checking whether a cache entry matches, we fi...
Starlord
StarlordOP•2mo ago
great thank you appreciate your job hey Lee can you check please if the change is live? seems not yet
ballingt
ballingt•2mo ago
That's correct, it's not.
lee
lee•2mo ago
we're deploying that change ^ today. however, i just realized there are cases where it still doesn't cache when it should. we're continuing to work on it, but it may take a few more days. specifically, if two users request the same query at the same time, we don't know yet whether the query will check ctx.auth so the query executes twice. this happens frequently if both users are subscribed to the same query and the data changes, because then both queries rerun at the same time. 😢
Starlord
StarlordOP•2mo ago
ok understand thanks
lee
lee•5w ago
the full change has deployed today
Starlord
StarlordOP•4w ago
there is still a problem that paginated queries are not cached but dont know if it depends on users session of queries parameters
Starlord
StarlordOP•3w ago
No description
Starlord
StarlordOP•3w ago
i dont mind to pay more for subscription. but this database usage of paginated queries is killing me. this is just within 2 days after launching the app with 3k users
Starlord
StarlordOP•3w ago
No description
lee
lee•3w ago
This is including your hacky thing that sets the pagination id to 0? We were thinking of shipping that change officially, but if you're still not getting enough caching even with that change, we would like to determine why
Starlord
StarlordOP•3w ago
yes with id 0 this is is dynamic query. it doesnt depend on user but depends on pagination and products filter + combines results from multiple tables

Did you find this page helpful?