concurrency control using memcached __memcached

Source: Internet
Author: User
Tags cas memcached

Intro

A discussion of concurrency control using caching allows me to learn the balance between cost and benefit and what is true availability ...

There are several ways to prevent concurrency, and this article only involves using cache memcached control.

Concurrent Scenarios :

use case: A person with senior membership in a SNS system initiates an activity.

Business rules: 1. A person can create only one activity at a time. 2. With a senior membership.

The basic process is as follows:

There are obvious concurrency problems in this process, when process a verifies that member M is qualified and has created an activity, but starts the creation operation, another process B makes a rule judgment, passes through, and completes the creation operation, at which point a continues execution, resulting in two m of activity. (This concurrency scenario is simple and common)

The original solution:

The plan uses the atomicity of the memcached add operation to control concurrency in the following ways:

1. Request Lock: Before verifying whether the activity is created, the Add Operation key is MemberID, and if the add operation fails, there is another process that creates an activity for the MemberID and returns the creation failure. Otherwise indicates no concurrency

2. Perform creation activities

3. Release Lock: After the creation activity completes, performs the delete operation, deletes the MemberID.

question:

There are some problems with this implementation:

1.memcached stored in the value is valid, that is, automatically expire after expiration, such as add after M1, M1 failure, you can add success

2. Even through configuration, you can make memcached permanent effective, that is, not set the expiration date, memcached capacity limit, when the capacity is not enough will be automatically replaced, that is, it is possible to add M1, M1 by other key value replacement, then add can be successful.

3. In addition, Memcached is based on memory, after the loss of all data will be lost, resulting in the restart after all MemberID can be again Add.

Coping Problems:

For the above several problems, the root cause is the add operation is time-sensitive, expired, is replaced, restart, will be the original add operation failure. There are two ways to resolve this problem:

1. Adoption of persistent caching solutions, such as TT (Tokyo tyrant:http://fallabs.com/tokyotyrant/)

2. Mitigate the effects of timeliness by using the memcached CAS (check and set) method.

The first does not need to explain, very simple, all the original problems are timeliness caused misfortune, timeliness originates from Memcached is based on memory, then the use of persistent words stored TT can completely eradicate this problem.

The second way requires a brief introduction:

Memcached In addition to the add operation is atomic, there are two other operations are also atomic: incr and DECR, using CAS mode is:

1. Set a key value in advance in memcached, assuming Creatkey=1

2. Every time you create an activity, get out of the createkey=x before the rule is validated;

3. Make rules Check

4. Perform a incr createkey operation to verify that the return value is the desired x+1 and, if not, that there is another process performing the incr operation in the meantime, that is, concurrency, discarding the update. Otherwise

5. Perform creation activities

Comparing these two methods, it can be seen from the effect that the first 100% reliable, there is no problem, the second, there may be a miscarriage, that is, there is no concurrency, but was sentenced to concurrency, such as a cache restart, or the key value is invalidated, the INCR value may be different from the expected, resulting in a miscarriage

But from the cost considerations, TT is a persistent cache solution, perfect means high cost, we must maintain persistent data, and using memcached CAs, can solve the timeliness problem in almost 0 cost, although there is a little flaw, but this kind of flaw can be solved by simple retry. Considering the actual output ratio, it is more suitable for the actual situation to adopt memcached CAs method.

The balance between cost and income, the difference between doing science and doing engineering ~

 

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.