Search:

Search all manuals
Search this manual
Manual
Membase Manual 1.7
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
6.2 Best practices
Chapter Sections
Chapters

6.2.3. Locking

Advisory locks can be useful to control access to scarce resources. For example, retrieving information from backend or remote systems might be slow and consume a lot of resources. Instead of letting any client access the backend system and potentially overwhelm the backend system with high concurrent client requests, you could create an advisory lock to allow only one client at a time access the backend.

Advisory locks in Membase or Memcached can be created by setting expiration times on a named data item and by using the 'add' and 'delete' commands to access that named item. The 'add' or 'delete' commands are atomic, so you can be know that only one client will become the advisory lock owner.

The first client that tries to ADD a named lock item (with an expiration timeout) will succeed. Other clients will see error responses to an ADD command on that named lock item, so they can know that some other client is owning the named lock item. When the current lock owner is finished owning the lock, it can send an explicit DELETE command on the named lock item to free the lock.

If the lock owning client crashes, the lock will automatically become available to the next client that polls for the lock (using 'add') after the expiration timeout.