The newest condominiums in Kauai at Kauai Regency Resorts. Located at Po'ipu Kai Resort, you'll have access to world famous Po'ipu Beach. Spacious 1 to 5 bedroom units available with central air (a must have in Kauai). Internet Specials available. Price chart bitcoin dzerzhinskij s p

Bitcoin litecoin namecoin exchange Total Merchant Services offers a comprehensive line of payment solutions for your business credit card, debit card and check acceptance for all types of businesses including retail stores, restaurants, service, and internet businesses. What is bitcoin reddit

Visit PartyPoker.com вЂ“ the world's largest online poker site. Enjoy Poker Games and Tournaments with over 34,000 simultaneous players. Secure and regulated with 24/7 Live Help. Easy download of games. Get $100 Bonus via this special page! Gyft amazon bitcoin

Bitcoin faucet mbtc Order prescription weight loss medicines like phentermine, Xenical, Meridia, Adipex, Bontril and more, right over the internet. Compare prices of online pharmacies that have no consultation fee. Links to brand and generic drugs. Blockchain applications B505 blockchain

Surf the Internet with Netzero's unlimited Platinum Internet access for only $6.95 per month or High Speed accelerated service for $9.95 per month. 2 minute download, no credit card needed. Links to order now online. B505 blockchain nizhnij susalnyi 1per

Gyft amazon bitcoin Make money selling the downloaded "Instant Office Cleaning Kit"! The "Kit" tells how anyone can start their own office cleaning business with little or no money! No investment No experience you can start right away! Bhagwan chowdhry bitcoin

WinFax Pro is the leading fax management software for small businesses. Its high quality faxes help you project a professional image to your clients and customers. Buy and download the latest version Now 20% Off. Bitcoin currency exchange calculator

Blockchain explained pdf Access downloadable PC security and privacy software such as spyware removers, history erasers, adware removers, and antivirus. Get antispam and parental control and many more from PC Download. Bitcoin faucet mbtc

Market cap bitcoin High speed Internet from America's leading DSL providers at new, low rates! Compare the best offers from Verizon, Earthlink and more. Save with free equipment and other great incentives. Bitcoin watch only What makes bitcoin price go up

GoToMyPc is a remote computer access service that lets you access your pc from anywhere. Links to sign up for a free download and trial. Setup takes less than 2 minutes. Sign up now online. Buy tf2 items with bitcoins Sell bitcoins for free

B505 blockchain

Easy to understand plans starting at $19.99 from the Official Site of AT&T Wireless. Great internet specials and instant online rebates. Free FedEx with activation, restrictions apply. Tony gallippi bitcoin Onename blockchain

Download Keno for free and play for fun or money. Extremely fast and user friendly Software from RTG. Chat to other gamers about tips and strategies. Safe secure online gaming at iNetBet Everledger bitcoin Bitcoin like i'm five

Optimistic or pessimistic locking – Which one should you pick?

Assume that Alice and Joe both read the same data item from Couchbase Server, then they both changed the data, and then both tried to write the new versions back to the database. Whose changes should be saved? Alice’s? Joe’s? Neither? A combination?

Developers use locking to serialize access to shared data items. But which locking scheme should you pick for your application – optimistic or pessimistic?

In this blog, I am going to explain the differences between pessimistic and optimistic locking, and discuss about the optimistic and pessimistic locking APIs that you can use in Couchbase Server to control concurrent access to your data.

Optimistic Locking in Couchbase Server

Let’s assume we are building an online wikipedia – like application using Couchbase Server: users can update an article and add newer articles. Let’s assume Alice is using this application to edit an article on ‘bicycles’ to correct some information. Alice opens up the article and makes those changes but before she hits save, she gets distracted and walks away from her desk. In the meantime, let’s assume Joe notices the same error in the bicycle article and wants to correct the mistake.

If optimistic locking is used in the application, Joe can edit the article and save his changes. When Alice returns and wants to save her changes, either Alice or the application will want to handle the latest updates before allowing Alice’s action to change the document. Optimistic locking takes the “optimistic” view that data conflicts due to concurrent edits occur rarely, so it’s more important to allow concurrent edits.

Pessimistic Locking in Couchbase Server

Now let’s assume that your business process requires exclusive access to one or more

documents or a graph of documents. Referring to our previous example, when Alice is editing the document she does not want any other user to edit the same document. If Joe tries to open the page, he will have to wait until Alice has released the lock.

