Home >Database >Redis >How to use Redis locks to deal with concurrency issues

How to use Redis locks to deal with concurrency issues

尚
forward
2020-05-07 09:09:546032browse

How to use Redis locks to deal with concurrency issues

Use Redis lock to handle concurrency issues to ensure that only one instance of multiple processes is running. When the running instance goes down, one of the other instances can come up, ensuring that there is and only There is an instance running.

import redis
r = redis.Redis(...)

last_heart = 0		# 记录上一次得到的锁心跳
free_lock_try = 6	# 锁无心跳的最大次数 

while not r.setnx('mylock', 1):
    now_heart = r.get('mylock')
    print(f"没获取到锁,now_heart={now_heart},last_heart={last_heart},free_lock_try={free_lock_try}")
    if now_heart == last_heart:
        free_lock_try = free_lock_try - 1
        if free_lock_try == 0:	# 锁已经1分钟没有心跳了
            old_heart = r.getset('mylock', 1)	# 将lock重置为1,并返回set之前的心跳值
            if old_heart < now_heart:
                time.sleep(10)
                continue
            else:
                break	# 成功获取到锁,退出循环
    else:
        free_lock_try = 6	# 锁有心跳,重置free_lock_try值
        last_heart = now_heart
    time.sleep(10)

def producer_exit():
    """程序正常退出时候自动清理锁"""
    r.delete(&#39;mylock&#39;)
import atexit
atexit.register(producer_exit)

# 业务代码
while True:
  r.incr(&#39;mylock&#39;)	# 让锁心跳加一
  ...

Let’s take a look at what problems this program solves in concurrent locks:

1. Under high concurrency, multiple processes cannot obtain locks at the same time. Redis.setnx is used here. If the lock already exists, other processes cannot reset the lock and obtain the lock. In addition, when multiple processes find that there is a lock and no heartbeat at the same time, they use redis.getset to reset the heartbeat to 1. The set can be successful, but the value obtained by multiple processes is different. Only the lock is actually acquired. The process returns the heartbeat of the previous process, while other processes get 1.

2. The process with the lock exits normally. You can use atexit to register the process exit function to delete the lock. You don’t need to do it here, but you will have to wait for the new process to wait for several heartbeats when starting next time.

3. The locked process exits unexpectedly, and the heartbeat will no longer increase after exiting. After the number of free_lock_try is exceeded, other processes will reset and acquire the lock

4. All processes exit unexpectedly. This problem is not of concern to locks. You can use Supervisor performs the daemon process.

For more redis knowledge, please pay attention to the redis introductory tutorial column.

The above is the detailed content of How to use Redis locks to deal with concurrency issues. For more information, please follow other related articles on the PHP Chinese website!

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