This article is a mirror article of machine translation, please click here to jump to the original article.

View: 50989|Reply: 2

[Source] .NET Core implements distributed locks based on Redis Lua [with source code]

[Copy link]
Posted on 2021-5-9 21:11:18 | | | |
Application scenarios of distributed locks: With the development of services, single applications become clusters, distributed locks solve the operation of different processes (threads) on the same resource in concurrent situations, and some services must require a single thread to operate, for example: changes in deposit amounts, changes in commodity inventory, etc.

Review:

.NET Core implements distributed lock principle parsing based on Redis
https://www.itsvse.com/thread-9391-1-1.html

.net/c# Zookeeper Distributed Lock Implementation [Source Code]
https://www.itsvse.com/thread-4651-1-1.html

Redis uses Lua scripts for detailed explanations
https://www.itsvse.com/thread-9634-1-1.html

[Actual combat] ASP.NET Core is based on Redis distributed lock flash sale
https://www.itsvse.com/thread-9397-1-1.html

In the previous article "[Practice]ASP.NET Core Based on Redis Distributed Lock Flash Kill", when we added a lock, we also enabled a background thread to extend the lock time, so as to ensure that when the lock is released, the lock occupied by other threads will not be mistakenly deleted.

After learning Redis to use Lua scripts, you can easily use scripts to complete occupancy locks and release locks.


First, let's take a look at the renderings as follows:



We first opened 1000 parallel threads to compete for the lock, and the lock expired for 5 seconds.We completed 1000 threads in 5 seconds, and only 1 thread successfully acquired the lockAfter waiting for 6 seconds, the lock will be automatically released, and you can see that the second task has successfully obtained the lock, and immediately released the lock, and the subsequent code has successfully obtained the lock.

The lua script to get the lock is as follows:

The lua script to release the lock is as follows:

Interface code:

Source code download:The hyperlink login is visible.

Modify "[Practice] ASP.NET Core based on Redis distributed lock flash sale" is implemented using this article, and our website simulates it10000 items, and then wrote a console to simulate the HTTP request timeThe sold-out is completed in 6 seconds, as shown in the figure below:

[Actual combat] ASP.NET Core is based on Redis distributed lock flash sale
https://www.itsvse.com/thread-9397-1-1.html



Modify the test simulation request code:



(End)




Previous:Kafka manually sets the offset offset
Next:ASP.NET Core gets the request full URL link
Posted on 2021-11-4 13:50:43 |
{:1_1:} {:1_1:} {:1_1:} {:1_1:} {:1_1:} {:1_1:} {:1_1:} {:1_1:} {:1_1:} {:1_1:} {:1_1:} {:1_1:} {:1_1:} {:1_1:} {:1_1:} {:1_1:}
Posted on 2022-6-11 13:36:33 |
Thank you, thank you
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com