当前位置:网站首页>redis-6. Redis master-slave replication, cap, Paxos, cluster sharding cluster 01

redis-6. Redis master-slave replication, cap, Paxos, cluster sharding cluster 01

2022-06-13 07:13:00 Floating across the sea

redis

redis stand-alone

stay redis stand-alone , A single node , Problems with single instances

  • A single point of failure ( The physical machine hung up )
  • Limited capacity
  • Under great pressure

Solution

AKF

Yes XYZ Three axes , Only one can happen , Multiple dimensions can occur together

AKF Cubes are also called scala cube, It's in 《The Art of Scalability》 First proposed in a Book , It aims to provide a systematic expansion idea .AKF The system expansion is divided into the following three dimensions :

X Axis : Directly and horizontally copy the application process to expand the system .
Y Axis : Split the functions to expand the system .
Z Axis : Expand the system based on user information .
No matter what redis,mysql still kafka Can be extended based on this concept

 Insert picture description here
adopt akf To solve the problem will extend to other problems
problem 1 Data consistency issues
programme 1: All nodes are blocked , Until all numbers are consistent , But this situation will lead to redis Reduced availability of , More than one side is to solve the availability problem
 Insert picture description here
programme 2: Tolerate the possibility of data loss , Return directly to the host redis ok, Insert standby asynchronously redis, This situation must tolerate data loss
 Insert picture description here
programme 3: How to guarantee not to lose , The middle price can be inserted kafka, Achieve final consistency of data . There are many possibilities : Before reaching final consistency , Inconsistent data may be obtained
 Insert picture description here
problem 2 If the Lord redis Hang up , Make the whole service unavailable
The main equipment : The client does not participate in the business , Only as a standby machine
Master-slave : Master slave copy , You can retrieve data from the library ( Emphasize the concept )
Generally, it is highly available to the primary user HA, If the Lord redis Hang up , We can do it manually To prepare 1redis Top up , As a host , Let prepare 2redis Continue to be an assistant ,
 Insert picture description here
But ah , brothers , We are fucking programmers , Every time this kind of fault occurs, it is not troublesome , It will be very low And there is still time to recover , This is when the monitoring program appears , But a monitoring program will also hang up , Therefore, the monitor itself should also be a cluster that has become more and more .

Multiple monitors : Multiple monitors are waiting for a master , If redis Hang up , Monitor to find him , Because there are network delays , How to judge by multiple monitors redis Did you hang up or not ? Like monitor 1 Sending hung up , monitor 2 Sending hung up , monitor 3 No, I didn't listen , How to deal with this situation ? Strong consistency cannot be used at this time , Only some machines need to give the result judgment , that ! How many are there in one part ?

The three monitors produce different results , Create competition . There will be a problem : Network partition ( Split brain ), Different clients get different data , External performance is data inconsistency .
If 2 A monitor said redis Hang up ,1 A monitor said redis Still alive , this 2 The forces monitored are 2. The force is 1 There is no decision-making power for monitoring . At this time, the external performance is not an ambiguous state .
therefore , When the faction reaches n/2+1, That is, half way through , You can solve your mental problems . Therefore, the cluster generally uses an odd number of servers .
Why is it an odd number ?
We use it 3 Taiwan and 4 Taiwan comparison , They are only allowed to hang one . but 4 Table ratio 3 It's easier to hang one , and 4 The price is higher . So the first 4 There is no need for a platform .
 Insert picture description here

CAP

A term is used to explain :
Master-slave : The client can access the master You can also access the backup
The main equipment : The client will only access the primary The standby machine is used to replace the main hook
CAP: Uniformity Usability Partition tolerance Two at most Not all of them

 Insert picture description here

Master slave copy

Redis Use default asynchronous replication , It is characterized by low latency and high performance , It's the vast majority Redis The natural replication pattern of use cases . however , from Redis The server asynchronously confirms its slave master Redis The amount of data received by the server in a cycle .
Conditions are limited , I started three on one host redis
 Insert picture description here

5.0  Previous master-slave replication operations 
127.0.0.1:16379> help SLAVEOF

  SLAVEOF host port
  summary: Make the server a replica of another instance, or promote it as master. Deprecated starting with Redis 5. Use REPLICAOF instead.
  since: 1.0.0
  group: server
5.0  Subsequent master-slave replication operations 
127.0.0.1:16379> help REPLICAOF

  REPLICAOF host port
  summary: Make the server a replica of another instance, or promote it as master.
  since: 5.0.0
  group: server

I'm here at this time redis16380 Set the main database above

REPLICAOF 127.0.0.1 16379

Report errors ! Thought I was 16379 Added above password . Therefore, it is impossible to link
 Insert picture description here
To configure... Here 16380.conf On the configuration 6379 Password
 Insert picture description here
 Insert picture description here
Repeat the previous link operation

16379.log

 Insert picture description here
16380.log
 Insert picture description here

127.0.0.1:16379> set k1 zjj
OK

By default 16380 Is read-only

127.0.0.1:16380> get k1
"zjj"
127.0.0.1:16380> set k1 aaa
(error) READONLY You can't write against a read only replica.

I'm going to modify 16381 follow 16379, At this time, verify all the above problems , In the case of following ,redis Followers will flushall My own library

