Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max version check is wrong with high concurrency #1

Closed
jclem opened this issue Sep 7, 2016 · 2 comments
Closed

Max version check is wrong with high concurrency #1

jclem opened this issue Sep 7, 2016 · 2 comments

Comments

@jclem
Copy link

jclem commented Sep 7, 2016

With lots of operations coming in from multiple sites, it is possible in this adapter to commit an operation with a version that has already been committed.

Given a scenario where User A and User B concurrently submit operation version 2, the max version check here will pass for both operations, and one operation will not be rebased against the one before it.

In order to resolve this, you can move the max version check inside of your transaction and append FOR UPDATE to the max version query:

SELECT max(version) AS max_version FROM ops WHERE collection = $1 AND doc_id = $2 FOR UPDATE

This will create a lock on this row for the duration of the transaction, ensuring that the later operation's max version check query blocks until the previous operation is fully committed.

I'm not actually using this adapter because my schema is different from what sharedb-postgres assumes, but I just spent a lot of time debugging this, and hopefully it can help you, too!

@brainkim
Copy link

@jclem I implemented row-locking in a knex port here (https://github.com/brainkim/sharedb-knex/blob/master/index.js#L35-L70). You can’t use for update in queries with aggregate functions (in postgres at least) so I moved the for update to a separate select. I have no idea what I'm doing when it comes to databases but it feels like this should fix the issue you describe, right?
:3

@jclem
Copy link
Author

jclem commented Jun 2, 2018

@brainkim I’m not involved with this project anymore, so I can’t offer much guidance here. If it helps, I can point you to my old company’s lib we used to commit ShareDB operations: https://github.com/usecanvas/realtime-v2/blob/master/sharedb/lib/sharedb-postgres-canvas.js

Sorry I can’t be of more help. Good luck!

@jclem jclem closed this as completed Jun 2, 2018
alecgibson pushed a commit that referenced this issue Feb 8, 2023
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

No branches or pull requests

2 participants