I. Working principle of master-slave synchronization

Redis master/slave replication: After the slave is configured, the slave connects to the master and sends the sync command. Whenever you connect for the first time or reconnect, the master starts a background process that saves the database snapshot to a file, while the master process begins collecting new write commands and caching them. After the background process finishes writing the file, the master sends the file to the slave. The slave saves the file to the hard disk and loads the file to the memory. The master then forwards the cached commands to the slave. If the master receives a synchronous connection command from multiple slaves at the same time, the master will start only one process to write the database image and then send it to all slaves. The master synchronizes data in a non-blocking manner and can receive read and write requests from users. However, the slave is in blocking mode. When synchronizing master data, the slave cannot respond to client queries.

You can disable data persistence on the master by commenting out all save configurations in the master configuration file and then configuring data persistence only on the slave.

If the link between the Master and Slave is disconnected, the Slave can automatically reconnect to the Master, but after the connection is successful, a full synchronization is performed automatically.

  • Slave connects to master
  • Slave Sends the SYNC command
  • The master server backs up the database to the. RDB file
  • The master server transfers. RDB files to the slave server
  • Slave The slave server imports. RDB file data to the database

The above 5 steps are the first stage of synchronization, and each subsequent command invoked on the Master server uses replicationFeedSlaves() to synchronize to the slave server.

2. Configure primary/secondary synchronization

1. Create a master-slave folder, create master and slave in master-slave, and add redis.conf to the master and slave folders.

2. Modify the port number, process file, and log file locations of the master and slave configuration files.

3. Modify the slave configuration file.

slaveof <masterip> <masterport>    # specify the IP address and port of the master
masterauth <master-password>       # master has validation
slave-read-only yes                Set slave to read-only mode, default
Copy the code

4. Go to the master and slave folders respectively and start the redis service: redis-server redis.conf

5. Connect to the client

  1. Connection master: redis-cli -h 192.168.99.207 -p 6379

Enter info to view the information about the master role

  1. Connect slave: redis-cli -h 192.168.99.207 -p 6380

To view the information about slave, enter info. The role is slave, and

  • master_link_status:up
  • Master_repl_offset is the same as slave_REPL_offset
  • Master_last_io_seconds_ago If the value is within 10 seconds, the configuration is successful

Test 6.

  1. Add data to master: set name XBQ

  2. Query data in slave: get name, the result is the same as the data inserted in master.

  3. Set address shenzheng, (error) READONLY You can’t write against a read only slave.

This is because we set the slave to read-only and cannot write.

3. Application of master/slave synchronization

1. Used for hot backup of data

2. Read/write separation