当前位置:网站首页>Redis (II) distributed locks and redis cluster construction
Redis (II) distributed locks and redis cluster construction
2022-06-25 10:05:00 【@@Mr.Fu】
List of articles
One 、 Thread lock and distributed lock
Thread lock Single project
- Single project
step
- The code is as follows
// Define static global locks private readonly static object _lock = new object(); // Add code to the controller lock (_lock) { Stock sto = new Stock(); sto = demoDbContext.stock.Where(p => p.ID == 1).FirstOrDefault(); if (sto.count == 0) { Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "--- End of seckill , No stock "); return Ok(" End of seckill , No stock "); } Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "-- Seckill success ;"); // Inventory decrease 1 sto.count = sto.count - 1; demoDbContext.SaveChanges(); } return Ok(" End of seckill ");
- The code is as follows
The number of databases is 10
Pictured :
use jmeter Concurrent 10 Threads
Pictured :
The operation results are as follows :

- Single project
Distributed lock
Conditions
- Start two instances 5000/5001
- Nginx
- jmeter
- redis
step
Core code
public class RedisLock { public readonly ConnectionMultiplexer connectionMultiplexer; private IDatabase database = null; public RedisLock() { connectionMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6380"); database = connectionMultiplexer.GetDatabase(0); } /// <summary> /// Lock /// </summary> public void Lock() { while (true) // { //redis_lock Lock name // Thread.CurrentThread.ManagedThreadId Thread name // TimeSpan.FromSeconds(10) Set expiration time Prevent deadlock bool flag = database.LockTake("redis_lock", Thread.CurrentThread.ManagedThreadId, TimeSpan.FromSeconds(10)); //true : Locking success false: Locking failed if (flag) { break; } Thread.Sleep(10);// Prevent deadlock Waiting time Release resources . } } /// <summary> /// Release the lock /// </summary> public void UnLock() { database.LockRelease("redis_lock", Thread.CurrentThread.ManagedThreadId); connectionMultiplexer.Close(); } }Used in the controller
[HttpGet] [Route("SubStock")] public IActionResult SubStock() { RedisLock redisLock = new RedisLock(); redisLock.Lock(); Stock sto = new Stock(); sto = demoDbContext.stock.Where(p => p.ID == 1).FirstOrDefault(); if (sto.count == 0) { Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "--- End of seckill , No stock "); //redisLock.UnLock(); return Ok(" End of seckill , No stock "); } Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "-- Seckill success ;"); // Inventory decrease 1 sto.count = sto.count - 1; demoDbContext.SaveChanges(); redisLock.UnLock(); return Ok(" End of seckill "); }Run two instances
Pictured :

start-up Nginx
Pictured :
Database inventory
Pictured :
jmeter Concurrent 10 Threads

The operation results are as follows

The usage scenarios of distributed lock
When modifying a field value in the cluster system, a distributed lock is used .The design idea of distributed lock
For example, two processes are concurrent , When the first process is locked , The second process will fail to lock , Will sleep (10 millisecond ), Until the first process executes the business code and releases the lock , If the first process processes business code more than 10 millisecond ,redis The expiration time of is also 10 millisecond , Then the second process locks, executes the business code and releases the lock .
remarks : The number of milliseconds to sleep can be defined according to your own business code , The number of milliseconds should be the same as redis The expiration time is the same .
Two 、Redis colony
- First generation cluster Master-slave cluster
Pictured :

shortcoming
only one master, When maset After downtime , Whole redis The clustered system cannot be used .
- Second generation clusters The sentry cluster
Pictured

The second generation cluster is one more than the first generation cluster sentinel The role of monitoring , When the primary node goes down ,sentinel A master node will be selected from multiple slave nodes .
shortcoming
- only one master, Can't solve the problem of high concurrent write .
- Can't store massive data .
- Third generation cluster
Pictured :

Advantages and disadvantages
- advantage
- Solve the problem of high concurrent write .
- Storing massive amounts of data .
- shortcoming
- It consumes a lot of resources .
- advantage
Realization
- Conditions
- windows Environmental Science
- Redis
- Download address of online disk
link :https://pan.baidu.com/s/1-rdemcSLHHFSy3b03EnQsg
Extraction code :liiz
- Download address of online disk
- Ruby
- Download address of online disk
link :https://pan.baidu.com/s/1NEnVoZzzMyROdm0qNeqw0g
Extraction code :lf10
- Download address of online disk
- Ruby drive
- Download address of online disk
link :https://pan.baidu.com/s/1LkpTstTMenespCx4J0ZtWA
Extraction code :7wn6
- Download address of online disk
- Assign master-slave tools
- Download address of online disk
link :https://pan.baidu.com/s/18ah0ePiGr9XCukRsRdIiXw
Extraction code :0e02
- Download address of online disk
- Conditions
step
Configure the cluster file (6 An example ) To configure 6 Configuration files 【 And will 6 Copy configuration to redis The root directory 】 The configuration cannot have Chinese comments !!!!
port 6380 # port bind 127.0.0.1 #IP Address appendonly yes # The data storage format is aof appendfilename "appendonly.6380.aof" # Data saving file cluster-enabled yes # Do you want to open the cluster cluster-config-file nodes.6380.conf # Cluster node configuration file cluster-node-timeout 15000 # Node timeout cluster-slave-validity-factor 10 # verification slaver Number of nodes cluster-migration-barrier 1 # cluster-require-full-coverage yes #master Nodes and slaver Whether the nodes are fully replicatedExecute all instances
redis-server.exe redis.6380.conf redis-server.exe redis.6381.conf redis-server.exe redis.6382.conf redis-server.exe redis.6383.conf redis-server.exe redis.6384.conf redis-server.exe redis.6385.confPictured :






install ruby
ruby --version # Verify that the installation was successfulredis-cluster Drive installation command
# Get into ruby The installation directory bin Execute the installation command under the file ruby gem install --local D:\Assembly\redis\Windows\redis-cluster\redis-3.2.2.gem # Drive file pathExecute the script for assigning master-slave tools
ruby redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385 # Write all instance addresses and port numbers # --replicas 1: Whether to allocate 3 Lord 3 from A master node and a slave nodePictured :

Check whether the assignment is successful
When 6 An instance keeps outputting logs , Indicates that the assignment has been successful .
redis Cluster internal relationship structure diagram
Pictured :
stay redis In the cluster , Each node communicates with each other , The protocol used is Gossip agreement .
redis The principle of data storage in the cluster
- Slot Slot The primary node has slots [ Average distribution ] Slave node has no slot All in all 16384 Slot
ruby redis-trib.rb check 127.0.0.1:6380 - Hash Algorithm
- Modular algorithm
When the client writes data to the node , The node will key Use hash The algorithm takes a fixed length value , Then the total number of slots 【16384】 Take the module with the module taking algorithm , After the value is obtained, go to each master node to check the value between the number of slots of which node , When writing data to that master node .
- Slot Slot The primary node has slots [ Average distribution ] Slave node has no slot All in all 16384 Slot
边栏推荐
- Get started quickly with jetpack compose Technology
- clang frontend command failed with exit code 250
- 测试开发工程师
- Is GF Securities reliable? Is it legal? Is it safe to open a stock account?
- ShardingSphere-Proxy 4.1 分库分表
- SparseArray details
- Kotlin keyword and operator
- CYCA 2022少儿形体礼仪初级师资班 深圳总部站圆满结束
- Neat Syntax Design of an ETL Language (Part 2)
- Swift recursively queries the array for the number closest to the specified value
猜你喜欢

How to "transform" small and micro businesses (II)?
![[matlab] image binarization (imbinarize function)](/img/3e/066f460d9f436bbc43ea35e46093e2.jpg)
[matlab] image binarization (imbinarize function)

Rxjs TakeUntil 操作符的学习笔记

Redis(二)分布式锁与Redis集群搭建

Notes on writing questions in C language -- monkeys eat peaches

Mengyou Technology: six elements of tiktok's home page decoration, how to break ten thousand dollars in three days

puzzle(019.2)六边锁

How do wechat applets make their own programs? How to make small programs on wechat?

Cubemx stm32f105rb USB flash drive reading and writing detailed tutorial

Vscode attempted to write the procedure to a pipeline that does not exist
随机推荐
Notes on writing questions in C language -- monkeys eat peaches
Remove the mosaic, there's a way, attached with the running tutorial
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
Experience in writing C
CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
Exception: gradle task assemblydebug failed with exit code 1
Flutter dialog: cupertinoalertdialog
js工具函数,自己封装一个节流函数
字符串 实现 strStr()
How do dating applets make millions a year? What is the profit model?
WPF Prism框架
[Ruby on rails full stack course] course directory
汇付国际为跨境电商赋能:做合规的跨境支付平台!
Ruiji takeout project (II)
Use evo
Wechat official account can reply messages normally, but it still prompts that the service provided by the official account has failed. Please try again later
Learning notes of rxjs takeuntil operator
SparseArray details
An auxiliary MVP architecture project quick development library -mvpfastdagger