Lot's of simultaneous edits on same document


#1

Hi guys! We’re using Couchbase Community Edition 4.5. Here is the problem we’ve stuck at…

We have a document that, among other keys, contains a key that holds an array of JSON objects. Each object in the array has a boolean key. Example:

{
…,
test: [
{
blah: false
},

],

}

The problem is when multiple clients try to change some of the array elements (not the same index) to true.
Many of the changes are “getting lost”. All the changes must be made…

We are using N1QL, we have searched numerous solutions, but nothing seems to work right… We have read about CAS etc, but it’s confusing!!

Could anyone help a bit?


#2

If you are using N1QL UPDATE statement, you should have got error when CAS value mismatch and you need to retry.

General info on CAS


#3

I get the error, I setup a loop and try 10 times…

The problem is that the query needs some time to complete, so when there are lots of requests, inevitably some of them get rejected and the user has lost quite some time waiting! :


#4

Also, see this: https://blog.couchbase.com/optimistic-or-pessimistic-locking-which-one-should-you-pick/

In Couchbase, we use the optimistic locking when updating a document. We don’t hold a lock and instead use CAS to check we’re writing back to the same version we read. This will cause conflicts and you need a way to back off and retry.