当前位置:网站首页>Redis master-slave replication

Redis master-slave replication

2022-07-19 08:10:00 Program three two lines

One 、 summary

1、 What is master-slave replication

After the host data is updated, according to the configuration and Policy , Automatic synchronization to the standby machine master/slaver Mechanism ,master Write first ,slave Mainly reading , Data replication is one-way , From master to slave only , By default , Each station Redis Servers are all primary nodes ; And a master node can have multiple slave nodes ( Or no slave ), But one from A node can only have one master node .

Here's the picture

2、 Master-slave replication

1、 data redundancy : Master-slave replication realizes hot backup of data , It's a way of data redundancy beyond persistence .

2、 Fault recovery : When there is a problem with the master node , Can be served by a slave node , Fast fault recovery ; It's actually a service Redundancy of .

3、 Load balancing : On the basis of master-slave replication , Cooperate with the separation of reading and writing , Write service can be provided by the master node , Read service provided by slave node ( The write Redis Connect master node when data is applied , read Redis Apply connection from node when data ), Share server load ; Especially in writing Read less and read more , Sharing read load through multiple slave nodes , Can be greatly improved Redis Concurrency of servers .

4、 High availability cornerstone : In addition to the above functions , Master slave replication is also the foundation for sentinels and clusters to implement , So master-slave replication is Redis High availability Foundation .

Generally speaking , To put Redis Used in engineering projects , Use only one Redis Absolutely not ,

Here's why :

1、 Structurally , Single Redis The server will have a single point of failure , And a server needs to handle all the request loads , The pressure is Big ;

2、 In terms of capacity , Single Redis Server memory capacity is limited , Even if it's a Redis The memory capacity of the server is 256G, And not all Memory is used for Redis Storage memory , Generally speaking , A single Redis The maximum memory used should not exceed 20G. Products on e-commerce websites , It's usually a upload , Countless times , To say professional is to say " Read more and write less ". For this scenario , We can make the following Architecture :

 

Two 、 Configure master-slave replication

One master and two slaves are configured here ( explain : Here, you can simulate multiple servers by opening different ports on the same server , The actual business requires multiple servers )

1、 establish myredis Folder

Copy multiple redis.conf file

[[email protected] myredis]#cp redis.conf redis6379.conf
[[email protected] myredis]#cp redis.conf redis6380.conf
[[email protected] myredis]#cp redis.conf redis6381.conf

2、 Write contents in three configuration files respectively

include /myredis/redis.conf
pidfile /var/run/redis_6379.pid
port 6379
dbfilename dump6379.rdb


include /myredis/redis.conf
pidfile /var/run/redis_6380.pid
port 6380
dbfilename dump6380.rdb


include /myredis/redis.conf
pidfile /var/run/redis_6381.pid
port 6381
dbfilename dump6381.rdb

3、 Start the three redis service

3 A service passes 3 A different configuration file opens

[[email protected] myredis]#redis-server redis6379.conf
[[email protected] myredis]#redis-server redis6380.conf
[[email protected] myredis]#redis-server redis6381.conf

Open three windows to connect in turn 3 individual redis Service and view each operation information

redis-cli -p 6379

Every redis All are master service , Configure as a Master Two Slave Deserve never be worthy of the Lord

Set the value on the host , You can get it from the slave ! The slave cannot write values !

 

3、 ... and 、 Frequently asked questions

1、 Hang up from the machine

Hang up from the machine , Host information , Recover from the machine , Host information and data replication problems

Hang up here 6380redis service

127.0.0.1:6380>shutdown

View host information

You can see that there is only one from the service

  At this time, write data to the main service

From the service 6381 You can still see the data

start-up 6380 Service view operation information

You can see from the service redis Restart the default main service after hanging up , It needs to be reset to slave service

slaveof 127.0.0.1 6379

View the data , You can see that the main server data is automatically backed up from the beginning

2、 The host hangs up

here shut down 6379 after , From the server or from the server , Will not be superior , Wait for the main service to resume , Such as 6380 Slave information

3、 Layers of links

the previous Slave It could be the next slave and Master,Slave Can also receive other slaves Connection and synchronization requests for , that The slave As the next in the chain master, Can effectively reduce master The pressure of writing !

test :6379 After setting the value 6380 and 6381 You can get !OK

4、 Seek power and usurp the throne

In the case of one master and two slaves , If the host is disconnected , The slave can use the command SLAVEOF NO ONE Change yourself to a host ! This time Wait for other slaves to link to this node . Execute a command on a secondary server SLAVEOF NO ONE Will make this slave server Turn off the copy function , And from the slave server back to the master server , The data set from the original synchronization will not be discarded .

Test data

5、 Sentinel mode Automatic switching

summary

The method of master-slave switching technology is : When the primary server goes down , You need to manually switch one from the server to the primary server , This requires manual intervention , Laborious and arduous , It will also make the service unavailable for a period of time . This is not a recommended way , More time , We give priority to Sentinel mode .

Redis from 2.8 It began to officially offer Sentinel( sentry ) Architecture to solve this problem . An automatic version of the plot to usurp the throne , Be able to monitor the failure of the host in the background , If it fails, it will automatically convert from the library to the main library according to the number of votes . Sentinel mode is a special mode , First Redis The order of the sentry , Sentinels are an independent process , As a process , It will be alone Vertical operation . The principle is that the sentry sends orders , wait for Redis Server response , So as to monitor the operation of multiple Redis example .

To configure

Self defined /myredis New under the directory sentinel.conf file , The name must not be wrong

Configure the content

sentinel monitor mymaster 127.0.0.1 6379 2

Other extended configurations

