Optimistic vs Multi Version Concurrency Control - Differences?

前端 未结 3 926
感动是毒
感动是毒 2021-01-31 09:49

I am trying to find out, what the difference between optimistic concurrency control (OCC) and multi version concurrency control (MVCC) is?

So far I know that both is bas

相关标签:
3条回答
  • 2021-01-31 10:25

    I think they are sometimes used interchangeably, and if the transaction only involves one object then they are essentially the same, but MVCC is an extension of optimistic concurrency (or a version of it) that provides guarantees when more than one object is involved. Say that you have two objects, A and B, which must maintain some invariant between them, e.g. they are two numbers whose sum is constant. Now, a transaction T1 subtracts 10 from A and adds it to B, while, concurrently, another transaction T2 is reading the two numbers. Even if you optimistically update A and B independently (CAS them), T2 could get an inconsistent view of the two numbers (say, if it reads A before it's modified but reads B after it's been modified). MVCC would ensure T2 reads a consistent view of A and B by possibly returning their old values, i.e., it must save the old versions.

    To sum up, optimistic locking (or optimistic concurrency control), is a general principle for synchronization w/o locks. MVCC is an optimistic technique which allows isolated transactions which span multiple objects.

    0 讨论(0)
  • 2021-01-31 10:38

    Just to rectify Dimos's answer: timestamp based concurrency control is still a pessimistic method (it may still abort/block transactions during their execution phase).

    0 讨论(0)
  • 2021-01-31 10:50

    To directly reply to the question, multi version concurrency control (MVCC) is a concurrency control method, (typically) belonging in the category of optimistic concurrency control (OCC).

    There are 2 main concurrency control approaches:

    • Pessimistic Concurrency Control: this approach assumes that conflicting operations happen more frequently (that's why it's called pessimistic). Since the conflicts are common, this approach makes use of locks to prevent conflicting operations from executing, assuming that there is no significant overhead from their usage.
    • Optimistic Concurrency Control: this approach assumes that conflicting operations are rare and they do not happen so frequently. Under this assumptions, the locks would impose significant & not needed overhead to the performance. For this reason, this approach generally avoids locking and attempts to execute the operations, checking (at the commit of each transaction), whether there has been a conflict with another transaction during its operations. If there was any conflict, this approach proceeds with aborting the transactions that had conflicting operations.

    One widely known algorithm of pessimistic concurrency control is the 2-phase locking.

    Two widely known algorithms of optimistic concurrency control are:

    • The timestamp-based concurrency control
    • The multi-version concurrency control

    The main difference between these 2 algorithms is the following. The timestamp-based algorithm assigns a single (more correctly one for each kind of operation, read & write) timestamp to each object, denoting the last transaction that accessed it. So, each transaction checks during the operation, if it conflicts with the last transaction that accessed the object. The multi-version approach maintains multiple versions of each object, each one corresponding to a transaction. As a result, the multi-version approach manages to have fewer aborts than the first approach, since a potentially conflicting transaction can write a new version, instead of aborting in some cases. However, this is achieved at the cost of more storage required for all the versions.

    Strictly speaking, MVCC is concerned mostly with how data are stored, i.e. the fact that there can be multiple physical versions for each data item. As a result, it is theoretically possible to combine it with pessimistic methods as well(e.g. locking), but its multi-version nature is best combined with optimistic methods.

    0 讨论(0)
提交回复
热议问题