当前位置:网站首页>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
边栏推荐
- Chitubox micromake l3+ slicing software configuration correspondence
- Oracle function trigger
- Etcd教程 — 第四章 Etcd集群安全配置
- 如何自制一个安装程序,将程序打包生成安装程序的办法
- Grabcut image segmentation in opencv
- Armbian version name comparison
- Free platform for wechat applet making, steps for wechat applet making
- Flutter replaces the default icon of Gaud positioning
- Vscode attempted to write the procedure to a pipeline that does not exist
- NetCore性能排查
猜你喜欢

Encoding format for x86

Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.

Rxjs TakeUntil 操作符的学习笔记

Methodchannel of flutter

Exception: gradle task assemblydebug failed with exit code 1

Jetpack compose layout (I) - basic knowledge of layout

8、智慧交通项目(1)

Learning notes of rxjs takeuntil operator

Jetpack compose layout (II) - material components and layout

【mysql学习笔记21】存储引擎
随机推荐
力扣-104. 二叉树的最大深度
Redis(二)分布式锁与Redis集群搭建
How to make small programs on wechat? How to make small programs on wechat
What are the PMP scores?
What should be paid attention to in PMP examination?
MySQL create given statement
Rxjs TakeUntil 操作符的学习笔记
Mysql 源码阅读(二)登录连接调试
Mengyou Technology: tiktok live broadcast with goods elements hot topics retention skills shaping image highlight selling points
Puzzle (019.2) hexagonal lock
瑞吉外卖项目(二)
字符串 最长公共前缀
What functions should smart agriculture applet system design have
广发证券靠谱吗?是否合法?开股票账户安全吗?
Encoding format for x86
The problem of automatic page refresh after the flyer WebView pops up the soft keyboard
Redis(一)原理与基本使用
Fcpx quickly add subtitles | Final Cut Pro import fcpxml subtitle file does not match the video time? I got it in code
Gradle download warehouse is slow
[Ruby on rails full stack course] course directory