Redis cluster

BugMaker-shen2022-06-23 18:11:53

One 、 Cluster concept

Peak bottlenecks in business development :

  • redis Services provided OPS You can achieve 10 ten thousand / second , Current business OPS Already achieved 20 ten thousand / second
  • The memory capacity of a single machine reaches 256G, Current business needs memory capacity 1T

At this time, cluster can be used to quickly solve the above problems

Cluster architecture : Cluster is to use the network to connect several computers , And provide a unified way of Management , Make it present the service effect of single machine

The role of clusters :

  • Disperse the access pressure of a single server , Load balancing
  • Disperse the storage pressure of a single server , Achieve scalability
  • Reduce the possibility of business disaster caused by single server downtime , Achieve high availability

 Insert picture description here

Two 、 Cluster data storage design

  • adopt hash Algorithm design , To calculate the key Where it should be saved
  • Cut all storage space into 16384 Share , Each host holds a part of , Each represents one storage space ( Slot ), Is not a key The preservation space of , This storage space can hold many key
  • take key According to the calculated results, put them into the corresponding storage space

 Insert picture description here
Suppose we now add a machine , How should the stored data and space be allocated ?

 Insert picture description here

A machine is divided into slots for new machines , The so-called add machine and delete machine , Change the machine stored in the slot

3、 ... and 、 The design of communication within the cluster

  • Communication databases , Save the number of slots in each library
  • One hit , Go straight back to
  • A miss , Tell me the exact location , Hit twice at most

 Insert picture description here
At this point, a client is looking for key, First it will pass hash The algorithm calculates key Corresponding slot number , Then find the corresponding machine on the hash ring , Check if there are corresponding slots on this machine ( Due to the possible addition of machines 、 Delete , Slots on the machine will be added to different machines )

If this machine has key Corresponding slot , Direct search returns ; without , Then find the slot number of each machine and the record of the machine , Then go to the corresponding machine to get it

Four 、 Set up the cluster

Cluster To configure

Set join cluster, Become one of the nodes

cluster-enabled yes|no

cluster Profile name , This file is automatically generated , And it's only used to quickly find files

cluster-config-file <filename>

Node service response timeout , Used to determine whether the node is offline or switched to slave

cluster-node-timeout <milliseconds>

master Connected slave Minimum quantity

cluster-migration-barrier <count>

To configure 3 masters—3 slaves

To write redis-6379.conf

 Insert picture description here
from redis-6379.conf obtain redis-6380.conf、redis-6381.conf、redis-6382.conf、redis-6383.conf、redis-6384.conf, Respectively as 3 individual master and 3 individual slave The startup profile for

 Insert picture description here
Start all of master and slave

 Insert picture description here
 Insert picture description here
This is also a node by node , We need to connect them , stay src There is a redis-trib.rb Executable program of ( This file needs to be installed ruby)

 Insert picture description here

# there n Express 1 individual master Corresponding n individual slave
# hinder ip:port Express master and slave Information about ,master and slave The quantity needs to be the same as the previous one n Corresponding 
# hypothesis n=1, Yes 6 Group ip:port, It means before 3 A for master, after 3 A for slave, Realization 1 Lord 1 from 
# hypothesis n=2, Yes 9 Group ip:port, It means before 3 A for master, after 6 A for slave, Realization 1 Lord 2 from 
./redis-trib.rb create --replicas n ip1:port1 ip2:port2 ....

Write 6 Group ip:port, Execution instruction , Generate 3 Group 1 Lord 1 from

 Insert picture description here
Before generating multiple groups of master-slave structures , Let's take a look first data Catalog ( stay redis- port .conf Middle configuration ) The configuration file of the generated node nodes- port .conf
 Insert picture description here

We type in yes, Generate 3 Group 1 Lord 1 from
 Insert picture description here
cluster When connected together , The cluster is configured , here nodes- port .conf And it changed . Let's see master 6379 Corresponding node configuration file
 Insert picture description here

Let's see 6379master Prompt information of the terminal

 Insert picture description here

5、 ... and 、 Cluster usage practice

1. Use the cluster to store data

 Insert picture description here
We're connected to 6379 Port of redis After the server , Want to be in 6379 Put data on the server , It can be done by CRC Algorithm and module 16384 To calculate the itheima This data should be placed in 5798 Slot No , And this slot is 6380 On the machine , We are not allowed to put 6379 On the machine

We use -c Parameter login server , This parameter is dedicated to cluster operation

 Insert picture description here

We are still connected 6379 The server , When you put the data , Give tips , Although we are 6379 Put data on , But through CRC Algorithm and module 16384 To calculate the itheima This data should be placed in 5798 Slot No , So we redirected to 6380 machine

We go through -c Parameter login 6382 The machine slave The server fetches data
 Insert picture description here
The prompt message tells us , Redirect to 6380 On the machine 5798 From slot No . As long as we log in to any machine in the cluster , Can be in 6380 On the machine 5798 Take out slot No , If we log in 6380 The machine will not have redirection information , Instead, you take the data directly

2. slave The impact of dropping the line on the cluster

The biggest advantage of a cluster is not that it can store data on multiple machines , Instead, the cluster can solve the business disaster caused by downtime

Let's stop first 6382 slave1

 Insert picture description here
Let's see slave1 Corresponding master1,master1 Find out slave1 10s No response within , Mark it as offline
 Insert picture description here
At this time, the others in the cluster master and slave Will receive master1 The notice of ,slave1 Offline

 Insert picture description here
Then we let slave1 Back online

 Insert picture description here
After going online and master1 Just resynchronize ,master1 Then inform others master and slave of slave1 Online information of , The other machines are cleared slave1 End of line marking for

 Insert picture description here

summary : slave After offline , It corresponds to master It will be marked as offline , At the same time, other machines in the cluster are notified of this slave Offline ; When it goes online , And then synchronize the data with it , Then notify the other machines in the cluster of this slave Online information of

3. master The impact of dropping the line on the cluster

Let's stop. master1

 Insert picture description here
Here is with master1 Corresponding slave1 The operation of

 Insert picture description here
We use it cluster nodes Check the node information , Found to have 4 individual master, among 6379 Marked as fail

 Insert picture description here

We now restart 6379

 Insert picture description here
6379 Request and 6382 Data synchronization
 Insert picture description here
Reuse cluster nodes Check the node information ,6379 The state of the from master fail Changed to slave

 Insert picture description here
summary : When master1 After offline , Corresponding slave1 Will try to connect , After the timeout, you become a new master, Then notify the other machines in the cluster ,master1 Go offline and become master The news of ,master1 The state of is marked as master fail;6379 When it comes back online , It's going to be slave, And with their own master Data synchronization

Cluster Node operation command

View the cluster node information

cluster nodes

Enter a slave node redis, Switch its master node

cluster replication <master-id>

Find a new node , Add master node

cluster meet ip:port

Ignore one without slot The node of

cluster forget

Manual failover

cluster failover

thank
Similar articles

2022-06-23