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 use master-slave replication of Redis?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Like mysql, redis supports master-slave replication, read-write separation, and 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 primary node. Now, let me show you how to turn on master-slave replication.

First, open two redis services. The configurations of the two redis services are identical except for different binding ports and different persistent files.

127.0.0.1 keys 6379 > keys * 1) "age" 2) "name" 127.0.0.1 keys * (empty list or set)

The 6379 redis currently has two keys, while 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, enter slaveof 127.0.0.1 6379 directly

127.0.0.1 slaveof 6380 > 127.0.0.1 6379OK127.0.0.1:6380 > keys * 1) "name" 2) "age"

As you can see, replication has been established, and now 6380 of the data is consistent with 6379.

You can see the information when you execute the info replication command for 6379 and 6380, respectively.

127.0.0.1 6379 > info replication# Replicationrole:masterconnected_slaves:1. 127.0.0.1 6380 > info replication# Replicationrole:slavemaster_host:127.0.0.1master_port:6379.

Disconnect replication

Disconnecting the replication operation is as simple as performing a slaveof no one.

127.0.0.1 6380 > slaveof no oneOK

In addition, the primary node can be switched 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 when you cut the master, you need to note that the data before the slave node will be emptied, and then the data of the new master node will be copied. Therefore, if the previous data is useful and not backed up, and the primary operation cannot be carried out.

Password verification

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

Read-only

By default, read operations are performed from the node, and write operations are not allowed. Because it is necessary to do so, if the slave node is written, it will cause data inconsistency between the master and slave nodes. If you want to write from the node, you can modify the configuration item slave-read-only=no.

Application scenario

Common application scenarios for redis replication are

Real-time backup of data, in general, there is only one slave node, aof persistence is enabled from the node, and the main task of the node is to back up the data in real time.

Failover, if the master node fails, then the slave node can be used to continue to run the system

Read-write separation is more suitable for scenarios with more reads, in which the master node performs write operations and multiple slave nodes perform read operations. Because replication occurs asynchronously, there may be delays in slave node data, which is also something to be aware of during development.

These are the details of Redis master-slave replication, please pay attention to other related articles!

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

Database

Wechat

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

12
Report