当前位置:网站首页>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 # related knowledge points
- 比较DFS和BFS的优点和缺点及名称词汇
- A. Vacations (dp 贪心
- GCC compilation process, function library related compilation process
- 论文笔记: 多标签学习 BP-MLL
- Upgrade the project of log4j to log4j2
- Issues related to C # delegation and events
- 理财产品连续几天收益都是零是怎么回事?
- Ticdc synchronization task
- It's called the next generation monitoring system. Let's see how awesome it is
猜你喜欢

Implementation of fruit mall wholesale platform based on SSM

redis-1. Install redis with pictures and texts
![[vivefocus uses the wavevr plug-in to obtain handle operation events]](/img/4f/2ca02799ef5cde1a28101d61199856.jpg)
[vivefocus uses the wavevr plug-in to obtain handle operation events]

Export chrome plug-ins and import local plug-ins to chrome

Three handshakes and four waves of TCP protocol and why------ One two pandas

论文笔记: 多标签学习 BP-MLL

Mui mixed development - when updating the download app, the system status bar displays the download progress

Login registration

量化框架backtrader之一文讀懂Analyzer分析器

Data desensitization tool advance tool Datamask
随机推荐
redis-3. Redis list, set, hash, sorted_ set、skiplist
Un des backtraders du cadre de quantification lit l'analyseur
socket编程2:IO复用(select && poll && epoll)
Find the first and last positions of elements in a sorted array
Postgraduate entrance examination English
Compilation and development process of Quanzhi v3s environment
China phosphate market in-depth analysis and investment prospect forecast report 2022-2028
Pdf to word
Calculate running total / running balance
Three handshakes and four waves of TCP protocol and why------ One two pandas
C # Advanced Programming - Feature Section
[hard copy] core differences among dirty reading, non repeatable reading and unreal reading scenarios
Tree list under winfrom treelist related
6. system call
Questions about ETL: io trino. jdbc. TrinoDriver
Adding certificates to different systems
Redis learning journey -- do you know the data types of redis?
Export chrome plug-ins and import local plug-ins to chrome
Redis learning journey --redis Conf details
Some optimization for seckill project