当前位置:网站首页>Redis master-slave realizes 10 second check and recovery
Redis master-slave realizes 10 second check and recovery
2022-07-01 17:54:00 【Brother Xing plays with the clouds】
Redis Master-slave framework
problem :redis down After restart , Can recover key value , Time will be delayed ,down Values between machines will be lost .
Experimental environment , The master and slave are on one The server On , Use different ports .
Solution :redis The master-slave is still not perfect , We make use of redis The master and slave mainly do redis Backup ,master After downtime , Realization 10s Recover as much as possible within key value , The main job is reading and writing , Achieve fast read and write , Do not do any backup , Simple and practical from rdb Way to achieve backup .
The principle of thinking :
redis There are two ways to persist ,rdb And aof Pattern , in consideration of aof The mode grows too fast , Recovery is slow , Just do it from the top rdb Pattern , Lord down after , The script checks the state of the master , Then do it from the top bgsave, Put the top rdb file copy To the master data Directory , Restart the main . By judging the Lord key Worth the quantity , Whether it is the same as the quantity on , To determine whether it returns to normal , And then you can do it key Worth a full recovery . The backup method is , An hour copy Once from the top rbd file , Make an hourly backup .
The idea of script It mainly realizes the above ideas and principles , The script is a little awkward ,2 Be the master ,1 It's done redis from . Awkward , experiment , There is no time to revise
#!/bin/bash
DATE=`date +%Y%m%d%M%s`
REDIS_DIR=/usr/local/redis
REDIS2_DIR=/usr/local/redis2
BIN_DIR=/usr/local/redis/bin
DATA1_DIR=/usr/local/redis/data
DATA2_DIR=/usr/local/redis2/data
PORT1=6378
PORT2=6379
function BACKUP_RDB {
ps -ef |grep $PORT2 >/dev/null
if [ $? -eq 0 ];then
${BIN_DIR}/redis-cli -p $PORT2 bgsave >/dev/null
if [ $? -eq 0 ];then
echo -e "\033[31m MASTER REDIS DOWN AND SLAVE bgsave DONE\033[0m"
${BIN_DIR}/redis-cli -p $PORT2 SLAVEOF NO ONE >/dev/null
fi
fi
}
function CP_RDB {
[ -d ${DATA1_DIR}/redisbackup ] || mkdir ${DATA1_DIR}/redisbackup
if [ -f $DATA2_DIR/dump.rdb ];then
mv ${DATA1_DIR}/dump.rdb ${DATA1_DIR}/redisbackup/dump.rdb.$DATE
cp ${DATA2_DIR}/dump.rdb ${DATA1_DIR}/
fi
}
function START_REDIS {
KEY2_NUM=`${BIN_DIR}/redis-cli -p $PORT2 INFO |grep db[0-9]*.key |awk -F[:=,] '{total+=$3}END{print total}'`
${BIN_DIR}/redis-server ${REDIS_DIR}/etc/redis.conf
netstat -tnlp |grep "$PORT1" >/dev/null
a=$?
if [ $a -ne 0 ];then
while [ $a -ne 0 ];do
sleep 2
netstat -tnlp | grep "127.0.0.1:$PORT1" 1>/dev/null 2>/dev/null
a=$?
done
echo -e "\033[32m MASTER REDIS RUNNING ........\033[0m"
sleep 7
KEY1_NUM=`${BIN_DIR}/redis-cli -p $PORT1 INFO |grep db[0-9]*.key |awk -F[:=,] '{total+=$3}END{print
total}'`
# echo ${KEY1_NUM}
if [ $KEY1_NUM -ge $KEY2_NUM ];then
${BIN_DIR}/redis-cli -p $PORT2 SLAVEOF 127.0.0.1 $PORT1 >/dev/null
echo -e "\033[32m MASTER AND SLAVE are Normal sync \033[0m"
else
echo -e "\033[31m KEY NUM are not equal, MASTER AND SLAVE are not Normal sync,place to check ! \033[0m"
break
fi
else
echo -e "\033[32m MASTER REDIS RUNNING ........\033[0m"
sleep 7
KEY1_NUM=`${BIN_DIR}/redis-cli -p $PORT1 INFO |grep db[0-9]*.key |awk -F[:=,] '{total+=$3}END{print total}'`
# echo ${KEY1_NUM}
if [ $KEY1_NUM -ge $KEY2_NUM ];then
${BIN_DIR}/redis-cli -p $PORT2 SLAVEOF 127.0.0.1 $PORT1 >/dev/null
echo -e "\033[32m MASTER AND SLAVE are Normal sync \033[0m"
else
echo -e "\033[31m KEY NUM are not equal, MASTER AND SLAVE are not Normal sync,place to check ! \033[0m"
break
fi
fi
}
while true;do
sleep 10
netstat -tnlp | grep "127.0.0.1:$PORT1" 1>/dev/null 2>/dev/null
TRAP1=$?
ps -ef |grep "$PORT1" >/dev/null
TRAP2=$?
if [ $TRAP1 -ne 0 ] || [ $TRAP2 -ne 0 ];then
#echo redis-master down
BACKUP_RDB
CP_RDB
START_REDIS
fi
done
In the process of recovery , Compare the main key And the slave after the Master goes down key, To determine whether it returns to normal ,
Problems encountered : In the process of recovery , Lord key The value of is always less than that of key Value , But the main startup is completely normal ,key The value is exactly the same , After thinking, I found , It is the problem of program execution , Script execution is too fast , When it comes to comparison ,key The number of values has not fully recovered , It's just a matter of time , So it's implemented sleep 3 Seconds later , Everything is all right . Everyone should according to their own key It is worth the size of the quantity to determine the judgment sleep Time for .
If there's a better one redis Master-slave implementation , Please share .
边栏推荐
- June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries
- PHP实现敏感词过滤系统「建议收藏」
- Alibaba cloud Li Feifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
- EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
- Penetration practice vulnhub range Nemesis
- [2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
- Report on research and investment prospects of UHMWPE industry in China (2022 Edition)
- MySQL -- explain performance optimization
- There is a new breakthrough in quantum field: the duration of quantum state can exceed 5 seconds
- Reflective XSS vulnerability
猜你喜欢

How to use JMeter function and mockjs function in metersphere interface test

ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing

Rotation order and universal lock of unity panel

(16) ADC conversion experiment

Thinkphp6 - CMS multi wechat management system source code

(十七)DAC转换实验

In aks, use secret in CSI driver mount key vault

DNS

New 95 community system whole station source code

Kia recalls some K3 new energy with potential safety hazards
随机推荐
Three dimensional anti-terrorism Simulation Drill deduction training system software
Common design parameters of solid rocket motor
Equipment simulation and deduction training system software
Work and leisure suggestions of old programmers
【牛客网刷题系列 之 Verilog快速入门】~ 优先编码器电路①
Research Report on China's enzyme Market Forecast and investment strategy (2022 Edition)
MySQL + JSON = King fried
(17) DAC conversion experiment
Cassette helicopter and alternating electric field magnetic manometer DPC
期货先锋这个软件正规吗安全吗?选择哪家期货公司更安全?
vulnhub靶场-hacksudo - Thor
New 95 community system whole station source code
Glidefast consulting was selected as the elite partner of servicenow in 2022
Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
Detailed explanation of string's trim() and substring()
Is Huishang futures a regular futures platform? Is it safe to open an account in Huishang futures?
Oom caused by improper use of multithreading
Replace UUID, nanoid is faster and safer!
[splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON
两数之和c语言实现[通俗易懂]