This is the 23rd day of my participation in the More text Challenge. For more details, see more text Challenge


Related articles

Redis combat summary: Redis combat


preface

By default, each Redis server is the primary node; For personal server performance reasons, all of the operations below are single-machine cluster concepts! This is not what you would do in practice, but you would use sentry mode to monitor!

A concept,

  • Master-slave replication refers to the replication of data from one Redis server to another Redis server. The former is the master node (master/leader) and the latter is the slave node (slave/follower). Data is replicated one-way from the primary node to the secondary node. Master mainly writes and Slave mainly reads.

  • Main functions:

    • (1) Data redundancy: Master/slave replication implements hot backup of data. It is a data redundancy method besides persistence.

    • (2) Fault recovery: When the primary node is faulty, the secondary node can provide services to achieve rapid fault recovery. It’s actually a redundancy of services.

    • (3) Load balancing: On the basis of primary and secondary replication, with read and write separation, the primary node can provide the write service, and the secondary node can provide the read service (that is, the application connects to the primary node when Redis data is written, and the application connects to the secondary node when Redis data is read), and the server load is shared. Especially in the scenario of less write and more read, the concurrent Redis server can be greatly improved by sharing the read load among multiple secondary nodes.

    • (4) Cornerstone of high availability (cluster) : in addition to the above role, the master-slave replication or sentinel and cluster can implement the foundation, so said master-slave replication is the foundation of Redis high availability.

Ii. Environment Configuration (Single-machine cluster)

  • 1. Basic view command:

127.0. 01.:6379> ping # test whether the connection is successful! PONG127.0. 01.:6379> info replication # replication role:master # replication role:master0# Number of slave machines is0Unique id master_replid2 master_replid: b9565cf2edea63b7e9860f3ef1a170d59ff7a4d4 # :0000000000000000000000000000000000000000# Master_repl_offset master_repl_offset0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
Copy the code
  • 2. Start three services:

    • ① Copy three configuration files:

    • ② Modify the following configurations:

      Port:

      Pid name:

      Log file name:

      Dump. RDB name:

      ③ Start and view all:

      Check all Redis ports: indicates successful startup!

Iii. One master and two slave (single machine test)

  • 1, mobile phone!! !

127.0. 01.:6380> ping
PONG
127.0. 01.:6380> slaveof 127.0. 01. 6379# Let the machine recognize6379The machine for big brother! OK127.0. 01.:6380> info replication # replication role:slave # master_host:127.0. 01.# Host IP master_port:6379Master_link_status :up master_last_IO_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:14
slave_priority:100
slave_read_only:1
connected_slaves:0
Copy the code
  • 2, the second machine is the same, let’s look at the host information:

127.0. 01.:6379> ping
PONG
127.0. 01.:6379> info replication # replication role:master2Slave0: IP =127.0. 01.,port=6380,state=online,offset=56,lag=1Slave1: IP =127.0. 01.,port=6381,state=online,offset=56,lag=1
Copy the code

The above content proves that we configured successfully!

  • 3, note: this configuration through the command is’ one-time ‘, if the machine down, power, etc., you need to re-recognize the mobile phone!

In practice, we all modify the specified configuration through the configuration file! The diagram below:

You can modify the above configuration to achieve the configuration of the master and slave!

  • 4, test read and write operations:

(1) Write from the host and read from the machine

Write:

Read:

That proves master/slave replication is successful!

② If the host is disconnected

The slave machine can read data normally:

View the information of the slave:

Prove, although the host is disconnected, but the slave machine can still read the original data!

③ Reconnect the disconnected host

The slave machine can also be connected to the host normally, because the configuration, will automatically find the host.

④ What if the slave is disconnected and reconnected?

Proof: if the slave is disconnected and reconnected, it will not be automatically connected to the host! Because our configuration is written on the slave, and it is written by command, it will reset on reboot!

⑤ Do you write from function?

Slave machine can only read, not write!

  • 5. Replication principle:

    • After the Slave is successfully started and connected to the master, it sends a sync command

    • After receiving the command, the Master starts the background saving process and collects all the received commands to modify the data set. After the background process completes, the Master transfers the entire data file to the slave and completes a full synchronization.

    • ** Full copy: ** The Slave service saves the database file data and loads it into memory after receiving it.

    • Incremental replication: The Master continues to pass all the new collected modification commands to the slave, completing the synchronization, but as soon as the Master is reconnected, a full synchronization (full replication) will be automatically performed! Our data must be visible from the machine!

  • 6. Layer upon layer links

At this time, we can complete the master-slave replication!

  • Plotting to usurp the throne

If the host is disconnected, we can use

 SLAVEOF no one
Copy the code

Make yourself a host! Other nodes can then manually connect to the latest master node (manually)! If this time the original boss is repaired, then reconnect to become a little brother!!

Pay attention!!

You can also use this command to directly make yourself the boss!

Four,

  • Generally speaking, to use Redis in a project, only one Redis should not be used (down) for the following reasons:

    • 1. From the structure, a single Redis server will have a single point of failure, and a single server needs to handle all the request load, the pressure is large;

    • 2, from the capacity, a single Redis server memory capacity is limited, even if a Redis server memory capacity of 256G, can not use all the memory as Redis storage memory, generally speaking, a single Redis maximum use of memory should not be 20G.

  • Master/slave replication, read/write separation! 80% of the time you’re reading! Relieve the pressure on the server! Often used in architecture! One master and two followers!

  • Master-slave replication is a must as long as you’re in a company, because it’s impossible to use Redis on a standalone basis in a real project!


I see no ending, but I will search high and low

If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah