Home  >  Article  >  Database  >  Redis explains master-slave replication and sentry mode

Redis explains master-slave replication and sentry mode

coldplay.xixi
coldplay.xixiforward
2021-01-26 09:45:332131browse

Redis explains master-slave replication and sentry mode

Recommendation (free): redis

##Article Directory

    Master-slave replication
    • Command
    • Configuration
  • Copy principle
    • Full copy
    • Incremental copy
    • Test
  • Nested master-slave
  • Sentinel mode
    • Configuration Sentinel
    • Test
##Master-slave replication


Master-slave replication

refers to copying the data of one Redis server to other Redis servers. The former is called the master node Master, and the latter is called the slave node Slave. It can only be copied one-way from the Master to Slave, generally Master mainly performs writing operations, and Slave mainly performs reading operations, achieving separation of reading and writing.

Function

Data redundancy: Master-slave replication realizes hot backup of data, which is a kind of data redundancy besides persistence. Way.
  1. Failure recovery: When a problem occurs on the master node, the slave node can provide services to achieve rapid failure recovery; it is actually a kind of service redundancy.
  2. Load balancing: Based on master-slave replication, combined with read-write separation, the master node can provide write services and the slave nodes can provide read services (that is, when writing Redis data, the application connects to the master node and reads Redis data When the application connects to the slave node), the server load is shared; especially in scenarios where there is less writing and more reading, sharing the read load through multiple slave nodes can greatly increase the concurrency of the Redis server.
  3. Cornerstone of high availability: In addition to the above functions, master-slave replication is also the basis for the implementation of sentinels and clusters. Therefore, master-slave replication is the basis of Redis high availability.
Command

##CommandFunctionslaveof host port will cause the slave server to turn off the replication function and transition from the slave server back to the master server. The original synchronized data set will not be discarded. info [, you can make the command return only a certain part of the information:

Configuration

Take a single machine with multiple services as an example (normally multiple machines with multiple services, but I only have one server)

First of all, each redis client The default is the host, which can be viewed through the info replication command.
Redis explains master-slave replication and sentry mode

Then we now need to open three clients at the same time to simulate one master and two slaves, so we need to modify the configuration:

  1. Modify the port number
  2. Modify pid name
  3. Modify log name
  4. Modify rdb name
  5. Set host connection (optional, use command line)

First Copy two configuration files as the slave configuration, and the master can use the default.
Redis explains master-slave replication and sentry mode
Take redis80.conf as an example to modify the above five configuration points in sequence. For 81, only the first four points are modified.
Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode## Then start them (79, 80, 81)

Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode Set master and slave:

    80 is set in the configuration file (permanent), view it directly:

  1. Redis explains master-slave replication and sentry mode##81 If there is no configuration, you can manually set the command line

  2. Redis explains master-slave replication and sentry mode
  3. View 79 (master) at this time:


Redis explains master-slave replication and sentry mode

Copy principle


Full copy

Every time the slave connects to the host, it will copy in full, copying all the data from the host to the slave.

Incremental copy

After the slave machine is connected to the master machine, the data updated later by the master machine will be synchronously updated to the slave machine only for this part of the data.

Test

The slave is read-only by default and will incrementally copy the data of the synchronization host:

  1. Redis explains master-slave replication and sentry mode
    Redis explains master-slave replication and sentry mode
    Redis explains master-slave replication and sentry mode Host downtime:

  2. Redis explains master-slave replication and sentry mode
    Redis explains master-slave replication and sentry mode
    Redis explains master-slave replication and sentry mode
    Redis explains master-slave replication and sentry mode# 3 .Slave machine downtime:
    Redis explains master-slave replication and sentry mode

    Redis explains master-slave replication and sentry mode
    Redis explains master-slave replication and sentry mode
    Redis explains master-slave replication and sentry mode
    Redis explains master-slave replication and sentry mode
    Redis explains master-slave replication and sentry mode#embedded Set master and slave

As shown in the figure 79 is the host of 80, and 80 is the host of 81. This is a nested master-slave relationship.
Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode

Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode

#Sentinel Mode


The above 80 upper position and nested master-slave are all entered by us on the command line manually.

The purpose is to avoid write operations after the host is down. During the window period, these require manual intervention.
Sentinel will run independently as an independent process. The principle is that Sentinel monitors multiple running Redis servers by sending commands and waiting for the Redis server to respond. If Sentinel detects that the host is offline, it will select a slave machine to "upper" (automatic fault migration) to become the new host. If the original host comes online, the original host will become the slave of the new host. The principle is to notify other servers through the publish and subscribe model, modify the configuration file, and thereby switch hosts.

Redis explains master-slave replication and sentry mode What if Sentinel goes down? Multiple sentinels can be used to monitor each other.

Redis explains master-slave replication and sentry mode
The picture is taken from https://www.jianshu.com/p/06ab9daf921d, intrusion and deletion

  • Subjective offline(Subjectively Down, SDOWN for short) refers to the offline judgment made by a single sentinel instance on the server.
  • Objective Down(Objectively Down, referred to as ODOWN) refers to multiple sentinel instances making subjective offline judgments on the same server, and through SENTINEL is-master-down- After the by-addr commands communicate with each other, the server is offline judged.
When the host objectively goes offline, Sentinel will vote for a new host (

The specific algorithm is omitted), perform automatic failover (failover), and notify other servers to switch through publish and subscribe. host.

Configuring Sentinel

First, there is a detailed annotated sentinel configuration in the installation directory:


Redis explains master-slave replication and sentry mode Create a new sentinel.conf to monitor 6379, The rest can be defaulted:

Redis explains master-slave replication and sentry mode Start Sentinel:

Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode

##Test

Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode
Redis explains master-slave replication and sentry mode# Multi-sentinel mode, Configure configuration files for different ports to open multiple Sentinel clients, and then follow the same pattern (
lazyRedis explains master-slave replication and sentry mode )

Transform the current server into a slave server of the specified server. If it is already a slave, it stops synchronizing the old master server, discards the old data set, and starts synchronizing the new master server. SLAVEOF NO ONE
section]The INFO command returns information about the Redis server in a format that is easy to understand and read. various information and statistical values. By giving the optional parameter section

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

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete
Previous article:Redis key points analysisNext article:Redis key points analysis