With pessimistic locking, the application will need to explicitly get a lock on the document to guarantee exclusive user access. When the user is done accessing the document, the locks can be removed either manually or using a timeout.

So which one should you pick?

The answer is that, there is no correct answer – it depends. You should pick your locking scheme based on your application requirements.

Unless you expect a document to be heavily contended, optimistic locking is going to be much lower overhead than pessimistic locking – grab the item you need, update it quickly and attemp to apply it. If some other actor in the system beat you to it, you can just retry till you succeed.

With pessimistic locking, you can get exclusive access to a given item – no other thread can access the item while it is locked. You need to be make sure you release the lock during failures. Imagine, I have the cereal object and won’t give it up until I get the milk object. But you have the milk object and won’t give it up until you get the cereal object. Timeouts can be used to possibly break deadlocks or to deal with clients who fail to release the lock.

For simplicity sake, users might pick the same locking strategy across their application. This works well if the access requirements of all the different objects across your application are the same but in reality, this is not the case – different application objects have different access requirements. For example, in the case of social games –

1. Character and player inventory information is heavily accessed during gameplay,

2. Player accounts and preferences are read during player login or at the start of a game

but not frequently updated. Optimistic locking would work well here too.

Obtaining a pessimistic lock in Couchbase Server

Obtaining a lock in Couchbase Server consists of the following steps :

Use the get-and-lock API to retrieve a value for a given key and lock that key

The application now has exclusive control over the document

Obtaining a optimistic lock in Couchbase Server

Obtaining a lock in Couchbase Server consists of the following steps :

Use the check-and-set (CAS) API to retrieve a CAS revision number

Releasing a lock in Couchbase Server

Perform these steps to manually release a lock:

Use CAS to modify the value and release the lock

Behind the scenes of CAS

CAS operation is a simple concept which can be used to build more advanced concurrency control mechanisms. CAS determines if an object has been updated by another client between the time it was initially read and the time the save was attempted. If the object is modified by another client, an error is raised and the application has to re-read the value and retry the operation. If objects are not in high contention, the transformation of the data is idempotent and retrying the operation is easily accomplished without too much wasted work, optimistic locking is a good solution that provides high performance in the average case.

A common way of interacting with CAS involves a CAS loop as shown in the community developed go memcached client.

For example: Imagine there are 3 clients as shown in the interaction diagram below. All of the three clients attempt to update object X using CAS – each client succeeds, one at a time.

You didn’t release the lock, let Couchbase do it for you

When you perform a get-and-lock operation you provide an expiration for the lock as a parameter. The default amount of time a key can be locked is 15 seconds. After 15 seconds, if you don’t release the lock manually, Couchbase releases it automatically.

Final Thoughts

Optimistic locking might not be the best solution for every situation. While some use-cases work well with optimistic locking, others might need stricter schemes like pessimistic locking.

Locking might not be good for all cases – your application can have a problem if there is a lock contention. A thread can hold a lock and be de-scheduled by the OS. Then all the threads that want to acquire this lock will be blocked. One option is to avoid locking altogether where possible by using atomic operations. These API’s can be very helpful on heavily contested data.

However, if you really need locking and feel that optimistic locking covers most of your applications’ use-cases, optimistic locking using CAS in Couchbase Server is so easy to implement there’s no reason you shouldn’t try.

Posted by Don Pinto, Principal Product Manager, Couchbase

Don Pinto is a Principal Product Manager at Couchbase and is currently focused on advancing the capabilities of Couchbase Server. He is extremely passionate about data technology, and in the past has authored several articles on Couchbase Server including technical blogs and white papers. Prior to joining Couchbase, Don spent several years at IBM where he maintained the role of software developer in the DB2 information management group and most recently as a program manager on the SQL Server team at Microsoft. Don holds a master's degree in computer science and a bachelor's in computer engineering from the University of Toronto, Canada.

6 Comments

Lock picking is an art oflegally, and with the permission of the user, unlocking a lock by analyzing itscomponents. Though lock picking is mostly associated with crime and theft, itis an essential skill for a locksmith.

I discovered the issue above is caused by the fact that I\’m using \’0l\’ as the cas value. It turns out that if you use \’0\’ as the case value then cas is not enabled (by design). Once I changed the initial cas value from \’0\’ to \’1\’ it worked.