How to set up master-slave replication in Redis

Redis master-slave replication

  • Persistence ensures that data will not be lost even if the redis service is restarted, but when the hard disk of the redis server is damaged, data loss may be caused. This single point of failure (single server failure) can be avoided through the master-slave replication mechanism of redis .
  • The data in the master redis and the data on the slave are kept in real-time synchronization, and when the master redis writes data, it is copied to the two slave services through the master-slave replication mechanism.
  • Master-slave replication does not block the master, and the master can continue to process client requests while synchronizing data.
  • Host master configuration: no configuration required

It is recommended to synchronize data in master-slave mode:
Insert picture description here

Generally used in work: one master and two slaves or one master and one slave

The data will be synchronized to the slave server. The same data exists on several servers in this cluster.

Master-slave construction steps:

Host: No configuration required. Only need to configure slave, slave slave configuration: (here is a pseudo cluster)

The first step: copy a slave machine, pay attention to use the root user

[root@localhost myapps]# cp redis/ redis1 -r
[root@localhost myapps]# ll
总用量 40
drwxr-xr-x. 3 root root 4096 2月 1 09:26 redis
drwxr-xr-x. 3 root root 4096 2月 1 09:27 redis1

Step 2: Modify the redis.conf of the slave

Syntax: replicaof // replicaof host ip host port number

Tip: Retrieve files: Enter: /replicaof When the current page is not available, enter n to find the next page

Insert picture description here

Step 3: Modify the port address of the slave to 6380

Modify in the slave redis.conf
Insert picture description here

Step 4: Clear the persistent files in the slave

[root@localhost bin]# rm -rf appendonly.aof dump.rdb
[root@localhost bin]# ll
总用量 15440
-rwxr-xr-x. 1 root root 4588902 7月 1 09:27 redis-benchmark
-rwxr-xr-x. 1 root root 22225 7月 1 09:27 redis-check-aof
-rwxr-xr-x. 1 root root 45443 7月 1 09:27 redis-check-dump
-rwxr-xr-x. 1 root root 4691809 7月 1 09:27 redis-cli
lrwxrwxrwx. 1 root root 12 7月 1 09:27 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 6450337 7月 1 09:27 redis-server

Step 5: Start the slave

[root@localhost redis1]# ./bin/redis-server ./redis.conf

Step 6: Start the 6380 client

[root@localhost redis1]# ./bin/redis-cli -p 6380
127.0.0.1:6380> keys *
1) "mylist"
2) "num"
3) "bookCate1"
4) "newbook"
127.0.0.1:6380>

Stop the client: ./bin/redis-cli -p 6380 shutdown

note:

Ø Once the host adds, deletes, and changes, the slave will automatically synchronize the data to the slave

Ø The slave cannot perform write operations, only read

127.0.0.1:6380> get username
"hehe"
127.0.0.1:6380> set username haha
(error) READONLY You can't write against a read only slave.

Principle of the process of replication

  • When the MS (master slaver) relationship is established between the slave library and the master library, a SYNC command will be sent to the master database;
  • After the main library receives the SYNC command, it will start to save the snapshot in the background (RDB persistence process), and cache the write commands received during the period;
  • After the snapshot is completed, the master Redis will send the snapshot file and all cached write commands to the slave Redis;
  • After receiving it from Redis, it will load the snapshot file and execute the received cache command;
  • Whenever the master Redis receives a write command, it will send the command to the slave Redis to ensure the consistency of the data; [It is done internally, so it does not support the client to manually write data on the slave.

Is there a downtime in the replication architecture?

Downtime from Redis: just restart

Master Redis is down: execute the SLAVEOF NO ONE command from the database (slave), disconnect the master-slave relationship and promote the master database to continue serving [put a slave as the master, this time the new master [previous slave] will have Write ability]; After the master server is repaired, after restarting, execute the SLAVEOF command to set it as a slave library [the old master is set as a slave]. [Manual execution, the process is complicated and error-prone. ] Is there a better solution?

Guess you like

Origin blog.csdn.net/weixin_43515837/article/details/113095987