Nika
Nika16mo ago

OptimisticConcurrencyControlFailure on Event Handlers

My backend service is subscribed to blockchain events and whenever there's an event, it inserts the row in the Convex database. There're times when lots of events happen at once, and that's where the Convex is having OptimisticConcurrencyControlFailure problems (VIEW THE PHOTO). The function which handles the event is very simple: 0) We query a table in convex to get a specific row. _id from this queried row will be used for the insert later. 1) we call mutation on convex client with the convex public function 2) Inside the public function we check if there's already an existing row with a specific column value, 3) If it is, we throw an error 4) If not, we insert the row. Data read or written in this mutation changed while it was being run. Consider reducing the amount of data read by using indexed queries with selective index range expressions Each event handler requires at least 2 queries. So I guess that's what causing the error considering the error message above about "amount of data read". Do you have any work-around ideas?
No description
11 Replies
Nika
NikaOP16mo ago
@ian @wjarjoui @orhir10
jamwt
jamwt16mo ago
@Nika hi! so convex retries on conflict several times before it actually bubbles the error up to you which means you probably have a lot of contention on these records. you can either read fewer records during your mutation to decrease the chances of races, or do so this change with relaxed consistency guarantees the way to do the latter is: use an action with seprate queries and mutations. but this removes the ACID guarantees within the mutation since the underlying data can change between the read and the write there are more sophisticated patterns you can do, but it might be worth sitting down on a call with you and looking at your code a bit more closely to brainstorm together. LMK if you're interested in that
wjarjoui
wjarjoui16mo ago
The thing is this mutation literally reads only one record
jamwt
jamwt16mo ago
yeah, then something might be going wrong here. if you all are open to it, sharing the code privately with the team, we might be able to help debug what's up
wjarjoui
wjarjoui16mo ago
sure thing - who can I add on github?
jamwt
jamwt16mo ago
@ian and I can take a quick look and see what we can figure out. I'm https://github.com/jamwt
GitHub
jamwt - Overview
Co-founder of @get-convex . jamwt has 57 repositories available. Follow their code on GitHub.
wjarjoui
wjarjoui16mo ago
OK - just added you Jamie. Ian send me your github and I'll add you too. It's a nestjs backend that listens on blockchain events and adds records to the db when it encounters them. The error you see above happens in our local dev environment when we're seeding: 1. we fire off a number of events to seed the local blockchain 2. the nestjs backend catches those events and adds records in convex (src/events/events.service.ts:handleEvent) 3. the error reported above happens specifically for GRANT_ROLE event (handleWhitelist in the same file)
jamwt
jamwt16mo ago
cool. what's your project name again? we'll create a channel to debug this with you all
wjarjoui
wjarjoui16mo ago
fortunafi/fund-backend
ian
ian16mo ago
GitHub: ianmacartney
wjarjoui
wjarjoui16mo ago
Jamie already gave us a good solution, we're implementing soon, if for whatever reason that is not the root cause we'll circle back

Did you find this page helpful?