In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you the method of redis to achieve data synchronization, the code is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The master-slave synchronization mechanism of Redis can ensure the data synchronization between master and slave of redis.
Synchronization methods include: full replication and incremental replication
Full copy
When slave starts for the first time, connect to Master and send the PSYNC command in the format of psync {runId} {offset}
{runId} is the running id; {offset} of master is the replication offset of slave itself. When slave connects to master for the first time, slave does not know the runId of master or its offset. At this time, slave will send a question mark and-1 to tell the master node that it is synchronizing for the first time. Format is psync?-1
When master receives psync?-1 and knows that slave is to be copied in full, it informs slave of its runId and offset and replies to the command fullresync {runId} {offset}. At the same time, master executes the bgsave command to generate the rdb file, during which all write commands are written to the buffer.
After slave receives a reply from master, the runId and offset,slave of master are saved and synchronized at this time. Slave is synchronized. If a request is received at this time, the current request will be responded to when the parameter slave-server-stale-data yes is configured; slave-server-stale-data no, an error will be returned.
After the execution of master bgsave, send the rdb file to slave. After the rdb file is sent, the write command in the buffer is sent to slave.
Slave receives the rdb file, discards all old data, and starts loading the rdb file.
After the rdb file synchronization ends, slave executes all write commands sent from the master buffer.
After that, each time master executes a write command, it sends the same write command to slave.
Incremental copy
If there is an abnormal situation such as network flash or command loss, when the master-slave connection is restored, because the slave node previously saved its own replicated offset and the running ID of the master node. Therefore, they are sent as psync parameters to the master node, requiring a partial copy operation in the format psync {runId} {offset}.
After receiving the psync command, the master node first checks whether the parameter runId is consistent with itself, which means that the current master node was copied before. Then the master node is searched in its own replication backlog buffer according to the parameter offset. If the data after the offset exists in the buffer, a + continue response is sent to the slave node, indicating that partial replication can be carried out; otherwise, full replication can be carried out.
The master node sends the data in the replication backlog buffer to the slave node according to the offset to ensure that the master-slave replication enters a normal state.
The above is the method of data synchronization in redis. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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.