Redis distributed lock automatic renewal method
Patent Information
- Application Number
- CN202210884359.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-07-25
- Publication Date
- 2026-11-03
- Estimated Expiration
- 2042-07-25
AI Technical Summary
[0003]redis分布式锁满足互斥、防止死锁的特性,但是有个特别大的隐患:无法自动续期,比如一个锁设置了一分钟超时释放,如果拿到这个锁的线程在一分钟内没有将业务执行完毕,那么这个锁就会被其他线程拿到,可能会导致严重的线上问题
[0022]1、实现redis锁自动续期,既能保证业务逻辑顺利执行完毕,又能不影响操作性能。
Smart Images

Figure CN115237612B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of cloud computing technology, and in particular relates to a method for automatic renewal of Redis distributed locks. Background Technology
[0002] Nowadays, most systems are deployed in distributed clusters to achieve high availability. Some resources need to be guaranteed to be unique, which involves the issue of distributed locks. Redis distributed locks are an efficient choice.
[0003] Redis distributed locks offer mutual exclusion and deadlock prevention, but they have a significant drawback: they cannot automatically renew. For example, if a lock is set to release after a one-minute timeout, and the thread that acquired the lock doesn't complete its business logic within that minute, the lock will be acquired by another thread, potentially causing serious online issues. Manually increasing the Redis lock expiration time is an option, but how long should it be? The execution time of business logic is unpredictable, and increasing it too much can negatively impact operational performance. Summary of the Invention
[0004] In view of this, in order to overcome the above-mentioned defects, the present invention aims to propose a method for automatic renewal of Redis distributed locks.
[0005] To achieve the above objectives, the technical solution of the present invention is implemented as follows:
[0006] On one hand, the present invention provides a method for automatic renewal of Redis distributed locks, comprising:
[0007] In response to the system acquiring a Redis distributed lock, create an auto-renewal task corresponding to the acquired Redis distributed lock;
[0008] The scheduler adds the automatic renewal task to the time wheel;
[0009] The scheduler uses the Netty time wheel algorithm to perform asynchronous renewal of the automatic renewal tasks in the time wheel.
[0010] Furthermore, the locking and unlocking methods of the Redis distributed lock include:
[0011] In response to the system's locking command, a Redis distributed lock is added. After successful locking, it is determined whether automatic renewal is required based on the business logic. If renewal is required, a renewal task for the current key is generated, and the task is added to the scheduler. After the business logic is executed and the lock is released, the automatic renewal task is removed.
[0012] Furthermore, methods for removing automatic renewal tasks include removing the key from the renewal MAP collection.
[0013] Furthermore, the scheduler adds the automatically renewed task to the time wheel, including the following steps:
[0014] 1. Create a thread-safe MAP collection object to store all expired keys. If a key is not in the collection, it means that the Redis distributed lock has been released.
[0015] 2. Create a TimerTask renewal task with the key. When the scheduler is initialized, a HashedWheelTimer time wheel executor of the Netty framework has already been initialized. The key parameters are dynamically configured according to business needs and hardware facilities.
[0016] 3. Iterate through the Timer linked list collection in sequence, retrieve the HashedWheelTimer time wheel executor and attempt to add the TimerTask renewal task. If a RejectedExecutionException occurs, it means that the number of tasks that the timer can accept has reached its limit. If the task cannot be added successfully even after reaching the last timer, check if the number of timers in the collection has reached the configured value. If it has, throw an exception indicating that the distributed lock acquisition failed. If it has not reached the configured value, perform real-time expansion, create a new HashedWheelTimer time wheel executor to add the task, and add the HashedWheelTimer time wheel executor to the Timer linked list collection.
[0017] 4. Perform the closing of idle Timers. Traverse the Timer linked list in reverse order. If the number of pending tasks is 0, call the wastedWheelTimer.stop() method to close it. Exclude the first Timer in the set, and always ensure that at least one Timer exists.
[0018] Furthermore, the key parameters include at least one of the following: clock toggle frequency, clock toggle task array size, and maximum holding time.
[0019] In a second aspect, the present invention provides an electronic device / server, including a processor and a memory communicatively connected to the processor and used to store executable instructions of the processor, wherein when the processor executes the instructions, it implements the method for automatic renewal of Redis distributed locks as described in the first aspect.
[0020] Thirdly, the present invention provides a readable storage medium for storing a computer program, which, when executed by a processor, implements the method for automatic renewal of Redis distributed locks as described in the first aspect.
[0021] Compared with existing technologies, the automatic renewal method for Redis distributed locks described in this invention has the following advantages:
[0022] 1. Implement automatic renewal of Redis locks to ensure that business logic is executed smoothly without affecting operational performance.
[0023] 2. The Netty time wheel algorithm framework is used to process renewal tasks asynchronously, resulting in efficient task scheduling.
[0024] 3. The scheduler can dynamically expand the HashedWheelTimer in real time to handle high concurrency and supports disabling the timer during idle periods.
[0025] 4. It can adjust the maximum number of tasks in the timer's maxPendingTimeouts. Each timer has only one worker thread. If there are too many tasks, it will affect the execution of the renewal command. It depends on the business and hardware facilities, making it more flexible. Attached Figure Description
[0026] The accompanying drawings, which form part of this invention, are used to provide a further understanding of the invention. The illustrative embodiments of the invention and their descriptions are used to explain the invention and do not constitute an undue limitation of the invention. In the drawings:
[0027] Figure 1 This is a flowchart of the automatic data binding process described in this invention;
[0028] Figure 2 This is a flowchart illustrating the multi-style data verification process for a single component of the present invention.
[0029] Figure 3 This is a flowchart illustrating the data verification process between components of the present invention. Detailed Implementation
[0030] It should be noted that, unless otherwise specified, the embodiments and features described in the present invention can be combined with each other.
[0031] The present invention will now be described in detail with reference to the accompanying drawings and embodiments.
[0032] This embodiment provides a method for automatic renewal of Redis distributed locks (such as...). Figure 1 As shown, after acquiring the Redis distributed lock, the lock auto-renewal task is added to the time wheel. The scheduler uses the Netty (an asynchronous I / O framework) time wheel algorithm to execute the task at the designated time to complete the asynchronous renewal. The scheduler can also dynamically expand the time wheel, that is, manage several time wheel taskers simultaneously.
[0033] The methods for locking and releasing locks in Redis distributed locks include:
[0034] like Figure 1 As shown, the process of acquiring and releasing locks in a program that requires adding a Redis distributed lock is as follows:
[0035] After successful locking, the system determines whether automatic renewal is needed based on business requirements. If renewal is required, a renewal task for the current key is generated, and the scheduler's method for adding the task is called. Once the business logic is executed after locking, the lock is released, and the automatic renewal task is removed (simply by removing the key from the renewal MAP set). The default expiration time for keys requiring renewal in Redis is 30 seconds.
[0036] like Figure 2 As shown, the process for adding a task to the scheduler is as follows:
[0037] 1. First, a thread-safe MAP collection object is created to store all expired keys. If a key is not in the collection, it means that the distributed lock has been released.
[0038] 2. Create a TimerTask (task object implementation class) renewal task with the key (reset the expiration time by operating the Redis key). When the scheduler is initialized, a Netty framework HashedWheelTimer (implementation class) time wheel executor has already been initialized. Based on business needs and hardware facilities, key parameters such as clock toggling frequency, clock toggling task array size, and maximum holding time are dynamically configured.
[0039] 3. We iterate through the Timer (the interface class of HashedWheelTimer) linked list collection in turn, taking a HashedWheelTimer (the time wheel executor class) and attempting to add the TimerTask (the task object implementation class) task. If a RejectedExecutionException occurs, it means that the number of tasks that the timer can accept has reached its limit. If we fail to add the task even after reaching the last timer, we check whether the number of timers in the collection has reached the configured value. If it has, we throw an exception indicating that the distributed lock acquisition failed. If it has not reached the configured value, we perform real-time expansion, create a new HashedWheelTimer, add the task, and add the HashedWheelTimer to the Timer linked list collection.
[0040] 4. Perform the closing of idle Timers. Iterate backwards through the Timer linked list. If the number of pending tasks is 0, call the wastedWheelTimer.stop() method to close it. Note: Exclude the first Timer in the collection and always ensure that at least one Timer exists.
[0041] For setting the task execution time, we set the renewal time to the expiration time / 3. If the expiration time is 30 seconds, then the key will be renewed for 30 seconds after 10 seconds, until the lock is released.
[0042] like Figure 3 As shown, the renewal task execution method is as follows:
[0043] Netty's efficient time wheel framework internally divides tasks into time slots. When the time comes, the corresponding task in the time slot is retrieved and executed one by one. The execution logic of a single renewed task is shown above. TimerTask (the task object implementation class) is a one-time task and needs to be added again after execution.
[0044] This embodiment provides an electronic device / server, including a processor and a memory communicatively connected to the processor and used to store executable instructions of the processor. When the processor executes the instructions, it implements the method for automatic renewal of Redis distributed locks as described in the above embodiment.
[0045] This embodiment provides a readable storage medium for storing a computer program, which, when executed by a processor, implements the method for automatic renewal of Redis distributed locks as described in the above embodiment.
[0046] Those skilled in the art will recognize that the units and method steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementations should not be considered beyond the scope of this invention.
[0047] In the several embodiments provided in this application, it should be understood that the disclosed methods and systems can be implemented in other ways. For example, the division of units described above is merely a logical functional division, and in actual implementation, there may be other division methods. For instance, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. The aforementioned units may or may not be physically separated. The components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of the embodiments of the present invention according to actual needs.
[0048] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features therein. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention, and they should all be covered within the scope of the claims and specification of the present invention.
[0049] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
Claims
1. A method for automatic renewal of Redis distributed locks, characterized in that: In response to the system acquiring a Redis distributed lock, create an auto-renewal task corresponding to the acquired Redis distributed lock; The scheduler adds the automatic renewal task to the time wheel; The scheduler uses the Netty time wheel algorithm to perform asynchronous renewal of the automatically renewed tasks in the time wheel. The scheduler adds the automatic renewal task to the time wheel, including the following steps:
1. Create a thread-safe MAP collection object to store all expired keys. If a key is not in the collection, it means that the Redis distributed lock has been released.
2. Create a TimerTask renewal task with the key. When the scheduler is initialized, a HashedWheelTimer time wheel executor of the Netty framework has already been initialized. The key parameters are dynamically configured according to business needs and hardware facilities.
3. Iterate through the Timer linked list collection in sequence, retrieve the HashedWheelTimer time wheel executor and attempt to add the TimerTask renewal task. If a RejectedExecutionException occurs, it means that the number of tasks that the timer can accept has reached its limit. If the task cannot be added successfully even after reaching the last timer, check if the number of timers in the collection has reached the configured value. If it has, throw an exception indicating that the distributed lock acquisition failed. If it has not reached the configured value, perform real-time expansion, create a new HashedWheelTimer time wheel executor to add the task, and add the HashedWheelTimer time wheel executor to the Timer linked list collection.
4. Perform the closing of idle Timers. Traverse the Timer linked list in reverse order. If the number of pending tasks is 0, call the wastedWheelTimer.stop() method to close it. Exclude the first Timer in the set, and always ensure that at least one Timer exists.
2. The method for automatic renewal of Redis distributed locks according to claim 1, characterized in that: The locking and unlocking methods of the Redis distributed lock include: In response to the system's locking command, a Redis distributed lock is added. After successful locking, it is determined whether automatic renewal is required based on the business logic. If renewal is required, a renewal task for the current key is generated, and the task is added to the scheduler. After the business logic is executed and the lock is released, the automatic renewal task is removed.
3. The method for automatic renewal of Redis distributed locks according to claim 2, characterized in that: Methods to remove an automatic renewal task include removing the key from the renewal MAP collection.
4. The method for automatic renewal of Redis distributed locks according to claim 1, characterized in that, The key parameters include at least one of the following: clock toggle frequency, clock toggle task array size, and maximum holding time.
5. An electronic device, comprising a processor and a memory communicatively connected to the processor and used for storing processor-executable instructions, characterized in that, When the processor executes the instructions, it implements the method for automatic renewal of Redis distributed locks as described in any one of claims 1-4.
6. A readable storage medium for storing computer programs, characterized in that, When the computer program is executed by the processor, it implements the method for automatic renewal of Redis distributed locks as described in any one of claims 1-4.
