Ehsan Ghanbari

Experience, DotNet, Solutions

Optimistic and pessimistic concurrency control

Optimistic and pessimistic concurrency control AKA optimistic and pessimistic locking is a mechanism to preserve database integrity in multi-user applications. Optimistic and pessimistic are two kinds of locking control. In general concurrency control ensures that correct results for operations are generated. Perhaps you are familiar with locking to serialize the data for accessing shared data; for example when several people in a system are working on the same data, what will happen if they save the changes at the same time?? Imagine this condition about hundreds or thousands of users! I heard about Optimistic and pessimistic locking in "Patterns of Enterprise application" book by Martin Fowler, let's consider them separately.

 

Optimistic Concurrency: it is based on the conflict and transaction restart. Optimistic concurrency looks a little bit optimist when two operations occur simultaneously. for example, when two users updating the same data, updates will be discarded and the user will be informed later and the system assumes that there will be no conflict but it will be checked again to save the future changes on the target data. Optimistic allows transactions to execute without locking any resources and it will just be checked later and  If a conflict will occur, the application must read the data and attempt the changes again.

 

Pessimistic Concurrency: it uses locking as the basic serialization mechanism. it is called "Pessimistic" because you can imagine it in the worst condition. When two or more people are making changes in the same data, the lock will be placed and prevents the possibility of conflicting. Pessimistic concurrency control locks resources as they are required, for the duration of a transaction. In pessimistic approach system assumes that some people will try to make a change in the same data at the same time, so system will not let all of them make change at the same time, it will be done one request after another and no other thread or user can access the item while it is locked.

 

 

More information

  1. Patterns of enterprise application by martin fowler
  2. http://blogs.msdn.com/b/marcelolr/archive/2010/07/16/optimistic-and-pessimistic-concurrency-a-simple-explanation.aspx
  3. http://blog.couchbase.com/optimistic-or-pessimistic-locking-which-one-should-you-pick
  4. http://publib.boulder.ibm.com/infocenter/soliddb/v6r3/index.jsp?topic=/com.ibm.swg.im.soliddb.sql.doc/doc/pessimistic.vs.optimistic.concurrency.control.html

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
Peridic pattern
How to use PagedList In asp.net MVC
Redis as a cache server
Domain driven design VS model driven architecture
How to query over Icollection<> of a type with linq
What's the DDD-lite?
comments powered by Disqus