Replies: 3 comments 3 replies
-
If this is a data race, I found Prisma added support for atomics (deep in their docs), which should do the trick |
Beta Was this translation helpful? Give feedback.
-
Yes, this snippet has race conditions. It's prone to a lost update: A and B load questions at the same time, receiving the same number of votes The issue wouldn't be as big if the result of In short, you should use atomic operations for these kinds of things. |
Beta Was this translation helpful? Give feedback.
-
Fyi, I've fixed this in the tutorial update I did today :) (I'm using the atomic number operations) |
Beta Was this translation helpful? Give feedback.
-
Hi,
I was working my way through the tutorial and ended with the code below for the starter voting app. I'm unfamiliar with the how/where Blitz handles mutations/data fetching, but is this technically a race condition for incrementing votes below?
If a client has a value of question.choice.votes stored locally from its initial rendering, isn't there a chance that another client could increment the server vote count from its local value, such that that if our client submits a mutation to increment the server vote count to 1 + (our now old) local value, it won't be correct.
Psudocode:
Client A: Get clientA.votes = 2
Client B: Get clientB.votes = 2
Client B: Mutate server.votes = (clientB.votes + 1 ) = (2 + 1) = 3
Client A: Mutate server.votes = (clientA.votes + 1 ) = (2 + 1) = 3
Ideally, server.votes should equal 4, when in actuality it equals 3
Does Blitz avoid this? Where is votes stored/handleVote called (server/client)? If this is a race condition, would a way to resolve it be to make a custom mutation?
Thanks!
Code (from tutorial):
Beta Was this translation helpful? Give feedback.
All reactions