Home  >  Article  >  Backend Development  >  How PHP prevents high concurrency and repeated requests based on redis distributed locks

How PHP prevents high concurrency and repeated requests based on redis distributed locks

青灯夜游
青灯夜游forward
2019-11-29 15:39:492959browse

How PHP prevents high concurrency and repeated requests based on redis distributed locks

Requirements:

Let’s first give an example of a certain system verification: (A channel system, business B system, external manufacturer C system)

(1) Business system B calls channel system A to verify whether the incoming mobile phone, ID card, and name are consistent.

(2) Channel A system then calls external manufacturer C system.

(3)A channel system returns the results to B business system.

Among these three processes, (2) process, calling external manufacturers requires billing.

When the concurrency of the B business system is very high, there are 100 identical three-factor verifications. Since they are the same three factors, the A channel only needs to call the manufacturer once to know the result. In order to prevent other requests from calling external systems while a certain request has not yet responded, locking is required at this time

Characteristics of distributed locks

●● Atomicity: At the same time, only one thread of one machine can obtain the lock;

● Reentrancy: The same object (such as thread, class) can call the lock repeatedly and recursively without death. Lock;

● Blockable: Before the lock is obtained, the lock can only be blocked and waited until the lock is obtained;

● High availability: Even if a program failure or machine damage occurs, the lock can still be acquired. , is released;

● High performance: The operations of acquiring and releasing locks are low-cost.

To achieve: locking, lock reduction, lock timeout

The implementation method can be: database mc redis system file zookeeper

I am now a channel system, when 100 are the same Business requests are passed in. My first request needs to be locked first, and then I request the external vendor system. After waiting for the response, I insert another key and then delete the lock.

Other requests first obtain the lower lock. If the lock already exists, they will wait in turn. If the lock no longer exists, they will query the results directly.

If the first request fails and the result is not inserted in place, continue to acquire the lock and query the external system.

Get the lock:

$redis->set('lock:手机号&身份证&姓名', 1, ['nx', 'ex'=>10]);

Release the lock:

Just delete the key directly

Lock timeout:

The lock key has timed out Time

The new version of the redis set command can implement distributed locks, and can simultaneously implement set and timeout only if it does not exist.


4b7f0a7304beadcaaaaa0874072c18ecconnect("127.0.0.1",6379);
//高并发时防止重复请求

//渠道系统传递过来的key
$lockKey='lock:18806767777&37781991111629092&taoshihan';
$resultKey='res:18806767777&37781991111629092&taoshihan';

//如果已经查询过值,可以直接返回
$info=$redis->get($resultKey);
if($info){
    exit($info);
}

//如果没有值的,获取锁
$lock=$redis->set($lockKey, 1, ['nx', 'ex'=>10]);
if($lock){
    //请求外部系统获取结果,比如响应结果比较慢
    sleep(8);
    $info='{"name":"taoshihan"}';
    $ret=$redis->set($resultKey,$info);
    if($ret){
        //删除锁
        $redis->del($lockKey);
        exit($info);
    }
}
echo "请稍后重试!";

Recommended learning: PHP tutorial

The above is the detailed content of How PHP prevents high concurrency and repeated requests based on redis distributed locks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete