I have multiple hosts trying to execute cron. Only one should win and continue execution.
To make this possible, I have put one doc in couchbase. Multiple hosts trying to get the doc and modify the doc and replace it with cas. if it successed start cron execution, if cas miss exception then exit(i.e. some other host won and running cron).
So this is my understanding please correct me if I am wrong.
CAS based concurrent mutation is optimistic approach to resolve concurrent update contention, but if correct cas value passed by both the updater at the same time, it will allowed to mutate. But it is very rare and corner case.
getAndLock based concurrent mutation is pessimistic approach, where exactly one will get the lock and will be allowed to update the doc in given time window. after the time window passed, lock will be released by server and anyone can get/update the document.
In my case, exactly one should win, so I should use getAndLock.
With CAS mutation, only one would win. There should be no possibility that two mutations with the same CAS would apply, as the CAS is changed by the first mutation.
@nihar.rathod concurrent/racing access on the same document with different CAS is certainly not possible.
Are you sure they are not calling one after another and how do you actually assert they are racing each other?
Btw this cas issue aside, you can use the RawJsonDocument to store and load the raw json string directly, no need to go through fromsjon or toString() and save some allocations.
From my quick reading you’re correct, one should succeed and the other should return a CAS mismatch.
Do you see something that deviates from this? If so, maybe you can share the program in a github gist or so and we can spot the problem. CAS is a heavily used feature-- I’ve been using it in the underlying memcached for over a decade-- so I’m pretty sure it’s solid but open to the idea there could be something not yet found!
I have put a sleep before cas based check, I thought that might be the reason for this issue.
I removed the sleep, so now more number of client tries the modify the doc.
Actually I again hit this without sleep.
I will put code snippet and infra details in next comment.
My App has spring based scheduler, which wake ups everyday 9:45AM.
I have deployed my app on 4 servers. (Couchbase Java client :2.4.2 )
I have 3 couchbase node cluster (version : 4.1.0.5005)
Important part - The job should be run by only one server. So, all will wake up at the same time but only one should run the job, others should silently exists.
that is weird - would it be possible for you to reproduce this in a standalone script that we can also try to run? Something might be amiss here, because many customers rely on CAS and we have tests in place that make sure this functionality works.