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 a 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 operation occur simultaneously. for example when two user updating the same data, updates will be discarded and user will be informed later and 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 checked later and If a conflict will be occurred, 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 change in a same data at the same time, so system will not let all of them to 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.
- Patterns of enterprise application by martin fowler