Locking and Unlocking a document

I want to implement locking in my application with below approach:
To get lock: Agent will call my custom API which would call couchbase server’s getAndLock API to get a lock. If it gets lock successfully, it would return CAS value otherwise would return -1. Returned CAS value would be passed to agent.
To release lock:Updated document with CAS value would be synced through Sync Gateway which would release lock if CAS value matches with that in server.

My question is where in document should I set CAS value?
Will it work through sync gateway(save doc and release lock) or should I call couchbase server API to save and release lock?

I think I’d need more specifics to understand exactly what you’re trying to accomplish with the locking, but there are a couple of problems with the approach. In particular, there’s no functionality during a Sync Gateway write that would allow you to do a CAS check against the Couchbase Server CAS (since Sync Gateway is designed for MVCC and not CAS-based concurrency).

An alternative might be to set a ‘lockedBy’ property in the document to a username (via your custom API), and then use the Sync Function to restrict write access based on the lockedBy property of oldDoc.

@vineet.kumar ,

Are you doing the Couchbase Server SDK to do CRUD operation on the data.
are you doing CRUD using HTTP REST through Sync Gateway Data port :4984/{db}/