当前位置:网站首页>Redis master-slave replication - underlying principle
Redis master-slave replication - underlying principle
2022-06-13 07:34:00 【A hard-working dog】
Redis Master slave copy
adopt Slaceof Options , Let one server replicate another , The server to be replicated is the primary server , The server that replicates the primary server is called the secondary server .
SLAVEOF <IP><PORT>
The data of both the master and slave servers during replication will be the same , Call it data consistency .
Implementation of data consistency
SYNC command ( The old version )
The synchronization operation from the slave server to the master server needs to send the SYNC Command complete .
- Sent from the slave server to the master server SYNC command
- received SYNC The main server of the command executes BGSAVE command , Generate a... In the background RDB file , And use a buffer to record all write commands executed from now on .
- When the main server BGSAVE At the end of the command , The master server will BGSAVE Command generated RDB File sent to slave , Receive and load this... From the server RDB file , Update your own database state to the master server for execution BGSAVE Database state at command time .
- The master sends all write commands recorded in the buffer to the slave , Execute these write commands from the server , Update your database status to the current status of the primary server database .
Keep data consistent in real time
When the primary server is modified , The command propagation operation will be performed on the slave server : The master server will execute its own write commands , That is, the write command that causes inconsistency between the master and slave servers , Send to execute... From server , After executing the same write command from the server , The master and slave servers will return to the consistent state again .
Defects in the old copy function
When you drop the line from the server and reconnect , The primary server will execute again SYNC command , It will cause a lot of waste of resources .
PSYNC command ( The new version )
Redis Use PSYNC Command to realize synchronous operation during replication .
PSYN Commands have full synchronization and partial resynchronization .
- Full resynchronization : Used to handle initial replication : Full resynchronization steps and SYNC The execution steps of the command are basically the same , All are By having the master server create and send RDB file , And send the write command saved in the buffer to the slave server for synchronization .
- Partial resynchronization : Used to handle copying after disconnection : When the slave server reconnects to the master server after disconnection , If conditions permit , The master server can send the write command executed during the disconnection of the master-slave server to the slave server , Just receive and execute these write commands from the server , You can update the database to the current state of the primary server .
Time | master server | From the server |
T0 | The master-slave server synchronization is completed | The master-slave server synchronization is completed |
T1 | Execute and propagate commands SET K2 V2 | Execute the SET k2 v2 |
....... | ....... | ........ |
T1000 | The master and slave servers are disconnected | The master and slave servers are disconnected |
T1001 | perform SET K10086 V10086 | Disconnection attempt reconnection |
T1002 | perform SET K10087 V10087 | Disconnection attempt reconnection |
T1003 | The master and slave servers are reconnected | The master and slave servers are reconnected |
T1004 | Send to primary server PSYNC command | |
T1005 | Return to the slave server +CONTINUE reply , Represents a partial resynchronization | |
T1006 | receive +CONTINUE reply , Ready to perform partial resynchronization | |
T1007 | To the slave server K10086 V10086、 K10087 V10087 command | |
T1008 | Receive and execute two messages from the master server SET command | |
T1009 | The master and slave servers complete the synchronization again | The master and slave servers complete the synchronization again |
It's obvious that PSYNC Partial resynchronization ratio of the command SYNC Many fast resources are much less , because PSYNC For partial resynchronization, you only need to send the missing write command from the slave server to the slave server for execution .
边栏推荐
- C language: how to give an alias to a global variable?
- Distributed transaction learning (I) preliminary understanding
- It's called the next generation monitoring system. Let's see how awesome it is
- redis-1. Install redis with pictures and texts
- redis-3. Redis list, set, hash, sorted_ set、skiplist
- [splashsplash] repeat the script that outputs splashsurf
- C # related knowledge points
- 2021-10-08
- MySQL summary
- Simple understanding of basic language of C language
猜你喜欢
redis-2. Redis string type & bitmap
MySQL row column conversion (updated version)
redis-1. Install redis with pictures and texts
Table access among Oracle database users
Login registration
How worker threads in the thread pool are recycled
关于#数据库#的问题:PGADMIN4 编辑sql窗口问题
2021-10-20
About database: pgadmin4 editing SQL window
Make cer/pfx public and private key certificates and export CFCA application certificates
随机推荐
Redis cluster parsing docker building redis cluster
量化框架backtrader之一文讀懂Analyzer分析器
How app inventor accesses resource files in assets directory
Fundamentals of assembly language: register and addressing mode
Priority analysis of list variables in ansible playbook and how to separate and summarize list variables
Database outline
Nodejs file module FS
Simple use of logs
Wechat applet - positioning, map display, route planning and navigation
Time field comparison time size in MySQL
redis-1. Install redis with pictures and texts
Socket programming 2:io reuse (select & poll & epoll)
[vivefocus uses the wavevr plug-in to obtain handle operation events]
Login registration
平衡二叉树学习笔记------一二熊猫
10. process communication
Relevant knowledge under WinForm
Classification of databases
JMeter encryption interface test
c#高級編程-特性篇