# Example sentinel.conf
#  sentry sentinel The port on which the instance runs   Default 26379
port 26379
#  sentry sentinel Working directory of 
dir /tmp
#  sentry sentinel Monitored redis The master node  ip port
# master-name  You can name the master node by yourself   Only letters A-z、 Numbers 0-9 、 These three characters ".-_" form .
# quorum  How many configurations sentinel The Sentinels agreed that master Primary node lost connection   At this time, it is objectively considered that the master node is disconnected 
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor mymaster 127.0.0.1 6379 2
#  When in Redis In the example, the requirepass foobared  Authorization code   So all connections Redis The clients of the instance are 
 To provide a password 
#  Set up a sentry sentinel  Password to connect master and slave   Note that the same authentication password must be set for master slave 
# sentinel auth-pass <master-name> <password>
sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
#  Specify how many milliseconds later   The master node didn't answer the sentry sentinel  here   Sentinels subjectively think that the primary node is offline   Default 30 second 
# sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel down-after-milliseconds mymaster 30000
#  This configuration item specifies what is happening failover How many can there be at most during the primary / standby handover slave Simultaneously on the new master Conduct   Same as 
 Step ,
 The smaller the number , complete failover The longer it takes ,
 But if the number is bigger , That means the more   Much of the slave because replication And is not available .
 You can do this by setting this value to zero  1  To make sure there's only one at a time slave  In a state where command requests cannot be processed .
# sentinel parallel-syncs <master-name> <numslaves>
sentinel parallel-syncs mymaster 1
#  Timeout for failover  failover-timeout  It can be used in the following ways :
#1.  The same sentinel To the same master two failover The time between .
#2.  When one slave From a wrong one master Where the synchronized data starts to calculate the time . until slave Corrected to be correct 
master Where the data is synchronized .
#3. When you want to cancel an ongoing failover The time required .
#4. When doing failover when , Configure all slaves Point to the new master Maximum time required . however , Even after this super 
 when ,slaves It will still be correctly configured to point master, But it won't parallel-syncs Here comes the configured rule 
#  Default three minutes 
# sentinel failover-timeout <master-name> <milliseconds>
sentinel failover-timeout mymaster 180000
# SCRIPTS EXECUTION
# Configure the scripts that need to be executed when an event occurs , The administrator can be notified by script , For example, send mail when the system is not working properly 
 Notify the relevant personnel .
# There are the following rules for the result of the script :
# If the script returns 1, Then the script will be executed again later , The number of repetitions currently defaults to 10
# If the script returns 2, Or 2 A higher return value , The script will not repeat .
# If the script is terminated during execution due to receiving a system interrupt , Then the same return value is 1 The same behavior when .
# The maximum execution time of a script is 60s, If I exceed that time , The script will be a SIGKILL Signal termination , After that, I will try again 
 That's ok .
# Notification scripts : When sentinel When any warning level event occurs ( for instance redis Subjective failure and objective failure of examples 
 etc. ), Will call this script , At this point the script should be sent by email ,SMS Etc. to inform the system administrator that the system is abnormal 
 Running information . When the script is called , Two parameters will be passed to the script , One is the type of event , One is the description of the event . If 
sentinel.conf The script path is configured in the configuration file , Then you have to make sure that the script exists in this path , And yes 
 Yes , otherwise sentinel Failed to start normally successfully .
# Notification script 
# sentinel notification-script <master-name> <script-path>
sentinel notification-script mymaster /var/redis/notify.sh
#  The client reconfigures the master node parameter script 
#  When one master because failover And when it changes , This script will be called , Notify related clients about master
 Information that the address has changed .
#  The following parameters will be passed to the script when the script is called :
# <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>
#  at present <state> Always “failover”,
# <role> yes “leader” perhaps “observer” One of them .
#  Parameters  from-ip, from-port, to-ip, to-port It's for the old master And the new master( I.e. old 
slave) communication 
#  This script should be generic , Can be called many times , It's not targeted .
# sentinel client-reconfig-script <master-name> <script-path>
sentinel client-reconfig-script mymaster /var/redis/reconfig.sh

Activate the sentry

Redis-sentinel /myredis/sentinel.conf

The above directories are configured according to their actual situation , Maybe the directory is different

If the main service 6379 Hang up ,10 Seconds or so, you can see sentinel Window log

see 6381 service , The main service has become 6380

Look again 6380

Restart 6370, You can see that it is no longer a master service, but a slave service

How to vote

After the death of the Lord , How to choose one from many to become the new master

  • The priority is redis.conf Middle configuration , Default slave-priority 100 The lower the value, the higher the priority , Some versions redis when replica-priority 100
  • Offset refers to the most complete synchronization of the original host data
  • Every redis A random one will be generated at startup 40 Bit ruuid, The above two are the same , choice ruuid The smallest from service

Code implementation

Just change the connection pool

Master slave replication delay

All writes are in Master operation , Then sync update to slave, So from master To slave The machine has a certain delay , When the system is busy, the delay problem becomes more serious ,slave The increase in the number of machines will also make this problem serious

Four 、 Master slave replication principle

Slave Start successfully connected to master A... Will be sent later sync command

Master Received orders , Start the background storage process , At the same time, collect all received commands for modifying the dataset , Execute in the background process After finishing ,master Transfer the entire data file to slave, And complete a full synchronization .

Copy in full : and slave The service receives the database file data , Save it and load it into memory .

Incremental replication :Master Continue to pass all new collected modification commands to slave, Complete synchronization But just reconnect master, One full synchronization ( Copy in full ) Will be executed automatically

原网站

版权声明
本文为[Program three two lines]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170557240770.html