[Distributed Learning] idempotent and achieve

Reference links

firstdream

CHEN river

concurrency

mvvc

innodb-locking-reads

introduction

What is idempotent


Excerpt from Baidu Encyclopedia.

Idempotent (idempotent, idempotence) is a mathematics and computer science concepts, common in abstract algebra.

In a programming operation is idempotent characteristics is performed any number of times which are the impact and influence of the first performance of the same. Idempotent other functions, methods, or power means can be repeatedly performed using the same parameters, and can achieve the same results function. These functions will not affect the system status, do not worry Repeat will cause the system to change. For example, "setTrue ()" function is a function of power and so on, whether executed multiple times, the result is the same. More complex operations such as power guarantee is the use of a unique transaction number (serial number) to achieve.


Idempotent action

Whether stand-alone or distributed system will encounter concurrency issues, in addition to using a similar method of flow control, etc., when the request reaches the real back-end server, 幂等设计it can be used as a solution to the.

Message Queuing distributed architecture based systems, for various reasons, such as network fluctuations, which queues the message will be retransmitted, in essence, the system automatically generates a concurrent requests. It simply can be understood as a webapi interface is invoked multiple times exactly "the same" argument, this time idempotent come in useful.

Read some articles, summarize the basic means.

specific method

Overall, power and other key implementation is actually anti-concurrency problems. As long as the system to prevent concurrency problems, and then returns the corresponding results of the fine according to the unique natural key.

lock stand-alone systems ()

.net the lock or the java ReentrantLock and other high-level languages have a keyword, the basic usage is to construct an object, for critical steps, lock the object, and then execute the code of business operation is completed and then release.

De-duplication table

After we finished performing all database operations to a redundant insert a table updating records, according to the database unique primary key, if the data already exists it will error, then rollback.

Multiversion concurrency control

Is literally a multi-version concurrency control. It is based 数据库concurrency control. We can make critical data table one more version field. First get a record value of $ oldVersion, then add conditions to update when version = $ oldVersion. It is described by sql

select id, value1, version from table1 where id = $id; --version为$oldVersion
update table1 set value1=$newValue where id = $id and version = $oldVersion; -- 比原先id=$id 多一个version=$oldVersion条件。

In this way, the code will be able to judge by the number of rows affected is 1 to determine whether the update was successful.

This is a kind of optimistic locking implementation. So, generally with some retry mechanism to optimize the user experience. Concurrency Control EFCore is also based on this implementation.

select for update pessimistic locking

Put simply lock records, update records to prevent other processes, or read the record (need to set the isolation level).

The next summary details such as a mysql database lock relevant content to say.

First check after change

Let's check before modifying the "record" exists, and if so, direct return, only to perform the operation. This record may be inside the database, a large quantity of concurrent time can not be used, because there will be two processes are read into the situation does not exist. This time it needs to achieve by redis such as caching. Because redis itself is single-threaded, all can guarantee the same time only one process to get.

Guess you like

Origin www.cnblogs.com/sheldon-lou/p/10973148.html