Search CTRL + K

Optmistic Concurrency Control

乐观并发控制(Optimistic Concurrency Control, OCC) 指假设并发得 事务 在处理时彼此不会相互影响,也就是说 事务 在处理时无需考虑任何并发问题,但在提交数据更新之前,每个 事务 会检查在该 事务 读取数据后是否有其他 事务 又修改数据。如果有,则回滚。是 并发控制 的一种方式。

乐观并发控制 适用于数据竞争不大、冲突较少的场景,这种场景下偶尔回滚 事务 的成本远低于读取数据时锁定数据的成本,因此吞吐量比 悲观并发控制 更高。

阶段

  1. 读取。事务 读取数据,系统分配时间戳
  2. 校验。事务 执行完毕,进行提交。此时校验所有 事务,如果读取的数据之后被修改,则产生冲突,回滚。
  3. 写入。

Wikipedia

Optimistic - Delay the checking of whether a transaction meets the isolation and other integrity rules (e.g., serializability and recoverability) until its end, without blocking any of its (read, write) operations ("...and be optimistic about the rules being met..."), and then abort a transaction to prevent the violation, if the desired rules are to be violated upon its commit. An aborted transaction is immediately restarted and re-executed, which incurs an obvious overhead (versus executing it to the end only once). If not too many transactions are aborted, then being optimistic is usually a good strategy.[1]


  1. https://en.wikipedia.org/wiki/Concurrency_control ↩︎