Home >Database >Redis >Redis master-slave replication

Redis master-slave replication

齐天大圣
齐天大圣Original
2020-05-25 14:08:321406browse

Like mysql, redis also supports master-slave replication and read-write separation, and the configuration is much simpler than mysql. Let's take a look at the master-slave replication of redis.

Establish replication

By default, Redis is the master node. Now, let me demonstrate how to enable master-slave replication.

First, open two redis services. Except for the different bound ports and different persistent files, these two redis services have the same configuration.

127.0.0.1:6379> keys *
1) "age"
2) "name"

127.0.0.1:6380> keys *
(empty list or set)

6379 There are currently two redis services. keys, and 6380 is currently empty. Establish a master-slave relationship between 6379 and 6380, with 6379 as the master node and 6380 as the slave node.

There are two ways to establish replication:

  • Modify the configuration file of 6380, add slaveof 127.0.0.1 6379 to the configuration file, and then restart the redis service

  • Dynamic modification, directly enter slaveof 127.0.0.1 6379

127.0.0.1:6380> slaveof 127.0.0.1 6379
OK
127.0.0.1:6380> keys *
1) "name"
2) "age"

You can see that the replication has been established, and now the 6380 data is consistent with 6379 .

Execute the info replication command on 6379 and 6380 respectively to see the relevant information.

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
……


127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
……

Disconnect replication

Disconnect replication operation is very simple, just execute slaveof no one.

127.0.0.1:6380> slaveof no one
OK

In addition, you can also switch the master node directly. For example, now 6380 is the slave node of 6370, and now 6380 wants to disconnect the master-slave relationship of 6379 and establish a new replication relationship with 6381. Then 6380 only needs to execute slaveof 127.0.0.1 6381.

But you need to pay attention when cutting the master. The data before the slave node will be cleared, and then the data of the new master node will be copied. Therefore, if the previous data is useful and not backed up, the main operation cannot be performed.

Password verification

If the master node is configured with requirepass, then the slave node needs to set masterauth

Read-only

By default, the slave node performs read operations and cannot perform write operations. Because this is very necessary, if the slave node performs a write operation, it will cause data inconsistency between the master and slave nodes. If you want to write from the slave node, just modify the configuration item slave-read-only=no.

Application Scenarios

Common application scenarios for redis replication include

  • Real-time backup of data , Generally in this case, there is only one slave node, and the slave node enables aof persistence. The main task of the node is to back up data in real time.

  • Failover, if the master node fails, you can use the slave node to continue running the system

  • Read and write separation, more suitable In scenarios where there are many reads, the master node performs write operations and multiple slave nodes perform read operations. Because replication is performed asynchronously, there may be a delay in data from the slave node, which needs to be paid attention to during development.

The above is the detailed content of Redis master-slave replication. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn