当前位置:网站首页>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 !
边栏推荐
- Ticdc introduction
- Powerdispatcher reverse generation of Oracle data model
- 比较DFS和BFS的优点和缺点及名称词汇
- redis-6. Redis master-slave replication, cap, Paxos, cluster sharding cluster 01
- Time field comparison time size in MySQL
- Related operations under Oracle Database
- B. I hate 1111 (mnemonic search number theory
- EF CORE执行SQL语句
- Sorting of numbers and strings
- 【splishsplash】重复输出splashsurf的脚本
猜你喜欢

Ticdc introduction

The biggest highlight of wwdc2022: metalfx

redis-2. Redis string type & bitmap

socket编程2:IO复用(select && poll && epoll)

Reflection of C # Foundation

MySQL row column conversion (updated version)

Detailed description of drawing ridge plot, overlapping densities of overlapping kernel density estimation curve, facetgrid object and function sns Kdeplot, function facetgrid map

通过函数seaborn.cubehelix_palette生成顺序调色板

redis-5. Redis' RDB, fork, copyonwrite, AOF, RDB & AOF are mixed

Try to use renderdoc to view the shader code of UE
随机推荐
powerdisgner逆向生成oracle数据模型
redis-2. Redis string type & bitmap
How is it that the income of financial products is zero for several consecutive days?
C语言:如何给全局变量起一个别名?
One article of quantitative framework backtrader read analyzer
Real time lighting of websocket server based on esp32cam
Powerdispatcher reverse generation of Oracle data model
redis-7. Redis master-slave replication, cap, Paxos, cluster sharding cluster 02
全志V3S环境编译开发流程
Simple understanding of basic language of C language
A solution to the problem that there is always a newline character when C merges multiple RichTextBox contents
redis-4. Redis' message subscription, pipeline, transaction, modules, bloom filter, and cache LRU
Deploy RDS service
关于#数据库#的问题:PGADMIN4 编辑sql窗口问题
不同系统添加证书
Paper notes: multi label learning bp-mll
MySQL row column conversion (updated version)
8. process status and transition
AQS - detailed explanation of reentrantlock source code
微隔离(MSG)