Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Redis Master-Slave replication

2025-02-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Redis master-slave replication how to achieve", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Redis master-slave replication how to achieve" it!

Master-slave replication

Master-slave replication can expand the performance of redis to some extent. The master-slave replication of redis is similar to the master-slave replication of relational database, and the slave can accurately replicate the content on the host. After master-slave replication, on the one hand, it can achieve the separation of data read and write, reduce the pressure of master, on the other hand, it can also achieve data backup.

Configuration mode

Suppose I have three redis instances with the following addresses:

192.168.248.128:6379 192.168.248.128:6380 192.168.248.128:6381

That is, three instances on the same server are configured as follows:

1. Rename the redis.conf file to redis6379.conf to make it easier for us to distinguish, and then make two more copies of the redis6379.conf, redis6380.conf and redis6381.conf. As follows:

two。 Open redis6379.conf and add 6379 to the following configurations (the default is 6379 without modification), as follows:

Port 6379pidfile / var/run/redis_6379.pidlogfile "6379.log" dbfilename dump6379.rdbappendfilename "appendonly6379.aof"

3. Similarly, open the redis6380.conf and redis6381.conf configuration files respectively, and change the 6379 involved in the second step to 6380 and 6381, respectively.

4. Start three redis instances by entering the following command:

[root@localhost redis-4.0.8] # redis-server redis6379.conf [root@localhost redis-4.0.8] # redis-server redis6380.conf [root@localhost redis-4.0.8] # redis-server redis6381.conf

5. Enter the following command to enter the console of the three instances:

[root@localhost redis-4.0.8] # redis-cli-p 6379 [root@localhost redis-4.0.8] # redis-cli-p 6380 [root@localhost redis-4.0.8] # redis-cli-p 6381

At this point, I successfully configured three redis instances.

6. Suppose that in these three instances, 6379 is the host, that is, master,6380 and 6381 are slaves, that is, slave, then how to configure this instance relationship is very simple, execute the following commands on 6380 and 6381 respectively:

127.0.0.1 SLAVEOF 6381 > 127.0.0.1 6379OK

This step can also be solved by adding the following configuration to the redis.conf of the two slaves:

Slaveof 127.0.0.1 6379

OK, after the master-slave relationship is built, we can view the current status of each instance with the following command, as follows:

127.0.0.1 purl 6379 > INFO replication# Replicationrole:masterconnected_slaves:2slave0:ip=127.0.0.1,port=6380,state=online,offset=56,lag=1slave1:ip=127.0.0.1,port=6381,state=online,offset=56,lag=0master_replid:26ca818360d6510b717e471f3f0a6f5985b6225dmaster_replid2:0000000000000000000000000000000000000000master_repl_offset:56second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:56

We can see that 6379 is a host, with two slaves hanging on it, and the addresses and ports of the two slaves are displayed. If we execute INFO replication on 6380, the message is as follows:

127.0.0.1 6380 > INFO replication# Replicationrole:slavemaster_host:127.0.0.1master_port:6379master_link_status:upmaster_last_io_seconds_ago:6master_sync_in_progress:0slave_repl_offset:630slave_priority:100slave_read_only:1connected_slaves:0master_replid:26ca818360d6510b717e471f3f0a6f5985b6225dmaster_replid2:0000000000000000000000000000000000000000master_repl_offset:630second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:630

We can see that 6380 is a slave, and the information of the slave and its host are displayed.

7. At this point, we store a piece of data in the host, and we can get it in the slave.

Master-slave replication attention

1. If the host has been running for some time and some data has been stored and connected from the computer, then back up all the data on the host from the opportunity instead of starting from the point in time of the connection.

two。 After master-slave replication is configured, the host is readable and writable, but the slave can only read and not write (you can also make the slave write by modifying the value of slave-read-only in redis.conf).

3. In the whole process of running the master-slave structure, if the host unfortunately dies, after reboot, he is still the host, and the master-slave replication operation can continue.

Principle of replication

Each master has a replication ID, which is a large pseudorandom string that marks a given dataset. Each master also holds an offset. When master sends its own replication stream to slave, the offset increases as many bytes of data are sent, so that when a new operation modifies its own dataset, it can update the status of slave. The replication offset increases even when no slave is connected to the master, so basically for each given pair of Replication ID, the offset identifies the exact version of the master dataset. When slave connects to master, they use the PSYNC command to send the old master replication ID they recorded and the offset they have processed so far. In this way, master can send only the increments required by slave. But if there is not enough command backlog in master's buffer, or if slave references history records that are no longer known (replication ID), a full resynchronization will be done instead: in this case, slave will get a complete copy of the dataset, starting from scratch (see the redis website).

To put it simply, it is the following steps:

When 1.slave starts a successful connection to master, it sends a sync command. 2.Master receives the command to start the save process in the background and collects all the commands received to modify the dataset. 3. After the background process finishes, master transfers the entire data file to slave to complete a full synchronization. 4. Full copy: after receiving the database file data, the slave service saves it and loads it into memory. 5. Incremental copy: Master continues to send all new collected modification commands to slave in turn to complete the synchronization. 6. However, as long as you reconnect to the master, a full synchronization (full copy) will be performed automatically. Thank you for your reading, the above is the content of "how to achieve Redis master-slave replication". After the study of this article, I believe you have a deeper understanding of how to achieve Redis master-slave replication, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report