Get the App
SLTechnology News&Howtos  ›  Database  › 

How to perform Master-Slave replication in redis

Shulou Source: shulou.com Published: 2022-06-01 19:47:25 09月15日 Update

Today, the editor will bring you an article on how to copy redis from master and slave. The editor thinks it's pretty good, so I'll share it for you as a reference. Let's follow the editor and have a look.

The process of synchronous implementation of redis master-slave replication

1. Send a sync synchronization command to the master service to request full synchronization from the slave service

two。 When the master service receives the sync synchronization command from the slave service, it will fork a child process to execute the bgsave command in the background (non-blocking) snapshot save, generate the RDB file, and set the

Send the RDB file to the slave service

3. Load the received RDB file into its own redis memory from the service

4. After the slave service loads the RDB, the master service sends all write commands in the buffer to the slave service.

5. The slave service loads all the write commands of the master service into memory to achieve complete data synchronization.

6. The slave service only needs to send its own offset location (equivalent to the location of mysql binlog) when it needs to synchronize data next time, and only the newly added data is synchronized, without the need for full synchronization.

Master-slave synchronization through configuration files

1. Just configure the slave service profile

[root@localhost] # vim / app/redis/etc/redis.conf... .. slaveof 192.168.1.9 6379 # add a slave service belonging to a host. .. masterauth 123456 # password for connecting to the master server from the slave service. Slave-read-only yes # is read-only from the service and cannot write data on the command line.

two。 Restart the slave service to realize the master-slave connection

[root@localhost ~] # redis-server / app/redis/etc/redis.conf [root@localhost ~] # redis-cli127.0.0.1:6379 > auth 123456OK127.0.0.1:6379 > info replication#Replicationrole:slavemaster_host:192.168.1.9master_port:6379master_link_status:up.

This is how redis does master-slave replication. The code should be clear enough, and I believe there are quite a few examples that we may meet in our daily work. Through this article, I hope you can gain more.

Tags: Services synchronization commands master and slave files data configuration location memory good clear for this size host code examples no more just background password Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Technology Shulou Information OPPO Reno Shulou Tech Info