当前位置:网站首页>Redis master-slave replication
Redis master-slave replication
2022-07-22 15:13:00 【Just number six Z】
Redis Master slave copy
Redis Master slave copy
Master slave copy , It means to put one redis Server data , Copy to other redis The server . The former is called the main node (master/leader), The latter is called the slave node (slave/follower); Data replication is one-way , From master to slave only .Master Write first ,Slave Mainly reading .
By default , Each station redis All servers of are primary nodes ; And a master node can have multiple slave nodes ( Or not from the node ), But a slave node can only have one master node .
The functions of master-slave replication include :
data redundancy : Master-slave replication realizes hot backup of data , It's a way of data redundancy beyond persistence .
Fault recovery : When the primary node has a problem , Can be served by a slave node , Fast fault recovery ; It's actually a redundancy of services .
Load balancing : On the basis of master-slave replication , Cooperate with the separation of reading and writing , The master node can provide write service , There are slave nodes that provide read Services ( The write redis Connect master node when data is applied , When reading data, the application connects to the slave node ), Share server load ; Especially when writing less and reading more , Sharing read load through multiple slave nodes , Can be greatly improved redis Concurrency of servers .
High availability ( colony ) footstone : 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 project engineering , Use only one redis Absolutely not ( Downtime 、 One master and two subordinates ), 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 , pressure .
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, You can't use all the memory Redis Storage memory , Generally speaking , A single Redis The maximum memory used should not exceed 20G.
Environment configuration
Just configure the slave , There is no need to configure the host .
127.0.0.1:6379>info replication View the current library information
# Rep7ication
role : master # role master
master_rep7id: b63c90e6c501143759cb0e7f450bd1eb0c70882a
master_replid2 : 0000000000000000000000000000000000000000
master_rep1_offset: 0
second_rep1_offset :-1
rep7_back1og_active : 0
rep1_back1og_size : 1048576
rep1_backlog_first_byte_offset :0
rep7_backlog_histlen : 0
Edit profile , Copy profile
1. Port number port The port number cannot be repeated
2.pid name pidfile /var/run/redis_6379.pid The background operation should be changed to the corresponding
3.log File name logfile "" Log file rename
4. dump.rdb name dbfilename dump.rdb Modify the name of the data file
After modification, start multiple redis process .
One master and two subordinates
By default , Each station redis Servers are all primary nodes .
In general , Just configure the slave .
slaveoe host port Configure host address 、 Port number
The real master-slave configuration should be configured in the configuration file , Permanent .
The host can write , But the slave can only read, not write .
test : The host is disconnected , The slave is still connected to the host , But there is no write operation , At this time , If the mainframe comes back , The slave can directly obtain the written information of the host .
If you use the master-slave configured by the command line , If you restart , It will become the original default configuration . As long as it becomes a slave , You can get the host data immediately .
The principle of replication
Slave Start successfully connected to master A... Will be sent later sync Synchronization command
Master Received orders , Start the background storage process , At the same time, collect all received commands for modifying the dataset , After the background process is finished ,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 .
Layers of links
The previous master node connects to the next slave node , There is one redis The server acts as an intermediate bridge . Lord -------> from ( Lord )----------> from
slaveof no one If the host is disconnected , You can make yourself the master node , Other nodes can be manually connected to this host
Sentinel mode
The election mode of the boss is automatic
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 human intervention , Laborious and arduous , It will also make the service unavailable for a period of time .l This is not a recommended way , More time , We prioritize 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 run on its own . The principle is that the sentry sends orders , wait for Redis Server response , So as to monitor the operation of multiple Redis example .
The Sentinels here have two functions
By sending commands , Give Way Redis The server returns to monitor its running state , Including the master and slave servers .
· When the Sentinels detect master Downtime , Will automatically slave Switch to master, Then the other slave servers are informed through publish subscribe mode , Modify the configuration file , Let them switch hosts .
But a sentinel process is right Redis The server monitors , There may be problems , So , We can use multiple sentinels to monitor . There will be surveillance between the Sentinels , In this way, the multi sentry mode is formed .
Suppose the primary server goes down , sentry 1 First detect the result , The system doesn't work right away failover The process , Just sentinels 1 Fengguan believes that the main server is unavailable , This phenomenon becomes subjective offline 、 When the rear sentry also detects that the primary server is not available , And when the quantity reaches a certain value , Then there will be a vote between the Sentinels , The result of the vote was initiated by a sentinel failover[ Fail over ] operation . After switching successfully , Through the publish and subscribe mode , Let each sentry switch the host from the server that they monitor , This process is called objective offline .
test
1. New sentry profile sentinel.conf
vim sentinel.conf
2. To configure
sentinel monitor myredis( The name being monitored ) 127.0.0.1 6379 1( Indicates voting )
1 On behalf of the host is hung up ,slave Vote to make the new host .
3. Activate the sentry
redis-sentinel kconfig/sentinel.conf
failover Fail over
If master The node is disconnected , You will randomly select a server from the slave .
If the original host is reconnected at this time , Then become the slave of the new host
advantage :
1. The sentry cluster , Based on master-slave replication , It has all the advantages of master-slave configuration
2. Master slave can switch , Faults can be transferred , The usability of the system will be better
3. Sentinel mode is the upgrade of master-slave mode , From manual to automatic , More robust
shortcoming :
1.redis Not good for online expansion , Once the cluster capacity reaches the online level , Online expansion is very troublesome
2. The configuration of sentinel mode is very troublesome
All configuration
#Example sentinel.conf
# sentry sentine1 The default port of the instance is 26379
port 26379
# sentry sentine1 Working directory of
dir /tmpl
# sentry sentine1 Monitored redis The master node ip port
# master-name The name of a master node that can be named by itself can only be composed of letters A-z、 Numbers 0-9、 These three characters ".-_" form .
# quorum How many configurations sentine1 The Sentinels agreed that master When the primary node is disconnected, it is objectively considered that the primary node is disconnected
了 # sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentine1 monitor mymaster 127.0.0.1 6379 2
# When in Redis In the example, the requirepass foobared Authorization password so that all connections Redis The client of the instance must provide the password
# Set up a sentry sentinel The password for connecting master and slave. Note that the same authentication password must be set for master and slave
# sentine1 auth-pass <master-name> <password>
sentinel auth-pass mymaster MySUPER--secret-0123passwOrd
# After a specified number of milliseconds, the master node does not respond to the sentinel sentine1 At this time, the sentinel subjectively thinks that the primary node is offline by default 30 second
# sentine7 down-after-mi77iseconds <master-name> <mil7iseconds>
sentine7 down-after-mi17iseconds mymaster 30000
# This configuration item specifies what is happening failover How many can there be at most during the primary / standby handover s1ave Simultaneously on the new master Conduct _ Sync , The smaller the number , complete failover The longer it takes , But if the number is bigger , It means the more s1ave 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 .
#sentine1 para1le1-syncs <master-name> <nums1aves>
sentinel para1le1-syncs mymaster 1
# Timeout for failover failover-timeout It can be used in the following ways :
#1. The same sentine1 To the same master two fai1over 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 fai1over The time required .
#4. When doing faiToveri when , Configure all slaves Point to the new master Maximum time required . however , Even after this timeout ,slaves It will still be correctly configured to point master, But it won't paralle1-syncs Here comes the configured rule
# Default three minutes
# sentinel failovertimeout <master-name> <mil7iseconds>
sentine1 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, when the system is not running normally, send an email to inform 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 , And then re execute .
# Notification scripts : When sentine7 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 ,SNS Wait for the way to inform the system administrator about the abnormal operation of the system . 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 sentine1.conf The script path is configured in the configuration file , Then you have to make sure that the script exists in this path , And it's executable , otherwise sentine1 Failed to start normally successfully .
# Notification script
# sentinel notification-script <master-name> <seript-path>
sentinel notification-script mymaster /var/redi/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",
# Parameters from-ip,from-port,to-ip,to-port It's for the old master And the new master( I.e. old slave)
# This script should be generic , Can be called many times , It's not targeted .
#sentinel client-reconfig-script <master-name> <script-path>
sentine1 client-reconfig-script mymaster /var/redis/reconfig.sh
边栏推荐
- Spark summary
- Notes de classe de la Silicon Valley (Partie 2)
- Worthington cell separation optimization system (including cell separation guide)
- 测试下投票的功能
- 《PyTorch深度学习实践》-1-Overview
- ZCMU--1925: hx & xh‘s game(C语言)
- Redis ---- three special data types, transaction, integration, configuration file, persistent RDB and AOF, publish and subscribe, master-slave replication (sentinel mode), cache penetration and avalan
- Redis----三大特殊数据类型、事务、整合、配置文件、持久化rdb和aof、发布订阅、主从复制(哨兵模式)、缓存穿透和雪崩
- RPC core module summary
- 测试下定时发布的时间
猜你喜欢
How to make the signal of conductive slip ring better
Spark summary
2022-07-13 mysql/stonedb的快速子查询和慢子查询执行对比
Redis的拓展方案
visual studio踩坑记录
NAT技术
Notes de classe de la Silicon Valley (Partie 2)
Study on the secretory enzymes of Worthington fungi -- galactose oxidase scheme
让安全动起来 | 甭管什么行业网络架构,这六招拿下靶标
Worthington 细胞分离优化系统(含细胞分离指南)
随机推荐
硅谷课堂笔记(下)
1382. Balance the binary search tree ●●
《PyTorch深度學習實踐》-1-Overview
Worthington 胆碱酯酶,丁酰相关说明书
Worthington真菌分泌酶研究 丨半乳糖氧化酶方案
等保合规2022系列 | 一个中心+三重防护,助力企业等级保护建设更科学
Tencent cloud deploys Devops
Worthington肽合成应用丨胰凝乳蛋白酶方案
Redis的拓展方案
MATLAB basic grammar (II)
Redis的conf配置
2022-07-13 mysql/stonedb子查询-优化器处理记录
Goodbye attention: a new paradigm for modeling users' long-term interests
细胞分离研究丨Worthington梭菌蛋白酶方案
How to operate around users' digitalization?
Redis----三大特殊数据类型、事务、整合、配置文件、持久化rdb和aof、发布订阅、主从复制(哨兵模式)、缓存穿透和雪崩
2022-07-18 Jenkins pipeline use and create your own pipeline
2022-07-15 mysql/stonedb子查询校验exists流程分析
Redis持久化(RDB和AOF)
Voting cannot be repeated