CAS in distributed programming

CAS in distributed programming

  Recently we discovered the problem two probabilistic data is overwritten in the project, after tracking the reasons are due to the concurrent discovery caused. When comparing data solutions are updated data is changed, if not changed, then the data is updated. This approach is CAS (Compare And Set), below is a CAS Ideological applications.

  Before turning to CAS cite a precedent above mentioned problem, the problem is as follows: low probability of human translation machine translation data is covered phenomenon occurs. Human translation data in a higher priority than the machine translation business scenarios, why human translation machine translation data will still be covered by it?

 

  In a multi-user environment, there will be two typical concurrency problems: lost updates, dirty read . The above problem is the lost update problem.

  There are two concurrency control mechanisms:

  Pessimistic locking: assume concurrency violation occurs, the shield may be in violation of all operational data integrity.

  Optimistic locking: assume concurrency conflicts do not occur, only checks data integrity violation when a commit operation.

 

  CAS is optimistic locking an application scenario, the principle is simple: submit updated data when the data before formal conflict or not to detect a conflict if found, then let users return an error message, allowing users decide what to do .

  Translation of the above scene data is overwritten human translation machines, because the machine translation to translate the read data A, not yet been translated to be manually modified as B, when the data is updated again after the machine translation for A. So long as update data in machine translation finished, judge whether the data is A. If the data is updated to A, if A is not, no data is updated. This problem can be solved this probability produced.

  In fact, CAS is a very simple idea, it is also very easy to understand, the key lies in the idea of using CAS in a very large complex and disruptive application scenarios .

 

  Pessimistic lock implementation is to use the database locking mechanism, for example: select * from goods where goods_code = '111' for update;

Guess you like

Origin www.cnblogs.com/wuchangliang/p/11102920.html