127.0.0.1:16381> set k2 aaa
OK
127.0.0.1:16381> get k2
"aaa"
127.0.0.1:16381> 
127.0.0.1:16381> REPLICAOF 127.0.0.1 16379
OK
127.0.0.1:16381> 
127.0.0.1:16381> get k1
"zjj"
127.0.0.1:16381> get k2
(nil)

At this time I shutdown 16381

127.0.0.1:16381> SHUTDOWN

16379 newspaper 16381 The link doesn't go up , At this time, we will redis 16381 Restart up , that 16379 How to synchronize your data
 Insert picture description here
stay redis 16381 Continue writing in case of hang up 16379

127.0.0.1:16379> set k2 aaa
OK
127.0.0.1:16379> set k3 bbb
OK

Restart 16381 redis-server ./6381.conf --replicaof 127.0.0.1 6379
After reboot , It will still synchronize the increment during the period of suspension .

127.0.0.1:16381> keys *
1) "k1"
2) "k3"
3) "k2"

 Insert picture description here

  • masterauth < master-password >

Set the password

  • replica-serve-stale-data yes

When in the process of synchronization , Or slave and master The connection between them has been broken ,replica Can I still provide read requests ?
By default ,replica You can't provide a request , Go straight back to error, But when the following configuration is turned on , The old data can be used to respond to the request .

  • replica-read-only yes

By default ,slave Is read-only , Prevent misoperation from writing bad data , But if you have to be writable , It can also be opened through the following configuration .

  • repl-diskless-sync no

about master End :
The following configuration can be enabled , After opening master Will no longer be generated rdb file , Directly encode the memory data and send it to replica

  • repl-backlog-size 1mb

Incremental replication
Is the buffer configured as large as possible ?
If the configuration is too large , such as 10G, The actual data in memory may only be 100M, When replica and master The connection between them has been broken long enough , then replica It's reconnected master, Sent a offset To come over , And then it started 10G The transmission of commands ,
In fact, if it is full synchronization at this time , You may only need 100M The transmission of the data of . According to this situation , The buffer should not be too large .

  • min-replicas-to-write 3
  • min-replicas-max-lag 10

If replica and master The connection between is broken ,master How does it work ? It's indifference ?
By default ,replica and master After disconnection ,master I don't do anything , It's just a replica, after all ,master Don't care . But it can be configured through the above , Give Way master care about .
This configuration is represented in 10 At least... In seconds 3 individual replica On-line , and master The connection is normal , otherwise master Write operations from the client are not accepted ,master During this time it became read-only , This will allow replica As much as possible and master The gap between the two countries has narrowed . But it also affects master Normal request for .
Detailed Links :https://blog.51cto.com/happytree007/2586641

Sentinel sentry

Redis Of Sentinel The system is used to manage multiple Redis The server (instance), The system performs the following three tasks :

  • monitor (Monitoring): Sentinel Constantly check whether your master and slave servers are working properly .
  • remind (Notification): When someone is being monitored Redis When there's a problem with the server , Sentinel Can pass API Send notifications to administrators or other applications .
  • Automatic failover (Automatic failover): When a primary server doesn't work , Sentinel An automatic failover operation will start , It will upgrade one of the failed master servers from the server to the new master server , And let the other slave servers of the failed master server copy the new master server ; When a client tries to connect to a failed primary server , The cluster will also return the address of the new primary server to the client , So that the cluster can use the new master server instead of the failed server .

at present Sentinel System is Redis Of unstable Part of a branch , You must go to Redis Project Github page Clone a copy of unstable The score is , Then compile to get Sentinel System .
Sentinel The program can be compiled in src Found in the document , It's a name for redis-sentinel The program .
You can also use the methods described in the next section , Give Way redis-server The program runs in Sentinel In mode .
in addition , A new version of Sentinel It's already included in Redis 2.8.0 Release file of version .

redis The installation directory src It can be found below

 Insert picture description here

Take a look at our system default program directory

 Insert picture description here

[[email protected] redis]  redis-sentinel 26379.conf 

 Insert picture description here

port 26379
##  At present Sentinel Node monitoring  127.0.0.1:6379  This master node  , 1  Represents the minimum required to determine that the master node failed 2 individual Sentinel Node node agreement 
sentinel monitor mymaster 127.0.0.1 16379 2

# sentinel author-pass Define the password for the service ,mymaster It's the service name ,123456 yes Redis Server password 
# sentinel auth-pass <master-name> <password>
sentinel auth-pass mymaster 123456

##  Every Sentinel Nodes are scheduled PING Command to judge Redis Data nodes and the rest Sentinel Whether the node is reachable , If exceeded 10000 MS and no reply , It is unreachable 
sentinel down-after-milliseconds mymaster 10000

You can refer to :https://www.jianshu.com/p/06ab9daf921d

redis-sentinel 26379.conf 
redis-sentinel 26380.conf 
redis-sentinel 26381.conf 

 Insert picture description here

take salve The library is promoted to the main library

slaveof no one
 127.0.0.1:16379> SHUTDOWN

here 26379,26380 Listen for service changes ( Be careful , Wait for the network or heartbeat 1 About seconds )

 Insert picture description here

This moment reminds me

原网站

版权声明
本文为[Floating across the sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270550097076.html