Technology Sharing

6. Redis master-slave cluster construction

2024-07-11

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Redis master-slave cluster construction

1. What is a Redis master-slave cluster?

​ Redis master-slave cluster is a "one master and multiple slaves" read-write separation cluster. The Master node in the cluster is responsible for the client's read and write requests, while the Slave node is responsible for the client's read requests. The reason why the cluster is built in read-write separation mode is mainly because, for the database cluster, the write operation pressure is generally small, and the read pressure is greater. Therefore, only one node is responsible for processing the write request operation.

2. Pseudo cluster construction and configuration

When using a single-threaded IO model, in order to improve the utilization of the processor, multiple Redis servers are usually installed on a host to build a Redis master-slave pseudo cluster. Of course, the pseudo cluster scenario is mainly to learn Redis.

The read-write separation pseudo cluster to be built below contains a Master and two Slaves. Their port numbers correspond to: 6380, 6381, 6382.

2.1. Copy the redis.conf file

​ Create a directory named cluster in the redis installation directory. Then copy the redis.conf file to this directory. This file will be included by other configuration files later, so the same common properties of each Redis node need to be set in this file.

2.2. Modify the redis.conf file

2.2.1、masterauth

​ Because we want to build a master-slave cluster, and each host may be the master, it is best to set the password verification attribute requirepass. If you really need to set it, make sure that the password of each host is set to the same. At this time, two identical attributes must be set in each configuration file: requirepass and masterauth. Requirepass is used to specify the current host access password, and masterauth is used to specify the access password submitted by the current slave to the master, so that the master can verify whether its identity is legal.

2.2.2、repl-disable-tcp-nodelay
  1. What is tcp-nodelay?

    We should know that in order to reduce network bandwidth, TCP always wants to send as large a data block as possible during data transmission. Therefore, TCP uses the Nagle algorithm. This algorithm will cache data to a certain size before sending it in a package.

  2. Configuration of the repl-disable-tcp-nodelay parameter:

    This property is used to set the algorithm to disable the TCP feature tcp-nodelay. If set to yes, it is disabled (that is, data is cached to a certain size before sending). In this case, the communication between the master and the slave will be delayed, but the number of TCP packets used will be reduced, and the network bandwidth will be less. If set to no, it will be the opposite. Note: When there are many master-slave levels, it is recommended to set it to no to prevent data delay.

2.3. Specific construction

Reference articles:

http://t.csdnimg.cn/Fjyhw