Accessing keyvalue from multiple threads

Dear Support,

My application (using C library) will have multiple threads and each thread will access same bucket (eg: bindCounter) and get the current bindCounter(key value) and then update bindCounter++ (key value). There are some chances to access multiple thread at same time to get the current bindCounter(key value).

Question :
If thread 1 is trying to update the bindCounter(key value) but the same time thread 2 is also trying to get the value. Does thread 2 will get updated bindCounter(key value).

Kindly advise.


All of Couchbase server’s operations are atomic, so in your example thread 2 will either see the complete ‘old’ document or the complete ‘new’ document - no intermediate state is exposed. If you want to synchronise updates you may want to look at the CAS operation - see <a href=">Retreiving items with CAS values in the Couchbase dev guide.

do your threads share the same connection handle?


Each thread will have its own connection.



Can anybody help on this?


If I understand your question correctly, then whether thread 2 sees the update is indeterminate - you have a classic thread race condition.

All Couchbase operations are atomic at the document level. So thread 2 will either see the complete ‘old’ document, or the complete ‘new’ document, it won’t observe an inconsistent state. See Reading / Writing Data in the dev guide for more information on this.

Assuming you want to do something like read a value, update some field and then write it back without another entity updating it, I suggest you look at the CAS (check-and-set) operation - see Retrieving Items with CAS values.