Home > Database > Redis > body text

Introduction to redis master-slave replication

Release: 2020-03-23 09:31:30
forward
2032 people have browsed it

Introduction to redis master-slave replication

The replication function of redis is to support data synchronization between multiple databases. One is the master database (master) and the other is the slave database (slave). The master database can perform read and write operations. When a write operation occurs, the data is automatically synchronized to the slave database. The slave database is generally read-only and receives For data synchronized from the master database, a master database can have multiple slave databases, while a slave database can only have one master database.

Recommended: redis introductory tutorial

The master-slave replication function of redis can effectively separate the reading and writing of the database and improve the load capacity of the server. The main server mainly Responsible for write operations, and the slave server is mainly responsible for read operations

Master-slave replication process:

1: When a slave database is started, a sync command will be sent to the master database ,

2: After receiving the sync command, the main database will start saving the snapshot in the background (performing RDB operations), and cache the commands received during the save period.

3: When the snapshot is completed , redis will send the snapshot file and all cached commands to the slave database.

4: After receiving it from the database, the snapshot file will be loaded and the received cached command will be executed.

Note: Versions before redis 2.8: When the master-slave database is synchronized, the slave database will re-perform the above operation after being disconnected and reconnected due to network reasons, and resuming the download is not supported. Redis2.8 and later support breakpoint resumption.

Note: Starting from Redis 2.8, in order to ensure data security, you can configure min-slaves-to-write to allow a master node to perform write operations only when it has at least N slave nodes. The slave node pings the master node once per second, and the master node records the last time each slave server sent a ping to it. Users can specify the maximum network delay min-slaves-max-lag and the minimum number of slave servers required to perform write operations through configuration

  min-slaves-to-write
  min-slaves-to-write 3
  min-slaves-max-lag 10
Copy after login

If there are at least min-slaves-to-write slave servers, And the delay value of these servers is less than min-slaves-max-lag seconds, then the main server will perform the write operation requested by the client. As long as one condition is not met, the write operation will not be performed and the master server will return an error to the client requesting the write operation.

2. Master-slave replication deployment:

Redis master-slave structure supports one master and multiple slaves

Master node: 192.168.1.170

Slave node: 192.168.1.171

Note: The configuration of all slave nodes is the same

Method 1: Manually modify the configuration file

Only additional modifications are required in the slave node The slaveof attribute in the redis configuration file can

slaveof 192.168.1.170 6379
Copy after login

Start redis on the 170 master node and view the redis info information (execute the info command)

Introduction to redis master-slave replication

Start redis on the 171 slave node

Introduction to redis master-slave replication

View redis info information

Method 2: Dynamic setting

Connect to the slave node server through redis-cli and execute the following command.

Introduction to redis master-slave replication

3. Issues that need to be paid attention to in master-slave replication:

① If you use master-slave replication, make sure your master has persistence activated. Or make sure it doesn't automatically restart after crashing. The slave is a full backup of the master, so if the master is restarted with an empty data set, the slave will also be cleared.

② If the master database has a password set when configuring the redis replication function, you need to set the password of the master database through the masterauth parameter in the slave data configuration file, so that the slave database will automatically use it when connecting to the master database. The auth command is authenticated. It is equivalent to a password-free login.

Related recommendations:

mysql video tutorial: https://www.php.cn/course/list/51.html

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

Related labels:
source:oschina.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template