Recently, I changed my unit redis and nginx, For a rookie like a blogger who has only heard of his name before , Deep feeling to head big , After several days of research , yes redis and nginx Have a preliminary understanding , Now let's take a look redis Common patterns of : Single point mode , Master slave mode , Sentry mode and cluster mode .

【 Single point mode 】

   
Single point mode is also called single node mode , It's the simplest Redis pattern , That's one redis example , If you just test the cache or small program yourself , The amount of data is small , Just make a small one KEY/VALUE Type database , Quite enough . In a nutshell , Single node mode is only suitable for understanding learning Redis, Play for yourself OK.

【 Master slave mode 】

The concept of master-slave model :

   The master-slave model is N individual redis example , It could be 1 main N from , it's fine too N main N from (N main N The subordinate mode is not the master-slave mode in the strict sense , We will talk about the following cluster mode ,N main N From then on N+N individual redis example .)

        One of the functions of master-slave mode is to back up data , So when a node is damaged ( Refers to irrecoverable hardware damage ) Time , The data is backed up , It's easy to recover .

   Another function is load balancing , If all clients access a node, it will definitely affect the performance Redis work efficiency , With master and slave , The query operation can be completed through the query from the node .

   Understanding of master-slave model :

  1. One Master There can be multiple Slaves, It could be 1 main N from .

  2. In default configuration ,master Nodes can read and write ,slave Nodes can only be read , Write operations are prohibited (readonly).

  3. Don't modify the configuration slave Nodes support write operations , meaningless , Reason one , The data written will not be synchronized to other nodes ; The second reason , When master After the node modifies the same data ,slave The data of the node will be covered .

  4.slave The node hung does not affect other nodes slave Read sum of nodes master Read and write of node , After restart, the data will be removed from the master The nodes are synchronized .

  5.master After the node hangs up , No impact slave Reading of nodes ,Redis The write service will no longer be available ,master After the node starts Redis Write services will be re provided to the outside world .

  6. Special notes : In this mode ,master After the node hangs up ,slave It's not going to be an election master.

   Explain the situation with password , When master When a node sets a password :

* Client access master A password is required
* start-up slave A password is required , It can be configured in the configuration
* Client access slave No password is required
   To sum up , The client only needs to configure one password parameter , and redis Two parameters need to be configured in the configuration file .

   namely :

  Redis Server configuration file :

  masterauth "chrdw,hdhxt!"

  requirepass "chrdw,hdhxt!"

   Client profile :

  jedis-cluster.password=chrdw,hdhxt!

   Note that there are no quotation marks .

  2.1 Disadvantages of master slave node

   The disadvantages of the master-slave model can be seen from the above description :

  master After the node hangs up ,redis We can't provide writing services to the outside world , Because of the rest slave Can't be master

   This shortcoming has a great impact , Especially for the production environment , You can't stop serving for a moment , Therefore, the general production environment is not only the master-slave mode . So there's the following sentinel pattern .

【 Sentinel mode 】

Sentinel mode is also called sentinel mode sentinel pattern
,sentinel The Chinese meaning of Sentry is sentry , guard . That is to say, since in the master-slave mode , When master After the node hangs up ,slave Nodes cannot elect one actively master Nodes come out , Then I'll arrange one or more sentinel To do it , When sentinel find master After the node hangs up ,sentinel From the slave Re elect one in the election master.

   yes sentinel Understanding of patterns :

  1.sentinel The mode is based on the master-slave mode , If there's only one Redis node ,sentinel It doesn't make any sense .

  2. When master After the node hangs up ,sentinel Will be in slave Choose one of them as the master, And modify their configuration files , other slave The configuration file of will also be modified , such as slaveof The property points to the new master.

  3. When master After node restart , It will no longer be master, But as slave Receive new master Synchronization data of nodes

  4.sentinel Because it is also a process that may hang up , therefore sentinel It will also start many to form one sentinel colony .

  5. When password is configured in master-slave mode ,sentinel The configuration information is also modified to the configuration file synchronously , Don't worry .

  6. One sentinel or sentinel A cluster can manage multiple masters and slaves Redis.

  7.sentinel You'd better not talk to me Redis Deployed on the same machine , Otherwise Redis After my server hung up ,sentinel It's dead, too .

  8.sentinel Monitored Redis Clusters will define one master name , The name stands for Redis Clustering master Redis.

   When using sentinel When it comes to mode , The client should not connect directly Redis, It's about connecting sentinel Of ip and port, from sentinel To provide specific services Redis realization , It's like this master After the node fails ,sentinel It's going to be able to sense and bring the new master Nodes are provided to users .

  Sentinel It also supports clusters , Use only a single sentinel Process to monitor redis Clustering is unreliable , When sentinel After process down ,sentinel There is also a single problem . So it is necessary to sentinel colony , There are several advantages to this :

  1. If there's only one sentinel process , If this process runs in error , Or network congestion , That will not be possible redis Active standby switch of cluster ( Single point problem ).

  2. If there are more than one sentinel,redis The client can connect to any one at will sentinel To get information about redis Information in cluster .

  3.sentinel Cluster itself also needs majority mechanism , that is 2 individual sentinel Process time , Hang up one and the other won't work .

  Sentinel colony :

   Different from other clusters , You don't have to set up other settings Sentinel Your address ,Sentinel Processes can automatically discover other instances that are monitoring the same primary instance by publishing and subscribing Sentinel. Be a
