Does mysql have CAS version control similar to that in memcached?

Source: Internet
Author: User
See php. netmanualzhmemcached. cas. phpmemcached's CAS is like this: first, castoken is actually a version number. 1. When I get a piece of data, a version number ($ casToken) will be returned to me: $ data $ m-& amp; gt; get (& #039; ip_block & #039;, nu... see http://php.net/manual/zh/memcached.cas.php
Memcached's CAS principle is as follows:

First, the cas token is actually a version number,

1. When I retrieve a piece of data, a version number ($ casToken) will be returned to me:$data = $m->get('ip_block', null, $casToken);

2. After I modify $ data and save this $ data, I need this version number to save it back:$m->cas($casToken, 'ip_block', $data);

If $ data is already stored between steps 1 and 2, step 2 will fail because the version $ casToken has changed.

With CAS, you can ensure that the data is up-to-date and avoid repeated problems due to concurrency when competing for limited resources. For example, I have one left for sale, however, three people place orders at the same time.

Is there anything similar to mysql.

// Supplement
A pessimistic lock on a transaction cannot solve this problem, for example:
A. obtain data ~~~~~~~~~~~~~~~~~~~~~~~~~~ B. obtain data.
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
After A operates on data, the transaction stores it with A pessimistic lock ~~~~~~ B System sleep
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
End ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ B. The system continues sleep.
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ After B operates on data, the transaction stores it with A pessimistic lock, and the modification of A is overwritten.

Reply content:

See http://php.net/manual/zh/memcached.cas.php
Memcached's CAS principle is as follows:

First, the cas token is actually a version number,

1. When I retrieve a piece of data, a version number ($ casToken) will be returned to me:$data = $m->get('ip_block', null, $casToken);

2. After I modify $ data and save this $ data, I need this version number to save it back:$m->cas($casToken, 'ip_block', $data);

If $ data is already stored between steps 1 and 2, step 2 will fail because the version $ casToken has changed.

With CAS, you can ensure that the data is up-to-date and avoid repeated problems due to concurrency when competing for limited resources. For example, I have one left for sale, however, three people place orders at the same time.

Is there anything similar to mysql.

// Supplement
A pessimistic lock on a transaction cannot solve this problem, for example:
A. obtain data ~~~~~~~~~~~~~~~~~~~~~~~~~~ B. obtain data.
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
After A operates on data, the transaction stores it with A pessimistic lock ~~~~~~ B System sleep
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
End ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ B. The system continues sleep.
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ After B operates on data, the transaction stores it with A pessimistic lock, and the modification of A is overwritten.

Apply a pessimistic lock to a transaction, that is

Start transactionselect... for update // lock data... update... // update the lock data status commit

The above is the mechanism of mysql itself, and you can also use your own optimistic lock, that is
Adds a version field to the database.

select ...,verison as last_version form table where condition...update table set ...,version=version+1 where condition and version = last_version

For selection, see database concurrency control. Do you choose optimistic or pessimistic locks?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.