Atomic probabilistic counting and set membership


I am looking to do probabilistic counting and set membership using structures such as bloom filters and hyperloglog. I know I can store such structures in Couchbase as binary, but I don’t want to use optimistic locking because contention is high.

Is there any support for performing such operations atomically on the server-side, through user-defined functions or similar? Or any way for me to add extensions with such functionality?

(I could ingest the data through another system and batch the updates to reduce the contention, but it would be far simpler if all this could be handled in the database server.)


Take a look at the Compare-and-Swap (CAS) command - this allows you to perform “optimistic” locking where an update will only occur if the base value hasn’t changed since that client last read it.

See the developer guide:


Thanks for the response. However, I don’t want to use optimistic locking because contention is high; the base value will frequently have changed since the last client read it.


The only operations the server supports (all atomically) are:

  1. Set / Create / Update /Delete (of the whole document).
  2. Increment/ Decrement of a 64bit counter document
  3. Binary append / prepend.

I don’t know exactly what you are trying to model but certainly Counters can be used for atomic incr/decr. You can also use part of the keyspace for sets, where you use presence / absence of a key to indicate set membership existence.


Thanks, it’s clear Couchbase doesn’t natively support the feature I’m looking for.