Sentinel Found a new one Sentinel Time , It will bring the new Sentinel Add to a list , This list is saved Sentinel
Known , Monitor all other servers on the same master server Sentinel.

  Sentinel Data mining in cluster Sentinel Not at the same time failover( Fail over or Fail over ) The same master, Be the first to do it failover Of Sentinel If it fails ( Configured above failover-timeout), The other one will do it again failover, and so on .

   When Sentinel Will be a slave Elected as master And send SLAVE OF NO
ONE after , Even if other slave It's not for the new one master Reconfigure yourself ,failover It's also considered a success .

   In the process of the above transition , If you restart at this time old
master, be redis The cluster will be in a no load state master state , You can only modify the configuration file manually at this time , Then restart the cluster .( Don't do such a stupid operation in production , Otherwise, you will cause the whole application cluster to fail to start .)

  Master-Slave After switching ,Sentinel Will rewrite master,slave and sentinel Of conf configuration file .

   Once a Sentinel Successfully to a master It's been done failover, It's going to be about master The latest configuration of will be broadcast to other users sentinel, Others Sentinel Then update the corresponding master Configuration of .

  Sentinel The model can basically meet the needs of general production , High availability . But when the amount of data is too large for a server to store ( This is usually a memory bottleneck , I have Redis The stress test of ,Redis At high concurrency , In the case of large amount of data CPU The consumption of other resources is not high , The main pressure is memory .) Time , Master slave mode or sentinel Patterns can't meet the needs , At this time, the stored data needs to be partitioned , Store data in multiple locations Redis In the example , That's what we're going to talk about .

【 Cluster mode 】

Used by my project team cluster pattern , The project has many advantages in the production environment 8 individual Redis colony , Per cluster dbsize It's all in the tens of millions . This amount of data is obviously not enough for sentinel mode .

  cluster The emergence of is to solve the problem of single machine Redis The problem of limited capacity , take Redis According to certain rules, the data are distributed to multiple machines . yes cluster Some understanding of :

   One Redis Cluster contains 16384 Hash slot (hash slot), Every key in the database belongs to this 16384
One of the hash slots , Each node in the cluster is responsible for processing some hash slots .

   For example, a cluster has three primary nodes , among :

   node A Responsible for handling 0 No. to 5500 Hash slot .

   node B Responsible for handling 5501 No. to 11000 Hash slot .

   node C Responsible for handling 11001 No. to 16384 Hash slot .

   This method of distributing hash slots to different nodes makes it easy for users to add or delete nodes to the cluster . for example : If the user adds a new node D Add to cluster , Then the cluster only needs to connect the nodes A ,B
, C Some slots in are moved to nodes D That's it .

   If the user wants to remove the node from the cluster A , Then the cluster only needs to connect the nodes A All hash slots in are moved to the node B And nodes C , Then remove the blank ( Does not contain any hash slots ) Node of A
That's it .

   Here's something to note , If the cluster is 5 main 5 from , So is the master node 16384 individual hash
slot, But not because of the increase of primary nodes slot Also increased . When we split the slot , Try to divide the slots equally among the main nodes . Because of one key In which slot , It's based on key Of CRC16 On value module 16384 It is calculated according to the obtained value .

  2.Redis The cluster uses the master-slave replication function for nodes : Each node in the cluster has 1 Gezhi N Copies (replica),
One of the replicas is the main node (master), And the rest N-1 Copies are slave nodes (slave).

   We know that in cluster mode ,1 main N From time to time , When the master node fails , Slave node through heartbeat monitoring mechanism , It will become the main node ( It's set at this time readonly It will fail ), So when it comes to deployment , The master and slave nodes should be deployed on different machines , At this time, if the server of the primary node goes down , After successful election, the slave node will continue to undertake the task of reading and writing .

  3.Redis The nodes of the cluster communicate with each other Gossip Protocol communication .

  4. current Redis Cluster does not support NAT Environment or IP, Environment for port remapping .

  cluster This mode is suitable for large amount of data cache requirements , When the amount of data is not very large sentinel that will do .

  Redis So much for the introduction of various models , Welcome to send message and discuss with me .

Technology