Home > Backend Development > PHP Tutorial > 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

青灯夜游
Release: 2023-04-07 21:02:01
forward
3083 people have browsed it

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]);
Copy after login

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.


<?php
$redis=new Redis();
$redis->connect("127.0.0.1",6379);
//高并发时防止重复请求

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

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

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

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template