In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly shows you "Redis how to achieve master-slave replication", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Redis how to achieve master-slave replication" this article.
In the previous introduction to Redis, we all operate on a server, that is to say, read, write and backup operations are carried out on a Redis server, so with the increase of project visits, operations on the Redis server become more frequent. Although the Redis read and write speed is very fast, it will also cause a certain delay to a certain extent, so in order to solve the problem of large traffic One method that is usually adopted is that the master-slave architecture Master/Slave,Master is write-based, Slave is read-based, and the Master master node is updated and automatically synchronized to the slave Slave node according to the configuration.
Next we will introduce how to build the master-slave architecture.
Ps: here I am simulating multiple Redis servers on one machine. Compared with the actual production environment, the basic configuration is the same, only the IP address and port number change.
1. Modify the configuration file
First, make three copies of the redis.conf configuration file and simulate the three Redis servers by modifying the port.
Then we modify the three redis.conf files respectively.
①, modify daemonize yes
Indicates that the specified Redis starts as a daemon (background startup)
②, configure PID file path pidfile
Indicates that when redis runs as a daemon, it writes pid to the / var/redis/run/redis_6379.pid file by default
③, configure Port port
④, name of configuration log file
⑤, configuration rdb file name
Configure 6380redis.conf and 6381redis.conf once in turn, and then the configuration is finished.
Next, we start these three services respectively.
Use the command to see if Redis is started:
Next, enter the three Redis clients with the following command:
one
two
three
Redis-cli-p 6379
Redis-cli-p 6380
Redis-cli-p 6381
2. Set up the master-slave relationship
①, view node roles through the info replication command
We found that all three nodes play the role of Master. So how do you turn 6380 and 6381 nodes into Slave roles?
②, select port 6380 and port 6381, execute the command: SLAVEOF 127.0.0.1 6379
Let's take a look at the 6379 node information:
Here, the master-slave relationship is set by command, and once the service is restarted, the role relationship will no longer exist. To save this relationship permanently, you can configure it by configuring the redis.conf file.
3. Test the master-slave relationship
①, incremental replication
The master node executes the set K1 v1 command, can it be obtained from the slave node get K1?
It can be seen from the above picture that it is available.
②, full replication
By executing SLAVEOF 127.0.0.1 6379, if there is some key before master node 6379, will the slave node copy all the previous information after executing the command?
The answer is also yes, so I won't post the test results here.
③, separation of master and slave read and write
The master node can execute the write command, and the slave node can execute the write command?
The reason for this is the configuration of slave-read-only in the configuration file 6381redis.conf
If we change it to no, it is OK to execute the write command.
However, the data of writing commands from the slave node cannot be obtained from either the node or the master node.
④, primary node downtime
If the master node Maste is down, will the roles of the two slave nodes change?
The figure above shows that after the master node Master is dead, the role of the slave node will not change.
⑤, recovery of primary node after downtime
After the primary node Master hangs up, start the host Maste immediately. Does the primary node still play the role of Master?
That is to say, the master node is restarted after it is hung up, and the role of the master node is restored.
4. Sentinel mode
Through the previous configuration, there is only one master node Master, once the master node dies, the slave node can not take up the task of the master node, then the whole system can not run. If after the master node dies, the slave node can automatically become the master node, then the problem is solved, and the Sentinel mode is born.
Sentinel mode is to monitor from time to time whether the redis is running well as expected (at least to ensure that the master node exists). If there is a problem with a host, the Sentinel will automatically set one of the slaves under that host as the new host and allow other slaves to establish a master-slave relationship with the new host.
Steps to build Sentinel mode:
①, create a new sentinel.conf file under the configuration file directory with no wrong name, and then configure the corresponding content
one
Sentinel monitor name of the monitored machine (name by yourself) ip address port number votes
Configure the monitored name, ip address, port number, and the number of votes obtained. If the number of votes above is 1, it means that after the host dies, salve votes to see who will take over as the host. If the number of votes is greater than 1, the host will become the host.
②, activate Sentinel
one
Redis-sentinel / etc/redis/sentinel.conf
Launch the interface:
Next, we kill the host 6379 and see what happens to the slave node.
After killing the master node, we looked at the background print log and found that 6381 of the votes had become the master node.
At this point, we look at the node information of slave node 6381:
The 6381 node automatically becomes the master node.
PS: there is also a single point of failure in Sentinel mode. If the Sentinel machine crashes, it cannot be monitored. The solution is that Sentinel also sets up clusters. Redis Sentinel mode supports clusters.
5. The principle of master-slave replication
The replication function of Redis is divided into two operations: synchronization (sync) and command propagation (command propagate).
①, old version synchronization
When the slave node issues the SLAVEOF command asking the slave server to copy the master server, the slave server does so by sending the SYNC command to the master server. The steps to execute the command:
1. Send the SYNC command from the server to the master server
2. The master server that receives the SYNC command executes the BGSAVE command, generates a RDB file in the background, and uses a buffer to record all write commands executed from the beginning.
3. When the BGSAVE command of the master server is completed, the master server will send the RDB file generated by the BGSAVE command to the slave server, receive the RDB file from the server, and update the server status to the status recorded by the RDB file.
4. The master server also sends all write commands of the buffer to the slave server, and executes the corresponding commands from the slave server.
②, command propagation
When the synchronization operation is completed, the master server will make the corresponding modification command, and the slave server and the master server will be inconsistent.
In order to keep the state of the master server consistent with the slave server, the master server needs to perform command propagation operations on the slave server, and the master server will send its own write commands to the slave server for execution. After the corresponding command is executed from the slave server, the state of the master and slave server continues to be consistent.
Summary: through the synchronous operation and command propagation function, it can ensure the consistent characteristics of master and slave.
But let's consider a problem: if the slave server is suddenly disconnected during the synchronization of the master server, and at this time the master server does some write operations, the connection from the server is restored at this time. If we are synchronizing, then we must re-generate a RDB file from the master server and load it to the slave server, although consistency can be ensured. But in fact, the state of the master and slave server is consistent before disconnecting, the inconsistency is that the master server is disconnected from the server, and the master server executes some write commands, so can you just disconnect the write commands after resuming the connection from the server instead of the entire RDB snapshot?
Synchronization is actually a very time-consuming operation, the master server needs to generate a RDB file through the BGSAVE command, and then needs to send the file to the slave server, receive the file from the server, and then load the file, and during loading, the slave server can not process other commands.
To solve this problem, Redis has replaced the SYNC command with the new synchronization command PSYNC since version 2. 8. Part of the resynchronization function of this command is used to deal with the efficiency of re-replication after disconnection. In other words, when the slave server reconnects to the master server after being disconnected, the master server only sends the write commands executed after the disconnection to the slave server, and the slave server only needs to receive and execute these write commands to keep the master and slave consistent.
6. the shortcomings of master-slave replication.
Master-slave replication solves the single point failure problem of the master node, but because all write operations are operated on the Master node and then synchronized to the Slave node, the synchronization will have a certain delay. When the system is very busy, the delay problem will be more serious, and it will become more serious with the increase of slave of the slave node.
The above is all the contents of the article "how to achieve master-slave replication in Redis". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.