当前位置:网站首页>Redis learning journey master-slave replication
Redis learning journey master-slave replication
2022-06-13 07:28:00 【Zhao JC】
Redis Learning journey -- Master slave copy
Master slave copy
Relevant concepts
Master slave copy , It means to put one Redis Server data , Copy to other Redis The server . The former is called the main node (master/leader), The latter is called the slave node (slave/follower);
- Data replication is one-way , From master to slave only .
- Master Write first ,Slave Mainly reading .
By default , Each station Redis Servers are all primary nodes ;- A master node can have multiple slaves ( Or no slave ), But a slave node can only have one master node .
Application of master-slave replication
- 1、 data redundancy : Master-slave replication realizes hot backup of data , It's a way of data redundancy beyond persistence .
- 2、 Fault recovery : When there is a problem with the master node , Can be served by a slave node , Fast fault recovery ; It's actually a service Redundancy of .
- 3、 Load balancing : On the basis of master-slave replication , Cooperate with the separation of reading and writing , Write service can be provided by the master node , Read service provided by slave node ( The write Redis Connect master node when data is applied , read Redis Apply connection from node when data ), Share server load ; Especially in the situation of less writing and more reading , Sharing read load through multiple slave nodes , Can be greatly improved Redis Concurrency of servers .
- 4、 High availability ( colony ) footstone : In addition to the above functions , Master slave replication is also the foundation for sentinels and clusters to implement , Therefore, the master-slave complex The system is Redis High availability Foundation .
The meaning of master-slave replication
Generally speaking , To put Redis Used in engineering projects , Use only one Redis Absolutely not ( Downtime ), Here's why :
- 1、 Structurally , Single Redis The server will have a single point of failure , And a server needs to handle all the request loads , pressure ;
2、 In terms of capacity , Single Redis Server memory capacity is limited , Even if it's a Redis The memory capacity of the server is 256G, You can't use all the memory Redis Storage memory , Generally speaking , A single Redis The maximum memory used should not exceed 20G.
Products on e-commerce websites , It's usually a upload , Countless times , To say professional is to say " Read more and write less ".
For this scenario , We can make the following Architecture :
Master slave copy , Read / write separation ! 80% In this case, they are all reading operations ! Slow down the pressure on the server ! Architecture often uses ! One master and two subordinates !
Just in the company , Master slave replication is what must be used , Because it's not possible to use it alone in a real project Redis!
test
Environment configuration
Only configure slave Libraries , No need to configure the main library !
- View the current configuration information info replication
127.0.0.1:6379> info replication # View the current library information
# Replication
role:master # role master
connected_slaves:0 # Number of slaves ( Not yet )
master_replid:0a24bb6800f3566379d0953f6b86769d019b8fe4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
Copy 3 Configuration files , Then modify the corresponding information
1、 port

2、pid name 
3、log File name 
4、dump.rdb name 
After modification , Start our 3 individual redis The server , You can view through the process information !
One master and two slaves
By default , Each station Redis Servers are all primary nodes ;
In general, we only need to configure the slave machine ! Recognize the boss ! A master (79) Two from (80,81)
Look at the host info replication

Configure slave slaveof 127.0.0.1 6379

Look at the host info replication
Using the same command will 6181 The port of is also configured as 6379 Slave of 
The real slave master configuration should be configured in the configuration file ( I talked about ), It's permanent , We're using commands here , Temporary !
One master and two slave verification
The host can write , The slave can't write, only read ! All the information and data in the host , Will be automatically saved by the slave machine !
- Host write

- The slave can only read , Can't write

The host is disconnected , The slave is still connected to the host , But there is no write operation , This is the time , If the mainframe comes back , The slave can still get the information written by the host directly ! - The host is disconnected , Set after reconnection k2

- Get from the front and back of the machine k2 Value

If you use the command line , To configure the master-slave , At this time, if the slave is restarted , It will change back to the host ! As long as it becomes a slave again , Immediately you get the value from the host !
- close 6781 Disconnected Services , And turn it back on ( After restart, it is the host )


- At this time, the host setting value ,6381 The port cannot be obtained


- 6381 Again 6379 The value can be obtained again after the slave of


The principle of replication
Slave Start successfully connected to master A... Will be sent later sync Synchronization command Master Received orders , Start the background storage process , At the same time, collect all received commands for modifying the dataset , After the background process is finished ,master Transfer the entire data file to slave, And complete a full synchronization .
- Copy in full : and slave The service receives the database file data , Save it and load it into memory .( The first connection )
- Incremental replication :Master Continue to pass all new collected modification commands to slave, Complete synchronization ( Data after the second and third times )
But just reconnect master, One full synchronization ( Copy in full ) Will be executed automatically ! We can see it in the data machine !
Layer by layer link mode
The last connection is Master, Next connection Salove! At this time, we can also complete our master-slave replication !
- modify 6381 The host to 6380

- see 6380 and 6379 The situation of


- 6379 Set the value ,6381 You can also get


Plot to usurp the throne
If there is no boss , Can you choose a boss to come out at this time ? Manual !
- close 6379 port

- 6380 Be the boss slaveof no one

- 6381 Port when 6380 The slave node of the port


- When 6379 After the port comes back ,6381 It doesn't belong to him anymore , It is equivalent to being usurped

If the host is disconnected , We can use slveof no one Make yourself a host ! Other nodes can be manually connected to the latest master node ( Manual )! If this is the time for the boss to repair , Then connect again !
边栏推荐
- Lightning breakpoint continuation
- redis-3. Redis list, set, hash, sorted_ set、skiplist
- oracle问题,字段里面的数据被逗号隔开,取逗号两边数据
- Login registration
- 考研英语
- C # using multithreading
- c#高级编程-特性篇
- 论文笔记: 多标签学习 BP-MLL
- How to use clion to debug a project built by the make tool
- Que se passe - t - il si les produits financiers sont nuls pendant plusieurs jours?
猜你喜欢

JMeter encryption interface test

Micro isolation (MSG)

Oracle problem: the data in the field is separated by commas. Take the data on both sides of the comma

Logback log framework learning and problems

powerdisgner逆向生成oracle数据模型

在排序数组中查找元素的第一个和最后一个位置

AQS - detailed explanation of reentrantlock source code

Try to use renderdoc to view the shader code of UE

Priority analysis of list variables in ansible playbook and how to separate and summarize list variables

对绘制丘岭密度图ridge plot的详细说明、重叠核密度估计曲线overlapping densities、FacetGrid对象、函数sns.kdeplot、函数FacetGrid.map
随机推荐
Find the first and last positions of elements in a sorted array
Table access among Oracle database users
An example of CSRF attack defense in web application scenarios
EF CORE执行SQL语句
SDN基本概述
RT-Thread 模拟器 simulator LVGL控件:slider 控件
The biggest highlight of wwdc2022: metalfx
全志V3S环境编译开发流程
Quick sort
C Advanced Programming - features
Personal JS learning notes
redis-7. Redis master-slave replication, cap, Paxos, cluster sharding cluster 02
Department store center supply chain management system
I always don't understand the high address and high position
Sharp weapon tcpdump
【ViveFocus使用WaveVR插件获取手柄操作事件】
Lightning breakpoint continuation
6. system call
Postgraduate entrance examination English
Make cer/pfx public and private key certificates and export CFCA application certificates