当前位置:网站首页>[Redis] The core principle of master-slave replication
[Redis] The core principle of master-slave replication
2022-08-09 02:35:00 【Super code meow】
Redis master-slave architecture
A single-machine redis can carry anywhere from tens of thousands to tens of thousands of QPS.For cache, it is generally used to support read high concurrency.Therefore, the architecture is made into a master-slave architecture, with one master and multiple slaves, the master is responsible for writing, and the data is copied to other slave nodes, and the slave nodes are responsible for reading.All read requests go to slave nodes.In this way, horizontal expansion can also be easily achieved, supports high read concurrency.redis replication -> master-slave architecture-> Read and write separation -> Horizontal expansion supports high read concurrency
Core mechanism of redis replication
- redis replicates data to the slave node asynchronously, but starting with redis 2.8, the slave node will periodically confirm the amount of data it replicates each time;
- A master node can be configured with multiple slave nodes;
- slave node can also connect to other slave nodes;
- When the slave node is replicating, it will not block the normal work of the master node;
- When the slave node is doing replication, it will not block its own query operations. It will use the old data set to provide services; but when the replication is completed, it needs to delete the old data set and load the new data set. ThisThe external service will be suspended at that time;
*slave node is mainly used for horizontal expansion and separation of read and write. The expanded slave node can improve the read throughput.
Note:
- If the master-slave architecture is adopted, it is recommended that the persistence of the master node must be enabled. It is not recommended to use the slave node as the data hot backup of the master node, because in that case, if you turn off the persistence of the master, the master may be down.When the machine restarts, the data is empty, and after replication, the data of the slave node may also be lost.
- In addition, various backup schemes of the master also need to be done.In case all local files are lost, select an rdb from the backup to restore the master, so as to ensure that there is data at startup, even if the high availability mechanism explained later is adopted, the slave node can automatically take over the master node,But it is also possible that the master node restarts automatically before sentinel detects the master failure, or it may cause all the slave node data above to be cleared.
The core principle of redis master-slave replication
- When starting a slave node, it sends a PSYNC command to the master node.
- If this is the first time the slave node connects to the master node, a full resynchronization full replication will be triggered.At this point, the master will start a background thread and start generating an RDB snapshot file,
- At the same time, all write commands newly received from the client client are also cached in memory.After the RDB file is generated, the master will send the RDB to the slave, and the slave will first write it to the local disk, and then load it into memory from the local disk,
- The master will then send the write command cached in memory to the slave, and the slave will also synchronize the data.
- If the slave node has a network failure with the master node and is disconnected, it will automatically reconnect. After the connection, the master node will only copy the missing data to the slave part.
Process principle
- When the MS relationship is established between the slave database and the master database, the SYNC command will be sent to the master database
- After the main library receives the SYNC command, it will start saving snapshots in the background (RDB persistence process), and cache the write commands received during the period
- When the snapshot is completed, the master Redis will send the snapshot file and all cached write commands to the slave Redis
- After receiving from Redis, the snapshot file will be loaded and the received cached command will be executed
- After that, the master Redis will send the command from Redis whenever it receives a write command, thereby ensuring data consistency
Disadvantages
- The replication and synchronization of all slave node data are handled by the master node, which will cause too much pressure on the master node and use the master-slave-slave structure to solve
边栏推荐
猜你喜欢
最强分布式锁工具:Redisson
【网络教程】IPtables官方教程--学习笔记3
VS2019编译boost_1_79,生成32位和64位静态库
MT4/MQL4 entry to proficient foreign exchange EA tutorial Lesson 1 Getting to know MetaEditor
微信公众号跳转小程序 wx-open-launch-weapp (最全指南及坑)
【电商运营】不知道怎么做网站优化?这里有你需要知道的一切!
Etcd realize large-scale application service management of actual combat
2022年最流行的自动化测试工具有哪些?全网最全最细都在这里了
数仓第一篇:基础架构
xml引配置文件
随机推荐
How to play knowledge graph in recommender system
313. 超级丑数-暴力解法
使网络安全威胁风险更高和成本更高的五个趋势
“蔚来杯“2022牛客暑期多校训练营7,签到题CFGJ
为什么应用程序依赖关系映射对于云迁移至关重要
Etcd realize large-scale application service management of actual combat
Which is the best increased whole life insurance?Is it really safe?
C#计算SHA1加密和base64编码
YOLOV1详解——Pytorch版
gpio子系统和pinctrl子系统(上)
帮助安全红队取得成功的11条建议
金融行业软件测试面试题(含答案)| 入门指南
点击div内部默认文本被选中
普通人如何增加收入
【HNUMSC】C language second lecture
Open3D 随机采样
不会吧!不会吧!居然还有人不知道重绘以及回流
【izpack】使用izpack为你的程序提供安装程序封装
【剑指offer65】不适用加减乘除做加法
Pytest+request+Allure实现接口自动化框架