Skip to content

Optimistic and pessimistic

一、tradeoff

1、需要根据data contention的频率、量来选择到底是使用optimistic还是pessimistic

二、之前介绍的各种concurrency control technique,都是可以根据optimistic and pessimistic进行分类的

入门文章

zhihu 【BAT面试题系列】面试官:你了解乐观锁和悲观锁吗?

参见 zhihu-你了解乐观锁和悲观锁吗 章节。

stackoverflow Optimistic vs. Pessimistic locking

NOTE: 各个回答是从DB的角度来谈的

wikipedia Optimistic concurrency control

Optimistic concurrency control (OCC) is a concurrency control method applied to transactional systems such as relational database management systems and software transactional memory. OCC assumes that multiple transactions can frequently complete without interfering(干涉) with each other. While running, transactions use data resources without acquiring locks on those resources. Before committing, each transaction verifies that no other transaction has modified the data it has read. If the check reveals conflicting modifications, the committing transaction rolls back and can be restarted.[1] Optimistic concurrency control was first proposed by H. T. Kung and John T. Robinson.[2]

NOTE:

1、通过上面描述的策略可以看出,显然相比于lock,optimistic concurrency control允许更大的concurrency。

2、以transaction的方式来理解optimistic concurrency control,这能够帮助我们的理解

OCC is generally used in environments with low data contention(数据竞争). When conflicts are rare, transactions can complete without the expense of managing locks and without having transactions wait for other transactions' locks to clear, leading to higher throughput than other concurrency control methods. However, if contention for data resources is frequent, the cost of repeatedly restarting transactions hurts performance significantly; it is commonly thought[who?] that other concurrency control methods have better performance under these conditions.[citation needed] However, locking-based ("pessimistic") methods also can deliver poor performance because locking can drastically limit effective concurrency even when deadlocks are avoided.

NOTE: lock-based method是pessimistic的。

Examples

NOTE: 下来列举了一些我知道的

1、Redis provides OCC through WATCH command.[16]

2、MySQL implements OCC in Group Replication configuration.[citation needed]

wikipedia Transactional memory#Motivation

In concurrent programming, synchronization is required when parallel threads attempt to access a shared resource. Low level thread synchronization constructs such as locks are pessimistic and prohibit threads that are outside a critical section from making any changes. The process of applying and releasing locks often functions as additional overhead in workloads with little conflict among threads. Transactional memory provides optimistic concurrency control by allowing threads to run in parallel with minimal interference.[2] The goal of transactional memory systems is to transparently support regions of code marked as transactions by enforcing atomicity, consistency and isolation.

Optimistic concurrency control的实现方式

主要参考:

1、zhuanlan 【BAT面试题系列】面试官:你了解乐观锁和悲观锁吗?

下面是我的总结:

两种实现方式都可以从transaction的角度来进行思考:

CAS 通过 value comparison 来判断 "状态 是否发生了改变"、是否有其他transaction发生,如果没有改变、没有其他transaction,则commit;否则rollback。

MVCC 通过 version ID 来判断 "状态 是否发生了改变"、是否有其他transaction,如果没有改变、没有其他transaction,则commit;否则rollback。

Optimistic and pessimistic synchronization in distributed computing

https://link.springer.com/chapter/10.1007/BFb0024159

See also

redis Transactions

wikipedia Optimistic replication

TODO

https://en.wikipedia.org/wiki/Server_Message_Block#Opportunistic_locking

https://docs.jboss.org/jbossas/docs/Server_Configuration_Guide/4/html/TransactionJTA_Overview-Pessimistic_and_optimistic_locking.html

dennyzhang Explain: Pessimistic And Optimistic Locking