当前位置:网站首页>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 .

边栏推荐
- 【硬记】脏读、不可重复读、幻读场景核心区别
- Quick sort
- 25 domestic and foreign literature databases
- Priority analysis of list variables in ansible playbook and how to separate and summarize list variables
- Functions about Oracle.
- Redis learning journey - cache exceptions (CACHE penetration, cache avalanche, cache breakdown)
- Three handshakes and four waves of TCP protocol and why------ One two pandas
- Redis learning journey - persistence
- [log4j2 log framework] sensitive character filtering
- Database outline
猜你喜欢
![[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]

Redis learning journey - cache exceptions (CACHE penetration, cache avalanche, cache breakdown)

redis-4. Redis' message subscription, pipeline, transaction, modules, bloom filter, and cache LRU

Ticdc introduction

redis-5. Redis' RDB, fork, copyonwrite, AOF, RDB & AOF are mixed

Problems encountered during commissioning of C # project

GCC compilation process, function library related compilation process

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

About database: pgadmin4 editing SQL window

论文笔记: 多标签学习 BP-MLL
随机推荐
I always don't understand the high address and high position
C drawing table and sending mail function
Export chrome plug-ins and import local plug-ins to chrome
The management practice of leading enterprises has proved that what is the core of sustainable development of enterprises?
TCP协议的三次握手过程和四次挥手过程以及为什么要这样? ------一二熊猫
Classification of databases
Database connection under WinForm
关于#etl#的问题:io.trino.jdbc.TrinoDriver
Simple understanding of basic language of C language
Oracle problem: the data in the field is separated by commas. Take the data on both sides of the comma
Considerations for using redis transactions
nodejs文件模块fs
C Advanced Programming - features
How to write an amazing design document?
Sharp weapon tcpdump
Relevant knowledge under WinForm
MySQL summary
Three handshakes and four waves of TCP protocol and why------ One two pandas
思路清晰的软光栅小引擎和四元数结合案例
How idea breaks point debugging