Is it possible to use CB for a distributed lock?

Is it possible to use Couchbase for distributed locking, similar to what you could do e.g. with Redis.

Why? We need a lock / semaphore mechanism (acquire, operate, release operations) and since we already use CB, it would be practical to do it instead of adding another technology for that purpose.

Ping. Anyone? Or is this maybe a topic for #java-sdk?

Yes, you can lock a document when retrieving it. It is called getAndLock. The lock is released automatically when you update the document. This is called pessimistic locking and you can learn more about it at:
https://docs.couchbase.com/java-sdk/2.6/concurrent-mutations-cluster.html#pessimistic-locking

1 Like

Thanks @shivani_g, but I read

A document can be locked for a maximum of 15 seconds, after which the server will unlock it.

Unfortunatelly 15 s is not long enough in our scenario. We have long running processes (background jobs) which may run for many hours.
Such processes would unlock after e.g. x hours.

For misbehaving processes: is it possible or planned to pass a maxLockTimeout to getAndLock()? Or write a manual cleanup background job, which unlocks locks of crashed processes (which would otherwise never be unlocked).

While Couchbase doesn’t directly have this functionality (the existing lock isn’t suitable for this), @btburnett3 has authored a distributed lock in the the .Net Extensions project that looks to be renewable and longer running. I bet he uses it in production as well. While not directly supported, it might be of interest to you and it should be possible to port it.

1 Like

@synesty

@ingenthr is correct, we are using that library in production. And it sounds like your use case is similar to ours, dealing with long running jobs and avoiding conflicts.

The core of the code isn’t very complicated and could be ported pretty easily: https://github.com/couchbaselabs/Couchbase.Extensions/blob/master/src/Couchbase.Extensions.Locks/Internal/CouchbaseMutex.cs

Also, here’s a blog about it: https://btburnett.com/couchbase/2018/11/04/couchbaselocks.html

1 Like

Thanks @btburnett3, I will have a look.