[Redis Notes 01] Stand-alone mode and master-slave mode for Redis environment construction

This article mainly introduces the stand-alone mode and master-slave mode of Redis environment construction.

Table of contents

1. Stand-alone mode and master-slave mode

1.1, stand-alone mode

1.2. Master-slave mode

(1) Principle of master-slave mode

(2) Master-slave mode construction

1.3. Master-slave replication (data synchronization)

(1) Establish a connection phase

(2) Data synchronization stage

(3) Command propagation stage


1. Stand-alone mode and master-slave mode

Redis is an in-memory database of high-performance key-value key-value pairs written in C language, which can read and write 100,000 times per second. There are roughly four modes of Redis operating environment, namely: stand-alone mode, master-slave mode, sentinel mode, and cluster mode.

1.1, stand-alone mode

Redis stand-alone mode is the simplest operating environment. You only need to download a Redis service and start it. Redis can go to [ https://github.com/tporadowski/redis/releases ] to download the corresponding version. After the following is completed, the unzipped directory looks like this:

In stand-alone mode, you only need to run the [redis-server.exe] file in the windows system to start the redis service.

The above is the stand-alone mode of Redis, and only one Redis service runs.

Disadvantages of stand-alone mode:

  • When there are a large number of read and write requests, a Redis service may not be able to handle them.
  • When the stand-alone Redis service goes down or fails, the entire application system becomes unusable.

1.2. Master-slave mode

(1) Principle of master-slave mode

The master-slave mode adopts the idea of ​​[read-write separation]. One Redis service is specially used to provide external write operations. The Redis service that supports write operations is called: master node; other Redis services provide external read operations, which can only read The Redis service is called: slave slave node. The master-slave mode architecture diagram is roughly as follows:

In the master-slave mode, it can be a [1-to-N] mode or a [tree-like] mode. In general, the [1-to-N] mode is used instead of the [tree-like] mode.

The master-slave mode, to a certain extent, improves the availability of services. When the master master node is down, the slave slave nodes can still provide external read services, but the write operations in the system are not available.

(2) Master-slave mode construction

To build a [1 master 2 slave] mode, three Redis services need to be started, one master node and two slave nodes.

Redis master-slave mode
node name IP address port
master master node 127.0.0.1 6379
slave from the node 127.0.0.1 6380
slave from the node 127.0.0.1 6381

  • Build the master master node: copy a redis configuration file, the name is: redis6379.conf, modify the port to: 6379.
# 启动master结点
redis-server.exe redis6379.conf

As follows:

  •  Build a slave node: copy a redis configuration file, the name is: redis6380.conf, modify the port to: 6380.
  •  Build a slave node: copy a redis configuration file, the name is: redis6381.conf, modify the port to: 6381.

Add the [slaveof <masterip> <masterport>] command to the above two slave node configuration files to configure the associated master node. Among them, <masterip> indicates the IP address of the master node, and <masterport> indicates the port of the master node (note: if you want to cancel the master-slave relationship, you can use the [slaveof no one] command).

# 从结点关联master主结点
slaveof 127.0.0.1 6379

Start two slave nodes separately, as follows:

  • Connect to the master node [redis-cli.exe -h 127.0.0.1 -p 6379] and execute a write command.

  • Check whether the data just written exists in the two slave nodes.

 At this point, the master-slave mode of Redis is set up.

1.3. Master-slave replication (data synchronization)

In the master-slave mode, the master master node can provide [read and write operations], but the slave node can only provide [read operations], and if a write operation is performed, an error will be reported. So how do the master and slave master-slave nodes ensure data consistency? ? ?

In the master-slave mode, the data enters the master node first, so in order to ensure that the slave nodes also have the same data, at this time, it is necessary to copy the data in the master node to all slaves from inside the node.

Master-slave replication has three steps:

  • The first step: master and slave establish [communication connection stage].
  • Step 2: Master and slave perform [data synchronization phase].
  • Step 3: The master and slave perform the [Command Propagation Phase].

(1) Establish a connection phase

When the slave node is started, it will establish a socket connection to the master node according to the [slaveof] information in the configuration file. When the socket connection is successfully established, the master and slave nodes can communicate normally. .

In order to ensure the stability of the communication between the master and slave nodes, the slave node will periodically send a ping command to the master master node. If the master node responds to the PONG from the slave node, it means that the master and slave nodes Communication between them is normal.

If the access password is set, authentication is required at this time. After the authentication is passed, the master and slave nodes can perform data synchronization replication at this time.

You can use the [info Replication] command to view the master-slave replication information of the node.

(2) Data synchronization stage

The previous step has ensured that the master and slave can communicate normally, but at this time the data in the master and slave are not consistent, so it is necessary to copy the data in the master to the slave, and the slave connects from the node to the master. Click to send the psync command for data synchronization. There are two copy methods: [full copy] and [incremental copy].

  • full copy

The slave sends the psync command to the master for full copy. After the master receives the full copy request, it will open a buffer at this time. The buffer area records the new write command operation at the moment, and executes the bgsave command to generate an RDB file in the background. The generated RDB file is sent to the slave node, and the slave node receives the RDB node, first clears the data in the slave node, and then executes the bgsave command to import the data of the RDB file. In addition, after the RDB file recovery is completed, send commands to the master node, the master will send all the commands in the buffer to the slave node, and the slave node executes these commands to complete data synchronization.

  • Incremental replication

Full copying consumes too much performance, so Redis proposes incremental copying, which means copying only part of the data each time.

TODO: Principles to be added.

(3) Command propagation stage

In the second step, the data of the master and slave nodes have been synchronized, but this situation will inevitably occur during the synchronization process, that is, the data has just been synchronized, but at this time a new write command is executed. At this time, it is necessary to Continue to copy the newly written data in the master to the slave node. This process is adopted: the command propagation mechanism, the master node will propagate the new write command to the slave node, and then the slave node executes the write command to complete data synchronization.

So far, the stand-alone mode and master-slave mode in Redis have been introduced.

In summary, this article is over, mainly introducing the stand-alone mode and master-slave mode of Redis environment construction.

Guess you like

Origin blog.csdn.net/qq_39826207/article/details/129719820