Get the App
SLTechnology News&Howtos  ›  Database  › 

How Redis locks deal with concurrency problems

Shulou Source: shulou.com Published: 2022-06-01 19:50:41 10月03日 Update

This article is to share with you how to use Redis locks to deal with concurrency problems. In order to facilitate you to learn, the detailed code has been placed in the article, interested partners to do it together.

Use Redis locks to deal with concurrency problems to ensure that only one instance of a multi-process is running. When the running instance down, one of the other instances can be topped up to ensure that there is and only one instance is running.

Import redisr = redis.Redis (...) last_heart= 0 # record the last lock heartbeat free_lock_try = 6 # maximum number of lock heartbeats while not r.setnx ('mylock', 1): now_heart= r.get (' mylock') print (f "no lock, 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: # the lock has lost its heartbeat for 1 minute old_heart = r.getset ('mylock', 1) # resets lock to 1 And return the heartbeat value before set if old_heart < now_heart: time.sleep (10) continue else: break # successfully acquire the lock and exit the loop else: free_lock_try = 6 # the lock has a heartbeat Reset free_lock_ try value last_heart = now_heart time.sleep (10) def producer_exit (): "r.delete ('mylock') import atexitatexit.register (producer_exit) # Business Code while True: r.incr (' mylock') # add one to the lock heartbeat when the program exits normally.

Let's take a look at what problems in concurrent locks are solved by this program:

1. Under high concurrency, multiple processes cannot acquire locks at the same time. Redis.setnx is used here, and if the lock already exists, no other process can reset the lock and acquire it. In addition, when multiple processes find that there is no heartbeat at the same time, use redis.getset to reset the heartbeat to 1, all can set successfully, but the value from get is different for multiple processes. Only the process that really acquires the lock returns the heartbeat of the previous process, while other processes get 1.

2. The locked process exits normally. You can use the atexit registration process exit function to delete the lock, or you don't need it here, but the next time you start, you have to wait for the new process to wait for several heartbeats.

3. The locked process exits unexpectedly, and the heartbeat does not increase after the exit. 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 a concern of locks. You can use supervisor as a daemon.

On the Redis lock how to deal with concurrency problems to share here, of course, not only the above and everyone's analysis methods, but the editor can ensure its accuracy is absolutely no problem. I hope that the above content can have a certain reference value for everyone, and can be put into practice. If you like this article, you might as well share it for more people to see.

Tags: Process problem instance multiple guarantee run accident success code method at the same time number of times program maximum learning for application business value all interest content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple Shulou Technology Docker MariaDB